News for hackers. Stuff that matters.
Re:Interesting commands for the Linux shell by hnlmorg on Thursday September 14, 2017 @06:25AM (#15246508)
Funny 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.
Re:Interesting commands for the Linux shell by Tepix on Thursday September 14, 2017 @07:02AM (#15246723)
Indeed! Also if $RANDOM is too small for your needs, you can use $RANDOM$RANDOM (and it will have different values)
Re:Interesting commands for the Linux shell by jwilk on Thursday September 14, 2017 @07:19AM (#15246798)
$RANDOM$RANDOM doesn't have uniform distribution.

Use $((RANDOM + (RANDOM << 15))) instead.

Re:Interesting commands for the Linux shell by vacri on Thursday September 14, 2017 @06:52AM (#15246669)
There 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
Re:Interesting commands for the Linux shell by monsieurbanana on Thursday September 14, 2017 @07:10AM (#15246760)
That's nifty.

    $ echo "$SECONDS / 60 / 60" | bc
    362.38500000000000000000
Re:Interesting commands for the Linux shell by 1024core on Thursday September 14, 2017 @08:38AM (#15247307)
"bc", without the "-l" option, will give you integers only. So the response of the command above will be "362", and not "362.38500000000000000000"
Re:Interesting commands for the Linux shell by mikegerwitz on Thursday September 14, 2017 @08:39AM (#15247317)
echo "$(( SECONDS / 60 / 60 ))"
Re:Interesting commands for the Linux shell by vram22 on Thursday September 14, 2017 @08:48AM (#15247380)
echo "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.

Re:Interesting commands for the Linux shell by lozf on Friday September 15, 2017 @05:22AM (#15255751)
Recently 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)}"
Re:Interesting commands for the Linux shell by vram22 on Friday September 15, 2017 @11:27AM (#15257915)
Interesting. 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.

Re:Interesting commands for the Linux shell by otherflavors on Saturday September 16, 2017 @03:51PM (#15266390)
printf "%s 60 60 *2k/p" "$SECONDS" | dc
Re:Interesting commands for the Linux shell by alexanderdmitri on Thursday September 14, 2017 @01:43PM (#15250472)
Yeah, 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."

Re:Interesting commands for the Linux shell by samueloph on Thursday September 14, 2017 @09:13AM (#15247605)
I 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