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

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
The Bourne Shell 吳 瑞 麟 National Cheng Kung University, Computer and Network Center.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Shell Script Examples.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Shell Control Statements and More
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Sed. Class Issues vSphere Issues – root only until lab 3.
Linux+ Guide to Linux Certification, Second Edition
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Awk 2 – more awk. AWK INVOCATION AND OPERATION the "-F" option allows changing Awk's "field separator" character. Awk regards each line of input data.
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
Bash Shell Scripting 10 Second Guide.
Arun Vishwanathan Nevis Networks Pvt. Ltd.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Lecture 9 Shell Programming – Command substitution
Shell Script Assignment 1.
Pepper (Help from Dr. Robert Siegfried)
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Linux Shell Script Programming
CSC 352– Unix Programming, Fall, 2011
Chapter 5 Bourne Shells Scripts
Essential Shell Programming
Introduction to Bash Programming, part 3
Review.
Presentation transcript:

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

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:  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

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

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 " ; 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 fi 2. 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 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

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

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...." ; 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 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

Example for “break” !/bin/bash # set an infinite while loop while true ; do read -p "Enter number ( to exit ) : " n # break while loop if input is [ $n -eq ] && { 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/bash for var1 in do for var2 in do if [ $var1 -eq 2 -a $var2 -eq 0 ] then break 2 else echo "$var1 $var2" fi done

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

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} ’] … …

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

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

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 4 th 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:

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