Shell Script Programming: Somsak Ketkeaw
Shell Script Programming: Somsak Ketkeaw
programming
Somsak Ketkeaw
www.aoddy.com
August 9 10, 2008
Outline
Very simple scripts.
Hello world.
All about redirection.
How to redirect between standard output with standard error.
Pipe command.
Variables
How to create conditionals programming.
How to use loop for, while and until.
How to create function.
String comparison.
Arithmetic operation.
Arithmetic relational operations.
How to debugging in shell script.
Very simple script.
Hello world!!!
#!/bin/bash
echo “Hello world. I'm mornor.”
All about standard
Input & Output
There are 3 IO streams.
1. Standard input.
2. Standard output.
3. Standard error.
Standard input.
Standard input (stdin) normally
comes from your keyboard.
UNIX commands that need
input will usually read stdin.
The shell can redirect stdin from a file.
http://www.linuxdevcenter.com/pub/a/linux/lpt/13_01.html
Standard input. (2)
Example :
$ mail joan < myfile
Standard output.
Standard output, sometimes
abbreviated stdout, refers to the
standardized streams of data
that are produced by command
line programs.
http://www.linfo.org/standard_output.html
How to redirect stdout to file.
$ grep passwd /etc/* > stdout.txt
Standard error.
Standard error, abbreviated stderr,
is the destination of error messages
from command line.
http://www.linfo.org/standard_error.html
How to redirect stderr to file.
$ grep passwd /etc/* 2> stderr.txt
How to redirect stdout&stderr to file.
$ grep passwd /etc/* &> stderr.txt
When you redirect stdout&stderr to file?
When you want to monitor your script.
Pipes command.
Pipes let you use the output of a
program as the input of another one .
Pipes command example.
$ cat /etc/passwd | wc l
$ cat /etc/passwd | grep home
Variables
Variables.
There are no data types.
Can contain a number, a character
and a string of characters.
No need to declare.
Example.
#!/bin/bash
STR="Hello World!, mornor.";
NUM=1;
echo No : $NUM = $STR
Example : Backup script.
#!/bin/bash
OF=/tmp/mybackup$(date +%Y%m%d).tgz
tar cvfz $OF /home/[Your home directory]/
Local variables.
Local variables can only be used
in a function.
Example.
#!/bin/bash
HELLO=Hello
function hello {
local HELLO=World
echo $HELLO
}
echo $HELLO
hello
echo $HELLO
How to create conditionals
programming.
IF syntax
if [expression1];
then
code if 'expression1' is true.
elif [expression2];
then
code if 'expression2' is true.
else
code if 'expression1&2' are false
fi
Example
#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
Loops for, while and until
For loop syntax
for var in list
do
commands;
done
For loop example
#!/bin/sh
logfile="/var/log/messages";
dir_out="OUTPUTSL28";
mkdir p $dir_out;
for mon in Sun Mon Tue Wed Thu Fri Sat
do
grep $mon $logfile > $dir_out/message.$mon
done
While loop syntax
while condition_is_true
do
commands
done
While loop example
#!/bin/bash
COUNTER=0
while [ $COUNTER lt 10 ]; do
echo The counter is $COUNTER
((COUNTER=COUNTER+1))
done
Until loop syntax
until false
do
commands
done
Until loop example
#!/bin/bash
COUNTER=20
until [ $COUNTER lt 10 ];
do
echo COUNTER $COUNTER
let COUNTER=1
done
String comparison operators.
1. s1 = s2 > s1 matches s2
2. s1 != s2 > s1 does not match s2
3. n s1 > s1 is not null
(contains one or more characters)
4. z s1 > s1 is null
Example
#!/bin/bash if [ n "$str3" ]
str1="ABC" then
str2="ABC" echo String3 is not null: $str3 : check by n;
str3="a"; else
echo String3 is null check by n;
if [ "$str1" = "$str2" ] fi
then
echo String1 is match String2. if [ z "$str3" ]
else then
echo String1 is not match String2. echo String3 is null check by z;
fi else
echo String3 is not null: $str3 : check by z;
fi
Arithmetic operators
+ = plus
= minus
* = multiply
/ = divide
% = (remainder)
Example
#!/bin/bash result=`expr $num1 / $num2`;
num1=9 echo Result of divide = $result;
num2=4
((result=num1+num2)); ((result=num1%num2));
echo Result of plus = $result; echo Result of mod = $result;
((result=num1num2));
echo Result of minus = $result;
((result=num1*num2));
echo Result of multiple = $result;
Arithmetic relational operators
lt (<) less than
gt (>) greater than
le (<=) less than equal
ge (>=) greater than equal
eq (==) equal
ne (!=) not equal
Example
#!/bin/bash
num1=9
num2=4
if [ $num1 gt $num2 ]
then
echo "Num1($num1) greater than Num2($num2)";
else
echo "Num1($num1) less than Num2($num2)";
fi
Useful command : awk
Example # 1
$ ls l | awk '{print $8}';
Example # 2
$ cat /etc/passwd | awk F':' '{print $1":"$NF}'
Example # 3
$ du sb * | awk '{print $1}'
$ du sb * | awk '{print $2}'
Useful command : sort
Example # 1
$ du sb * | awk '{print $1}' | sort n
Example # 2
$ du sb * | awk '{print $1}' | sort nr
Exercise # 1
I want to know files which are in /etc/ directory have
file size more than 4049 bytes, please show output
like :
File : acpi has 4096 bytes
File : adduser.conf has 2975 bytes
File : alternatives has 12288 bytes
How to create
function.
Function syntax
function nameoffunction {
command;
command;
}
Example
#!/bin/bash
function BOK_RUK {
echo I love $1;
}
# call function BOK_RUK
BOK_RUK "you, na krub";
How to debugging
your script.
How to debugging.
Put option x after '#!/bin/bash'
at top of your script.
Such as :
#!/bin/bash x
Example
#!/bin/bash x
function BOK_RUK {
str=$1
echo I love $str;
}
# call function BOK_RUK
BOK_RUK "you, na krub";
Reading user input with read
You may want to prompt the user
for some input.
Syntax :
read VARIABLE
Example
#!/bin/bash
function BOK_RUK {
str=$1
echo I love $str;
}
echo n "Please enter the name : ";
read name;
# call function BOK_RUK
BOK_RUK "${name}, na krub";
Exercise.
I have a file(name.txt) which stores many names and mobile
numbers of customers such as
Doddy:01234567
Moddy:98765432
I would like to create a message so as to send email to all
persons in this file.
Hi Doddy,
Please update your mobile number (01234567). I cannot
call to you.
Regards,
Aoddy.
The end.