############################################## • if…then • if…then…else • if…then…elif • for…in • for • while • until • break and continue • case • select • exec • Interrupt processing (SIGNAL) • Functions() ############################################## if [[ string/numeric expression ]] then command(s) elif [[ string/numeric expression ]] command(s) else command(s) fi ############################################## if (( numeric expression )) then command(s) elif [[ string/numeric expression ]] command(s) else command(s) fi ############################################## for _variable in list do command(s) done ############################################## for _IPS in `dig google.com | grep -v ';' | sed '1d' | awk '{print $5}' | uniq`; do ping $_IPS; done ############################################## while command do command(s) done ############################################## #!/usr/bin/bash clear; echo -n "whic rock group is my fovarite?" read _ANSWER while [[ $_ANSWER != @(kiss||Kiss) ]] do echo -n "No, try again: " read _ANSWER done echo "You got it !!!" exit ############################################## #!/bin/bash echo -n "whic rock group is my fovarite?" read _ANSWER until [[ $_ANSWER == @(kiss||Kiss) ]] do echo -n "No, try again: " read _ANSWER done echo "You got it !!!" exit ############################################## select var in wordlist do command(s) done ############################################## #!/usr/bin/bash PS3="Select a day (1-4): " select i in mon tue wed exit do case $i in mon) echo "Monday";; tue) echo "Tuesday";; wed) echo "Wednesday";; exit) exit;; esac done ############################################## #!/usr/bin/bash PS3="Select a program to run: " select _PROGRAM in 'ls -aih' uptime last who exit do $_PROGRAM done ############################################## case variable in value1) command(s) ;; value2) command(s) ;; value3) command(s) ;; value4) command(s) ;; value*) command(s) ;; esac ############################################## :- if its not have a value, one time only set substitution := if its not have a value, permenantly set avalue :? if its not have a value, print the error message. if its have then print it. :+ if its have a value, one time only set substitution ############################################## String Test [ string1 = string2 ] st1 is the same st2 [ string1 == string2 ] same as above [ string1 != string2 ] st1 is NOT the same st2 [ string1 ] st1 is NOT NULL - simply tests to see if string exists [ -n string1 ] test to see if string length is NON-zero [ -z string1 ] test to see if string length is IS zero [ -l string1 ] test to see what the length of the st1 is in characters Logical String Test [ string1 -a string2 ] test to see if BOTH st1 AND st2 are true [ string1 -o string2 ] test to see if EITHER st1 OR st2 are true [ ! string1 ] does NOT match st1 Logical Compound Testing [[ pattern1 && pattern2 ]] BOTH ptrn1 AND ptrn2 are true [[ pattern1 || pattern2 ]] EITHER ptrn1 OR ptrn2 is true [[ ! pattern1 ]] does NOT match ptrn1 Integer Test [ int1 -eq int2 ] int1 is EQUAL to int2 -ne NOT equal -gt greater than -lt less than -ge greater equal -le less equal Binary Operators for Files [ file1 -nt file2 ] true if file1 is newer than file2 (per the modification date) [ file1 -ot file2 ] true if file1 is older than file2 (per the modification date) [ file1 -ef file2 ] true if file1 and file2 have the same device or inode number (hard link) File Testing Operators TEST | TEST is TRUE if... ---------------------------------- -b filename the file is a BLOCK file -x filename the file is an EXECUTABLE file -c filename the file is a CHARACTER file -w filename the file is a WRITEABLE file -d filename the file is a DIRECTORY file -u filename the file has the SET-USER-ID bit on -e filename the file exist -t filename the fd (file descriptor) is opened on a terminal -f filename the file is a REGULAR file and it exists -s filename the file is a NON-ZERO in size -S filename the file is a SOKET file -g filename the file has the SET-GROUP-ID is set -G filename the file exists AND is owned by the effective GROUP ID -r filename the file is READABLE -k filename the file has the STICKY BIT is set -l filename the file is a SYMBOLINK LINK -p filename the file is a NAMED PIPE -O filename the file exists AND is owned by the effective USER ID ############################################## Expect spawn expect send interact ############################################## A; B Run A and then B, regardless of success of A A && B Run B if A succeeded A || B Run B if A failed A & Run A in background. A & B & Run A in background, then run B in background (regardless of success) (A; B) Run A and then B, regardless of A stop.