====== Bash ====== Bash stuff that I'm tired of looking for. ===== Command Substitution ===== Backticks or the other thing I never remember. `ping -c10 -W2 1.1.1.1` $(ping -c10 -W2 1.1.1.1) ===== Loops ===== for x in 0{1..9} {10..12} ; do mkdir 2016/2016-$x while read LINE; do echo $LINE; done < FILE ==== Ping Sweep with For Loop ==== From: https://www.rubyguides.com/2012/02/cli-ninja-ping-sweep/ Linux Ping Sweep for i in {1..254} ;do (ping -c 1 192.168.1.$i | grep "bytes from" &) ;done What this does is a for loop from 1 to 254, $i takes the value of the current iteration so in the first one it will be 1 then 2, 3… and so on, then we tell it to call the ping command with the -c option which means only ping once otherwise it would ping forever after that we pipe the output to grep so we only see the hosts that actually responded and the & at the end send it to the background so it will launch all the pings in parallel. If we only want the ip address and not the whole line we can further filter this using cut. ===== Detect if Script is Run by Cron ===== # Check if script is cron or shell - two versions. * [[https://stackoverflow.com/questions/3214935/can-a-bash-script-tell-if-its-being-run-via-cron|StackOverflow: Can a bash script tell if it's being run via cron?]] if [ -t 1 ] ; then echo "interacive mode"; else #send mail fi CRON=$(pstree -s $$ | grep -q cron && echo true || echo false) then test with if $CRON then echo "Being run by cron" else echo "Not being run by cron" fi