Presentation is loading. Please wait.

Presentation is loading. Please wait.

By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898

Similar presentations


Presentation on theme: "By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898"— Presentation transcript:

1 by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898 ppadmanabham@yahoo.com

2 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.

3 Shell Variables  Shell variables are available to all shell programs and commands as environment variables. Some important variables are:  HOMEThe default argument (home directory) for the cd command.  PATHThe search path for commands  PS1Primary prompt string, by default ``$ ''.  PS2Secondary prompt string, by default ``> ''.  IFSInternal field separators, normally space, tab, and new-line.  SHELLShell name that is invoked  TERMStores the name of the terminal

4 User Defined & Special Shell Variables User can define a shell variable: name=“Peter” (No gap on either side of = symbol) export name echo $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

5 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

6 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”

7 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).

8 contd.... -r FileName - FileName is readable by the current process. -s FileName - FileName has a size greater than 0. -e FileName- FileName exists

9 Some numeric testing options for test -ne (not equal) -gt (greater than) -ge (greater or equal) -lt (less than) -le (less or equal)

10 Example for “test” # delete lines that contain pattren [ $# -nq 2 ] && { echo " usage: $0 " ; exit 1 ; } [ -f "$2" ] && { echo "$2 :" ; sed '/'$1'/d' $2 ; }

11 if conditional There are three forms of if conditional: 1. If test/command successful then execute commands fi 2. If test/command successful then execute commands else execute commands fi

12 if conditional contd…. 3. If test/command successful then execute command elif test/command successful then......... elif............ then......... else....................... fi

13 Example for “if” # Dr Padmanabham simple if illustration if [ $# -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" fi

14 case conditional The syntax for case conditional case $variable-name in pattern1) Command1.... commandN ;; pattern2) Command1 … commandN ;;................... patternN) command1.... commandN ;; *) command1.... commandN ;; esac

15 Example for “case” # Dr Padmanabham case conditional illustration Echo “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 : do do echo -n "enter your choice:" echo -n "enter your choice:" read choice read 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" ;; esac esac done done

16 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.

17 Example “for loop” # checks if each argument is a directory or file [ $# -gt 0 ] ||{ echo "usage: $0...." ; 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

18 While :looping The while loop syntax: While condition is true do commands done Like in if you can use any command or test with while We 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

19 Example for “break” !/bin/bash # set an infinite while loop while 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

20 Example for “break 2” #!/bin/bash for var1 in 1 2 3 do for var2 in 4 0 5 6 do if [ $var1 -eq 2 -a $var2 -eq 0 ] then break 2 else echo "$var1 $var2" fi done

21 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

22 Basics of awk  awk 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.

23 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 “ ”] ‘ { } ’ [‘ {actions} ’] … …

24 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.

25 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.

26 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

27 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

28 Awk syntax contd.  Comparison operators: "==", " ", " =", "!=“, "~" and "!~“. (“~” and “!~” operators mean "matches" and "does not match“).  Arithmetic operators: “+", “-", “/", “*“;  “^” is the exponentiation operator.  “%” is the modulo operator

29 awk Syntax  Arithmetic 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.

30 BEGIN and END in awk BEGIN{ 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

31 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

32 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

33 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 4 th field of each record.

34 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: 20 10 40 30

35 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

36


Download ppt "By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile 9866245898"

Similar presentations


Ads by Google