Live data from Hacker News

Bash patterns I use weekly

will-keleher.com

11–20 of 115 posts

Re: Bash patterns I use weekly

#11

I am confused how this works. I would assume `SECONDS` would just be a shell variable and it was first assigned `0` and then it should stay same, why did it keep counting the seconds? > SECONDS bash: SECONDS: command not found > SECONDS=0; sleep 5; echo $SECONDS; 5 > echo "Your command completed after $SECONDS seconds"; Your command completed after 41 seconds > echo "Your command completed after $SECONDS seconds"; Yo…

`SECONDS` is like `PWD`: the shell keeps track of updating it somewhere, and it'll tell you how long your shell has been running.

https://www.oreilly.com/library/view/shell-scripting-expert/...

Re: Bash patterns I use weekly

#12
post #11

I am confused how this works. I would assume `SECONDS` would just be a shell variable and it was first assigned `0` and then it should stay same, why did it keep counting the seconds? > SECONDS bash: SECONDS: command not found > SECONDS=0; sleep 5; echo $SECONDS; 5 > echo "Your command completed after $SECONDS seconds"; Your command completed after 41 seconds > echo "Your command completed after $SECONDS seconds"; Yo…

`SECONDS` is like `PWD`: the shell keeps track of updating it somewhere, and it'll tell you how long your shell has been running. https://www.oreilly.com/library/view/shell-scripting-expert/...

Thank you. That make sense now.

Re: Bash patterns I use weekly

#13

I want to like this, but the for loop is unnecessarily messy, and not correct. for route in foo bar baz do curl localhost:8080/$route done That's just begging go wonky. Should be stuff="foo bar baz" for route in $stuff; do echo curl localhost:8080/$route done Some might say that it's not absolutely necessary to abstract the array into a variable and that's true, but it sure does make edits a lot easier. And, the orig…

Thanks for the note about the semicolon! Added it

Re: Bash patterns I use weekly

#14
> Use for to iterate over simple lists

I definitely use this all the time. Also, generating the list of things over which to iterate using the output of a command:

    for thing in $(cat file_with_one_thing_per_line) ; do ...

Re: Bash patterns I use weekly

#15
> git bisect is the "real" way to do this, but it's not something I've ever needed

uh, yeah, you did need it, that's why you came up with "2. Track down a commit when a command started failing". Seriously though, git bisect is really useful to track down that bug in O(log n) rather than O(n).

Re: Bash patterns I use weekly

#16
post #7

I want to like this, but the for loop is unnecessarily messy, and not correct. for route in foo bar baz do curl localhost:8080/$route done That's just begging go wonky. Should be stuff="foo bar baz" for route in $stuff; do echo curl localhost:8080/$route done Some might say that it's not absolutely necessary to abstract the array into a variable and that's true, but it sure does make edits a lot easier. And, the orig…

Even more correct would be to use an array: stuff=("foo foo" "bar" "baz") for route in "${stuff[@]}"; do curl localhost:8080/"$route" done

"${stuff[@]}" would be turning it back into "foo bar baz" though, right? I think if you were using arrays for this, it'd be something like:

    stuff=("foo" "bar" "baz");
    array_length=${#stuff[@]};
    for i in $(seq 0 $array_length); do
      curl localhost:8080/${stuff[i]}
    done
I'm betting even that isn't right: as soon as bash arrays are a thing, I reach for a different language.

[edit]: trying to get formatting correct

Re: Bash patterns I use weekly

#17
This thread seems like a good place to ask this:

When you're running a script, what is the expected behaviour if you just run it with no arguments? I think it shouldn't make any changes to your system, and it should print out a help message with common options. Is there anything else you expect a script to do?

Do you prefer a script that has a set of default assumptions about how it's going to work? If you need to modify that, you pass in parameters.

Do you expect that a script will lay out the changes it's about to make, then ask for confirmation? Or should it just get out of your way and do what it was written to do?

I'm asking all these fairly basic questions because I'm trying to put together a list of things everyone expects from a script. Not exactly patterns per se, more conventions or standard behaviours.

Re: Bash patterns I use weekly

#18

This thread seems like a good place to ask this: When you're running a script, what is the expected behaviour if you just run it with no arguments? I think it shouldn't make any changes to your system, and it should print out a help message with common options. Is there anything else you expect a script to do? Do you prefer a script that has a set of default assumptions about how it's going to work? If you need to mo…

IMO any script that makes any real changes (either to the local system or remotely) should take some kind of input.

It's one thing if your script reads some stuff and prints output. Defaulting to the current working directory (or whatever makes sense) is fine.

If the script is reading config from a config file or envvars then it should still probably get some kind of confirmation if it's going to make any kind of change (of course with an option to auot-confirm via a flag like --yes).

For really destructive changes it should default to dry run and require an explicit —-execute flag but for less destructive changes I think a path as input on the command line is enough confirmation.

That being said, if it’s an unknown script I’d just read it. And if it’s a binary I’d pass —-help.

Re: Bash patterns I use weekly

#19
post #18

This thread seems like a good place to ask this: When you're running a script, what is the expected behaviour if you just run it with no arguments? I think it shouldn't make any changes to your system, and it should print out a help message with common options. Is there anything else you expect a script to do? Do you prefer a script that has a set of default assumptions about how it's going to work? If you need to mo…

IMO any script that makes any real changes (either to the local system or remotely) should take some kind of input. It's one thing if your script reads some stuff and prints output. Defaulting to the current working directory (or whatever makes sense) is fine. If the script is reading config from a config file or envvars then it should still probably get some kind of confirmation if it's going to make any kind of cha…

Thanks for the reply! I really appreciate being able to pick other folks' brains on here :)

Re: Bash patterns I use weekly

#20
post #13

I want to like this, but the for loop is unnecessarily messy, and not correct. for route in foo bar baz do curl localhost:8080/$route done That's just begging go wonky. Should be stuff="foo bar baz" for route in $stuff; do echo curl localhost:8080/$route done Some might say that it's not absolutely necessary to abstract the array into a variable and that's true, but it sure does make edits a lot easier. And, the orig…

Thanks for the note about the semicolon! Added it

Sure thing. I apologize if my comment came across as overly negative. My goal was constructive criticism, but based on the downvotes, I'm guessing I missed the mark there.
Post reply on HN