Date post: | 20-Jan-2018 |
Category: |
Documents |
Upload: | elmer-horn |
View: | 212 times |
Download: | 0 times |
byDr P.Padmanabham
Professor (CSE)&Director Bharat Institute
of Engineering &Technology Hyderabad
Mobile 9866245898 [email protected]
Shell Programming• A group of commands that have to be executed
regularly are stored in a file and executed as shell script or a shell program. No compilation.
• The above file should have execute permission• Example #! /bin/bash #example.sh: sample program echo -n “Today is: ” ; date exit In addition shell programming has other features
like variables control structures etc.
Shell Variables
Shell variables are available to all shell programs and commands as environment variables. Some important variables are: HOME The default argument (home directory) for
the cd command. PATH The search path for commands PS1 Primary prompt string, by default ``$ ''. PS2 Secondary prompt string, by default ``> ''. IFS Internal field separators, normally space, tab,
and new-line. SHELLShell name that is invoked TERM Stores the name of the terminal
User Defined & Special Shell Variables
• User can define a shell variable:name=“Peter” (No gap on either side of = symbol) export nameecho $name• Following are some of the shell variables, which can
be used in a shell program The parameters are automatically set by the shell:
• $n n th positional parameter ($0=command name, $1 = 1st argument, ...)
• $* Holds the entire list of arguments, excluding the command name, as a string
Special Shell Variables
$@ Same as $* except when quoted as “$@” is “$1”, “$2”, .. where as “$*” is “$1 $2 $3 ...”.
$# Total number of positional parameters excluding the program name ($0)
$? Result of the last executed command. $$ Process ID of the current shell. $! Process ID of the last background
command, invoked by & operator. exit , exit 0 ,exit 1
Using test or [] test evaluates to true (0) or false (1)
Eample if test $x –gt $y ; then echo “ x > y” where x and y are user defined shell variables.
shorthand notation for test is [] Example: test $x –ne $y is same as [ $x -
ne $y ] test can be used to test file attributes also Example [ -f test.c ] || echo “ test.c not a
file”
Some file testing options for test
• -d FileName - FileName is a directory.• -f FileName - FileName is a regular file.• -h FileName - FileName is a symbolic link.• -k FileName - FileName's sticky bit is set• -p FileName - FileName is a named pipe (FIFO).
contd....
-r FileName - FileName is readable by the current process.
-s FileName - FileName has a size greater than 0.-e FileName- FileName exists
Some numeric testing options for test
• -ne (not equal)• -gt (greater than)• -ge (greater or equal)• -lt (less than)• -le (less or equal)
Example for “test”
# delete lines that contain pattren
[ $# -nq 2 ] && { echo " usage: $0 <pattren> <file-name>" ; exit 1 ; }
[ -f "$2" ] && { echo "$2 :" ; sed '/'$1'/d' $2 ; }
if conditional
There are three forms of if conditional:1. If test/command successful then execute commands fi2. If test/command successful then execute commands else execute commands fi
if conditional contd….
3. If test/command successful then execute command elif test/command successful then ......... elif ............ then ......... else .......................fi
Example for “if”# Dr Padmanabham simple if illustrationif [ $# -eq 1 ] then if ls $1 > /dev/null 2> /dev/null; then echo "File:" $1 "exists" else echo "sorry, the file:" $1 "does not exist" fifi
case conditional
The syntax for case conditional case $variable-name in pattern1) Command1 .... commandN ;; pattern2) Command1 … commandN ;; ................... patternN) command1 .... commandN ;; *) command1 .... commandN ;; esac
Example for “case”# Dr Padmanabham case conditional illustration# Dr Padmanabham case conditional illustrationEcho “1.list Files 2. lsit processes 3. show Date 4. show umaskEcho “1.list Files 2. lsit processes 3. show Date 4. show umask 5.show users 6. show Linux version7.disk space 8. exit"5.show users 6. show Linux version7.disk space 8. exit"while :while : dodo echo -n "enter your choice:" echo -n "enter your choice:" read choiceread choice case $choice in case $choice in 1) ls ;; 2) ps ;; 3) date ;; 4) umask ;;1) ls ;; 2) ps ;; 3) date ;; 4) umask ;; 5) who ;; 6) uname -r ;; 7) df -h ;;8) exit ;;5) who ;; 6) uname -r ;; 7) df -h ;;8) exit ;; *) echo "Invalid Option" ;;*) echo "Invalid Option" ;; esacesac donedone
for : looping with a list
The for loop does not support the 3 –part syntax of for loop used in C The for uses list instead.for variable in list do commands done The key words do and done delimit the loop body At every iteration each successive words in the list is assigned to variable and the body is executed. The loop terminates when the words in the list are exhausted.
Example “for loop”# checks if each argument is a directory or file
[ $# -gt 0 ] ||{ echo "usage: $0 <name1> <name2>...." ; exit 1 ; }for file in $* ; do [ -f "$file" ] && { x=$(wc -l < "$file") ; printf " %s: is a file of %d lines \n" $file $x ; } [ -d "$file" ] && echo " $file: is a directory"done
While :looping
The while loop syntax:While condition is true do commands done Like in if you can use any command or test with whileWe can also create an infinite loop by using while : or while true;Using break or break n we can come out of a loop or nested loops
Example for “break”!/bin/bash# set an infinite while loopwhile true ; do read -p "Enter number ( -9999 to exit ) : " n # break while loop if input is -9999 [ $n -eq -9999 ] && { echo "Bye!"; break; } isEvenNo=$(( $n % 2 )) # get modules [ $isEvenNo -eq 0 ] && echo "$n is an even number.“ || echo "$n is an odd number." done
Example for “break 2”#!/bin/bashfor var1 in 1 2 3do for var2 in 4 0 5 6 do if [ $var1 -eq 2 -a $var2 -eq 0 ] then break 2 else echo "$var1 $var2" fi donedone
Example# gets the largest file in the current directory
MSIZE=0
for file in *
do
[ -d $file ] && continue #if a directory continue
[ -r $file ] || continue # if no read permission continue
x=$(wc -c < $file)
echo $x
if [ $x -gt $MSIZE ]
then
MSIZE=$(($x)); MFILE=$file
fi
done
echo " $MSIZE $MFILE“ exit 0
Basics of awkawk reads data from a file or from standard input
(Keyboard or redirected input), and writes data to standard output (screen in general).
awk considers each line as a record.awk processes a file line by line, i.e., processes
records in a sequence.A record consists of fields separated by a delimiter.
The delimiter can be any character (tab, space, colon, semicolon, or any user defined character) and the default is Tab/Space character.
Basics of awk contd…..
Each field is represented by its number preceded by a $
i th field is accessed with $i (field 1 with $1, ….)$0 refers to the whole record. Syntax of awk:
awk [-F “<field separator in>”] ‘<pattern> {<action block>} ’ [‘<pattern> {actions} ’] … <input file 1> <input file 2> …
awk Syntax Pattern acts as a condition based on which action block is
executed. Patterns can be regular expressions, arithmetic relational expressions, string-valued expressions, and arbitrary Boolean combinations of these.
Action block is a sequence of commands separated by semicolon or new line.
File is the input file. awk programs can be written in a file or given directly in the
command line.
A simple example$ awk –F”:” ‘/svln/ {print $1 “ “ $5}’ /etc/passwd
– Prints login name and full name of svln from passwd file. Suppose /etc/passwd file contains the following entries
root:x:0:0::/root:/bin/bash
svln:x:500:500:SVL Narasimham:/home/svln:/bin/bash
guest:x:1000:1000::/home/guest:/bin/bash awk will see this file as follows
Each line is a record, and hence the number of records is the entries in the passwd file.Field Separator is : (-F’:’)Each record contains 7 fields separated by “:” and are denoted by $1 to $7. $0 Represents entire record.
A simple example (cont..)
Awk executable
pattern to search
Action to perform on line
If pattern matches
The file to operate
upon
Field Separator
$ awk –F”:” ‘/svln/ {print $1 “ “ $5}’ /etc/passwd
awk Syntax Awk follows C language .The control structures are similar to C (if, for,
and while)Awk’s variables are stored internally as
strings. x = “1.01” x = x + 1 print x The above will print the value 2.01
Awk syntax contd.
Comparison operators: "==", "<", ">", "<=", ">=", "!=“, "~" and "!~“. (“~” and “!~” operators mean "matches" and "does not match“).
Arithmetic operators: “+", “-", “/", “*“;
“^” is the exponentiation operator.“%” is the modulo operator
awk SyntaxArithmetic operators: “+", “-", “/", “*“, “%”
(Modulo), ^ (Exponent)C operators like “++”, “--”, “+=“, “-=”, “/=“ etc. are
supported.awk language supports associative one-dimensional
arrays for storing strings or numbers. Awk has two sections BEGIN and END which will be
executed only once (before and after the awk command. These are used to initialize variables and use the data at the end of the process respectively.
BEGIN and END in awkBEGIN{ x=0 }# executed before processing the file
/^$/ { x=x+1 } # blank line count
END { print " blank lines :" x} # executed at the
end
Note that BEGIN and END are special patterns.
The actual script:
awk –F”:” ‘ BEGIN {x=0 } /^$/ {x=x+1} END {print
“blank lines:” x}’ test.c
awk Built-in Variables
Some of the important variables are: FS: Input field separator. The value can be a single
character string or a multi character regular expression. –F option sets it.
OFS: Output field separator (default is space) RS: Record separator (default is newline) ARGC: Argument count ARGV: Array of arguments ARGV[i] is i-th
argument
awk Built-in Variables contd.
FILENAME: Name of the file awk is currently reading
FNR: Current record number in the current file. NF: Number of fields in the current record NR: Number of input records awk processed
since the begin of program execution
Awk examples$ awk 'BEGIN { RS = "/" } ; { print $0 }' file1.txt
In this example the RS is modified to “/” from the default \n.
$ awk '$1 ~ /abc/ { print $0 }' file.txt
The pattern will print out all records from file file.txt whose first fields contain the string “abc”.
$ awk '{ print $(2*2) }' file.txt
Prints the 4th field of each record.
awk Example
Finding average, Minimum and maximum of a set of numbers:
Say the data is in a file called data.txt with the following numbers:
20104030
awk example contd.awk ‘{if(min==”") {min=max=$1}; if($1>max) {max=$1}; if($1< min) {min=$1}; total+=$1; count+=1;} END {print total/count, min, max}’ data.txt