Fork me on GitHub
#babashka
<
2023-08-17
>
pesterhazy08:08:03

Show 'n Tell: rempath – remove a segment from your PATH

🎉 2
pesterhazy08:08:07

#!/usr/bin/env bb

# rempath
#
# Runs a command with a segment removed from PATH
#
# Usage:
#
# rempath path command [args ...]
#
# Example:
#
# rempath /usr/local/bin zsh

(let [needle (first *command-line-args*)
      path (System/getenv "PATH")
      segments (-> path (clojure.string/split #":"))
      path (clojure.string/join ":" (remove #{needle} segments))]
  (apply babashka.process/exec {:extra-env {"PATH" path}} (rest *command-line-args*)))

pesterhazy08:08:28

Nightmare to build in bash, easy as pie in bb

jackrusher09:08:24

This hardly seems like the stuff of nightmares? :thinking_face:

(PATH=`echo $PATH | sed 's!/usr/local/bin:!!g'` zsh)

pesterhazy09:08:38

Maybe not a nightmare but also not exactly correct: • doesn't work if /usr/local/bin is the last segment • will mess up /opt/usr/local/bin • trouble afoot if PATH contains *

touch foo.txt; foo='f*'; echo $foo
foo.txt

genRaiy10:08:48

I prefer to use tr for these things eg

genRaiy10:08:09

echo $PATH | tr ':' '\n' | egrep -v '^/usr/local/bin' | tr '\n' ':'

👍 6
2
genRaiy10:08:14

but yeah, a bit niche

mmer11:08:29

Although the example at the start of this thread is longer, it is understandable, Although I am not sure about using path twice in a let. Is clearly works, but looks strange to my eye.

jackrusher14:08:51

The point is that it isn’t much work in shell either. If your argument is that you like to do all of your programming tasks in your favorite language, that’s perfectly reasonable, but let’s not exaggerate the difficulties of using other tools.

💯 4
pesterhazy14:08:19

I think it's a significant amount of work and takes a lot of skill to do a task like this correctly in bash (it's easy in perl, python as well as bb)

genRaiy14:08:32

I've used the shell for many years so it doesn't feel so terrible /alien to me. But that's less common these days and even for me, once it gets beyond 10 or so lines or there is anything non-line terminated text (eg JSON) involved, the shell starts to lose its appeal. (I think @U04V15CAJ uses something similar to these metrics when marketing bb) I think you found an example that's on the edge cos it's reasonable in the shell • at least as easy to compose / experiment than with bb • .. and yes you can use vi or emacs as a shell inline editor too, if that's needed and it's arguably more work in Clojure - but it depends on what you're most comfortable with.

borkdude14:08:25

I wouldn't have come up with the tr example myself, without looking up the docs of tr and egrep, although I would have come up with the bb example without looking up any docs. 🤷

borkdude14:08:44

It's a matter of experience here. I guess people older than 50 are in favor of the tr solution :P

😆 2
mmer14:08:17

Not this one

borkdude14:08:51

ok, that was a bad take then ;)

mmer14:08:19

No I was agreeing with and I am 61

genRaiy14:08:17

stay young with babashka 💅:skin-tone-3:

🧡 8
borkdude14:08:25

the woman in the logo probably knows a bit of tr

💡 2
genRaiy15:08:59

which woman?

borkdude15:08:14

oh you thought it was a hacker with a hoodie? well, good to know it appeals to those people too ;)

genRaiy15:08:28

echo :x-ray: ❤️ babashka 2️⃣ | sed 's/^x-//'

😁 2