5 ms·
Doesn't matter how long you've been using a GNU/Linux shell, you'll always learn something new every day. Thanks for this.
by stiangrindvoll 9y ago
Doesn't matter how long you've been using a GNU/Linux shell, you'll always learn something new every day.
Thanks for this.
- hnlmorg 9y agoFunny enough I was saying this to a work colleague yesterday when I was looking at a shell script and scratching me head wondering how it was working when a variable named $RANDOM wasn't assigned a value. After a little investigation it turned out Bash (and possibly other $SHELL's?) returns a random number with each call of the $RANDOM variable. Handy little trick.
- vacri 9y agoThere is also the inbuilt variable $SECONDS, which seconds since the instance of bash was started. Makes it really easy to do 'time elapsed' at the end of a bash script. $ echo $SECONDS 83783 $ echo $SECONDS 83784 $ echo $SECONDS 83787
- monsieurbanana 9y agoThat's nifty. $ echo "$SECONDS / 60 / 60" | bc 362.38500000000000000000
- 1024core 9y ago"bc", without the "-l" option, will give you integers only. So the response of the command above will be "362", and not "362.38500000000000000000"
- mikegerwitz 9y agoecho "$(( SECONDS / 60 / 60 ))"
- vram22 9y agoecho "scale=2; $SECONDS / 60 / 60" | bc # will give 2 digits after the decimal point, instead of bc -l which gives more digits of precision after the decimal point, than we may usually want for this particular calculation.
- lozf 9y agoRecently I needed results that included nothing after the decimal point, so resorted to awk to do the math: awk "BEGIN { print int($SECONDS /60 /60)}"
- vram22 9y agoInteresting. I suppose you had to type Ctrl-D or Ctrl-Z to signal no input to awk, since it expects some in the above command? Never tried running awk without either a filename as input or stdin coming from a file or a pipe. Will check it and see what happens. Modifying my example from above: echo "scale=0; $SECONDS / 60 / 60" | bc might also work, need to check.
- otherflavors 9y agoprintf "%s 60 60 *2k/p" "$SECONDS" | dc
- samueloph 9y agoI prefer to use time when i want to know how much time some script took: $ time sleep 2 real 0m2.003s user 0m0.002s sys 0m0.001s
- alexanderdmitri 9y agoYeah, SECONDS is great. I just discovered this a few days ago to get a general idea of execution time for each process a script handles: # At top of script SECONDS = 0 # script commands ... # At bottom of script duration=$SECONDS echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed."
- Tepix 9y agoIndeed! Also if $RANDOM is too small for your needs, you can use $RANDOM$RANDOM (and it will have different values)
- jwilk 9y ago$RANDOM$RANDOM doesn't have uniform distribution. Use $((RANDOM + (RANDOM << 15))) instead.
- Aissen 9y agoIndeed, I was about to say this. I clicked thinking "No way I'll learn something here". Turns out there's 2 or 3 commands that I didn't know and might prove useful!
- kps 9y ago> Doesn't matter how long you've been using a GNU/Linux shell Or a Unix shell, even.