shell scripting notes

some of these notes may have been created from other online sources.

they are just my home schooling notes.

 

$?   = exit code of process
$!   = pid of current process
$*   = arguments passed to script
$@   = arguments passed to script *read more
unset  = unsets a variable
$1   = name of program
$2   = argument passed to program
$3   = argument passed to program (all the way up to 9)
$$   = parent pid
$RANDOM  = random number
$REPLY   = value of “read” when no name is gievn to read
$SECONDS = amount of seconds the script has been running
$HOME    = users home directory
$IFS     = internal field seperator. change this to change how the shell reads seperators.
(example IFS=”:”  will make it treat : as the separator)
$PATH    = a users path variable
$TMOUT   = timeout for shell commands (how long read will wait before timing out)
$TMPDIR  = temp directory (/tmp by default)
$UID     = user id of current user

? matches any single character
* matches any sequence of characters

Using the {} you can match multiple items.
ls -ld /etc/rc{0,1,2,3,4,5,6}.d

the above matches all rc.d scripts.
rc0.d
rc1.d
etc

using ranges ([ ]):

$ ls a[a-z]c.txt
abc.txt

matches all files with a to z as a middle character
page 69 for more info (expert recipes)

$ ls [aeiou]*
abc abcdefghijk abc.php abc.txt alphabet
abcdef abcdef.odt abctxt abc.TXT

Loops:

if [ condition ]
then
statement(s)
fi

if [ condition ];then statement(s);fi

if [ condition ]
then
statements for when the condition is true
else
statements for when the condition is false
fi

#!/bin/bash
OS=`uname -s`
if [ “$OS” = “FreeBSD” ]; then
echo “This Is FreeBSD”
else
if [ “$OS” = “CYGWIN_NT-5.1” ]; then
echo “This is Cygwin”

for loops:
#!/bin/bash
for fruit in $*
do
echo “I really like ${fruit}s”
done

for i in /etc/*.conf
do
echo $i
done

#!/bin/bash
for file in /tmp/*
do
if [ -f ${file} ]
then
if [ -e “${file}.bak” ]
then
echo “Error: Skipping ${file}.bak as it already exists”
else
echo “Backing up $file”
cp “${file}” “${file}.bak”
fi
fi
done

for ((i=1,j=100; i<=10; i++,j-=2))
do
printf “i=%03d j=%03dn” $i $j
done

WHILE LOOPS:

i=1
while [ “$i” -lt “100” ]
do
echo “i is $i”
i=`expr $i * 2`
done

UNTIL LOOPS:
until [ $a -gt 12 ] || [ $b -lt 100 ]
do
echo “a is ${a}; b is ${b}.”
let a=$a+1
let b=$b-10
done

-f FILE
FILE exists and is a regular file

-r test to see if a file exists and is readable.

-n STRING
the length of STRING is nonzero

STRING equivalent to -n STRING

-z STRING
the length of STRING is zero

STRING1 = STRING2
the strings are equal

STRING1 != STRING2
the strings are not equal

INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2

INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2

INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2

INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2

INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2

INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2

-e FILE                    = FILE exists

! EXPRESSION               = EXPRESSION is false

EXPRESSION1 -a EXPRESSION2 =  both EXPRESSION1 and EXPRESSION2 are true

EXPRESSION1 -o EXPRESSION2 = either EXPRESSION1 or EXPRESSION2 is true

Within brace expansion, the special characters {, }, and , can be escaped with a backslash

The let builtin command evaluates each supplied word from left to right as an arithmetic expression and returns an exit code according to the truth value of the rightmost expression

the arithmetic compound command should generally be preferred over let.
arithmetic compound is (( x = 2))

The arguments to let are therefore subject to all the same expansions and substitutions as any other simple command – requiring proper quoting and escaping – whereas the contents of (( aren’t subject to word-splitting or pathname expansion

[] is an alias for the test command
read      = reads data from the console (“read a” reads data ffrom the console into variable a)
sleep 5   = sleep for 5 seconds
:         = true
break     = breaks out of a loop
break 3   = breaks out 3 levels deep in nested looping. (can be any number)
continue  = jumps straight to the test which controls the loop
continue 3 = continues 3 levels deep in nested looping.
||        = or
&&        = and
The export command makes available variables to all child processes of the running script or shell.
One important use of the export command is in startup files, to initialize and make accessible environmental variables to subsequent user processes.

using an array:
options+=( “option 1” “option 2” “quit” )

page 133
————————————————————————–
CASE SWITCHING:

case EXPRESSION in CASE1) COMMAND-LIST;; CASE2) COMMAND-LIST;; … CASEN) COMMAND-LIST;; esac

quit=0
while read command data
do
case $command in
echo)
echo “Found an echo command: $data”
;;
upper)
echo -en “Found an upper command: “
echo $data | tr ‘[:lower:]’ ‘[:upper:]’
;;
lower)
echo -en “Found a lower command: “
echo $data | tr ‘[:upper:]’ ‘[:lower:]’
;;
quit)
echo “Quitting as requested.”
quit=1
break
;;
*)
echo “Read $command which is not valid input.”
echo “Valid commands are echo, upper, lower, or quit.”
;;https://www.suse.com/
esac
done

 

Leave a Reply