Table of Contents

Bash scripting cheatsheet

https://devhints.io/bash

https://www.ullright.org/ullWiki/show/bash-shell-scripting-cheat-sheet

curl check website reachability

for i in $(seq 10); do echo "$i:"; curl --connect-timeout 1 -sLk https://ipinfo.io/ip -w "%{http_code}\n" -o /dev/null; done

cut

https://youtu.be/_GxXbM0METw

test command

Using test command

One can use the test command to check file types and compare values. For example, see if FILE exists and is a directory. The syntax is:

test -d "DIRECTORY" && echo "Found/Exists" || echo "Does not exist"

The test command is same as [ conditional expression. Hence, you can use the following syntax too:

[ -d "DIR" ] && echo "yes" || echo "noop"

Getting help Read bash shell man page by typing the following man command or visit online here:

man bash
help [
help [[
man test

if statements

https://linuxacademy.com/blog/linux/conditions-in-bash-scripting-if-statements/

sunrise/sunset

http://www.giuseppeparrello.it/en/prg_bash_sunrise_sunset.php

https://linuxconfig.org/how-to-obtain-sunrise-sunset-time-for-any-location-from-linux-command-line

https://unix.stackexchange.com/questions/2931/sunrise-and-other-astronomical-data-from-the-command-line

examples

cat /proc/cpuinfo | awk '/^processor/{print $3}' | wc -l

Loop

Bash for loop

samples loops

while 1 < 4; do nc -vz -w 1 google.de 443; done
for i in {1..1000}; do nc -vz -w 1 google.de 443 ; done
until [ 1 -ge 5 ]; do nc -vz -w 1 google.de 443 ; done

xargs opkg upgrade

opkg list-upgradable | cut -f 1 -d ' ' | xargs -r opkg upgrade  

https://www.thegeekstuff.com/2013/12/xargs-examples/