UNIX SHELL SCRIPTING
Chapter -1
1) Date 2) who 3)who am I 4)echo
2) Ls 5)cat 6)wc –l 7)wc –c 8)wc –w
3) Cp 4)mv 5)rm
#echo a? #echo a* #echo a?b #echo ??*
Chapter -2
ls [a-z]*[!0-9] List files that begin with a lowercase letter and don't end with a digit
See the differenc using
#ls [a-z] #ls [a-z]? #ls [a-z]*
#sort #wc –l
Standard error
$ ls n* 2> errors
$ cat errors
n* not found
Chapter -3
Chapter-4
#w | grep 10.2.2.11 | cut -c10-15 ( cut command cut the charater from file
#cut –c1 #cut -c10-15
#who | cut -c1-8 Extract the first 8 characters
$ who | cut -c1-8 | sort Extract the first 8 characters and sort it
$who | cut -c10-16 Extract the characters 10 to 16
$who | cut -c1-8,18- extract the characters 1 to 8 + show all
character from 18 to onwards
$cut -d: -f1 /etc/passwd cut the first field from file /etc/passwd
delimiter is set to :
$ cut -d: -f1,6 /etc/passwd Extract fields 1 and 6
#cat /etc/passwd | cut –d: -f1 > username
#cat /etc/passwd | cut –d: -f3 > id
#paste username id > generalfile
#paste –d’+’ username id -d is the delimiter
$sed 's/Unix/UNIX/' filename Substitute Unix with UNIX
$sed -n '/root/p' username Print the line which has root word in
it
$sed -n '12p' username Print the 12 no of line from file
username
$sed '1,2d' intro Delete lines 1 and 2
$ sed '/UNIX/d' intro Delete all lines containing UNIX
Table 4.2. sed Examples
sed Command Description
sed '5d' Delete line 5
sed '/[Tt]est/d' Delete all lines containing Test or test
sed -n '20,25p' text Print only lines 20 through 25 from text
sed Change unix to UNIX wherever it appears in the first 10
'1,10s/unix/UNIX/g'
intro
lines of intro
sed '/jan/s/-1/-5/' Change the first -1 to -5 on all lines containing jan
sed 's/...//' data Delete the first three characters from each line of data
sed 's/...$//' data Delete the last 3 characters from each line of data
sed -n 'l' text Print all lines from text, showing nonprinting characters as
\nn (where nn is the octal value of the character), and tab
characters as \t
#tr e x < intro translate character e to x from file intro
#cut -d: -f1,6 /etc/passwd cut filed1 and field 6 delimiter is set
#cut -d: -f1,6 /etc/passwd | tr : ' '
cut filed 1 and filed6 delimiter is set and translate : to space
Table 4.3. Octal Values of Some ASCII Characters
Character Octal Value
Bell 7
Table 4.3. Octal Values of Some ASCII Characters
Character Octal Value
Backspace 10
Tab 11
Newline 12
Linefeed 12
Formfeed 14
Carriage Return 15
Escape 33
In the following example, tr takes the output from date and translates all spaces into
newline characters. The net result is that each field of output from date appears on a
different line.
$ date | tr ' ' '\12' Translate spaces to newlines
Sun
Jul From output of date command
Translate space to \12 it is
28 linefeed means space
converted to enter so output
19:13:46 is as shown
here
EDT
2002
$ tr '[a-z]' '[A-Z]' < intro
Translate smaill [a-z] character of file intro in to capital character
$ tr '[A-Z]' '[a-z]' < intro
#tr -s ':' '\11'
Translate with sqeez means multiple of single character in to one space
tab which has : before
$ tr –s ' ' ' ' < lotsaspaces
Translate Multiple occurs of space in to single space from file
lotspacess
$tr -d ' ' < intro
translate delte the space character from file intro
Table 4.4. tr Examples
tr Command Description
tr 'X' 'x' Translate all capital X's to small x's
tr '()' '{}' Translate all open parens to open braces, all closed parens to
closed braces
tr '[a-z]' '[A-Z]' Translate all lowercase letters to uppercase
tr '[A-Z]' '[N-ZA- Translate uppercase letters A–M to N–Z, and N–Z to A–M,
M]'
respectively
tr ' ' ' ' Translate all tabs (character in first pair of quotes) to spaces
tr -s ' ' ' ' Translate multiple spaces to single spaces
tr -d '\14' Delete all formfeed (octal 14) characters
tr -d '[0-9]' Delete all digits
Some grep Examples
Command Prints
grep '[A-Z]' list Lines from list containing a capital letter
grep '[0-9]' data Lines from data containing a number
Some grep Examples
Command Prints
grep '[A-Z]...[0- Lines from list containing five-character patterns that start
9]' list
with a capital letter and end with a digit
grep '\.pic$' Lines from filelist that end in .pic
filelist
#grep -v 'UNIX' intro Print all lines that don't contain UNIX
#grep 'Move_history' *.c Find Move_history in all C source files
The -l option to grep gives you just a list of files that contain the specified pattern, not
the matching lines from the files:
$ grep -l 'Move_history' *.c List the files that contain Move_history
$ grep -n 'Move_history' testch.c Precede matches with line numbers
The -u option tells sort to eliminate duplicate lines from the output.
$ sort -u names options tell to eliminates the duplicate name if any
$ sort –r name options tell to revers the process of sort
$ sort /etc/passwd | cut -f1 -d: | uniq -d Find duplicates
$sort names | uniq –c Count line occurrences
count=10
echo $((count=count-1))
echo $((i+1))
echo $((i = (i + 10) * j))
i=$(( i * 5 ))
result=$(( i >= 0 && i <= 100 ))
$ i=$(( 100 * 200 / 10 ))
$ j=$(( i < 1000 )) If i is < 1000, set j = 0; otherwise 1
$ echo $i $j
Chapter-6 Shell scripting
$ namelist=$(cat names)
$ echo "$names"
$ mail $(cat names) < memo
$ filename=/users/steve/memos
$ firstchar=$(echo $filename | cut -c1)
$ echo $firstchar
$ file=exec.o
$ lastchar=$(echo $file | sed 's/.*\(.\)$/\1/')
$ echo $lastchar
$ filename=/users/steve/memos
$ firstchar=$(echo $filename | cut -c1)
$ filename=$(echo $filename | tr "$firstchar" "^") translate / to ^
$ echo $filename
expr 1 + 2 expr 10+20/2 expr "17 * 6"
expr 17 \* 6
$ i=1
$ i=$(expr $i + 1) Add 1 to i
$ echo $i
string1 = string2 string1 is identical to string2.
string1 != string2 string1 is not identical to string2.
string string is not null.
-n string string is not null (and string must be seen by test).
-z string string is null (and string must be seen by test).
test Integer Operators
Operator Returns TRUE (exit status of 0) if
int1 -eq int2 int1 is equal to int2.
int1 -ge int2 int1 is greater than or equal to int2.
int1 -gt int2 int1 is greater than int2.
int1 -le int2 int1 is less than or equal to int2.
int1 -lt int2 int1 is less than int2.
int1 -ne int2 int1 is not equal to int2.
Table 8.3. Commonly Used test File Operators
Operator Returns TRUE (exit status of 0) if
-d file file is a directory.
-e file file exists.
-f file file is an ordinary file.
-r file file is readable by the process.
-s file file has nonzero length.
-w file file is writable by the process.
-x file file is executable.
Table 8.3. Commonly Used test File Operators
Operator Returns TRUE (exit status of 0) if
-L file file is a symbolic link.
("$count" -ge 0) -a ("$count" -lt 10)
"$a" -eq 0 -o "$b" -eq 2 -a "$c" -eq 10
if commandt
then
command
else
command
fi
if command1
then
command
else
if command2
then
command
else
command
...
fi
...
fi
-------------------------------
Program to print a greeting
hour=$(date | cut -c12-13)
if [ "$hour" -ge 0 -a "$hour" -le 11 ]
then
echo "Good morning"
else
if [ "$hour" -ge 12 -a "$hour" -le 17 ]
then
echo "Good afternoon"
else
echo "Good evening"
fi
fi
if commandl
then
command
elif command2
then
command
else
command
fi
# Translate a digit to English
if [ "$#" -ne 1 ]
then
echo "Usage: number digit"
exit 1
fi
case "$1"
in
0) echo zero;;
1) echo one;;
2) echo two;;
3) echo three;;
4) echo four;;
5) echo five;;
6) echo six;;
7) echo seven;;
8) echo eight;;
9) echo nine;;
esac