Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Further Shell Scripting Michael Griffiths Corporate Information and Computing Services The University of Sheffield
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.
Lab 8 Shell Script Reference:
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
An Introduction to Unix Shell Scripting
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
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
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
CS465 - UNIX The Bourne Shell.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
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.
LINUX programming UNIT-2 1. INDEX UNIT-IV PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Working with Bourne shell L1 1-2.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
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.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
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]
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Bash Shell Scripting 10 Second Guide.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Agenda Control Flow Statements Purpose test statement
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.
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Linux Shell Script Programming
Presented by, Mr. Satish Pise
CSC 352– Unix Programming, Fall, 2011
Chapter 5 Bourne Shells Scripts
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Shell Programming

Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and so on.  The shell is a UNIX program that interprets the commands you enter from the keyboard  UNIX provides several shells, including the Bourne shell, the Korn shell, and the C shell  Steve Bourne at AT&T Bell Laboratories developed the Bourne shell as the first UNIX command processor

Introducing UNIX Shells  The Korn shell includes many extensions, such as a history feature that lets you use a keyboard shortcut to retrieve commands you previously entered  The C shell is designed for C programmer’s use  Linux uses the freeware Bash shell as its default command interpreter

Shell Scripts  Definition :  “An executable file consisting of a set of commands to be executed is called a shell script or shell program”.  A “.sh” extension is used for shell scripts.  It is not mandatory to use such extension for shell script, but it is used to differentiate them from other files.

Shell Scripts  Shell scripts run in interpretive mode.  This means that shell scripts are executed one line at a time.  They are not compile to separate executable file as “C” programs.

When to use Shell Scripts ?  Shell scripts should be used on following occasions :  While customizing a work environment. For example, to display current date and time on log-on.  Automating daily tasks. For example, to take backup on regular intervals.  Automating repetitive tasks. For example, compiling and executing programs.  Executing important system procedures. For example, shutdown, formatting a disk.

When not to use Shell Scripts ?  Shell scripts should not be used on following occasions :  When the task to be performed is too complex.  When the task requires high degree of efficiency.  When the task requires variety of software tools.

Creating & Executing Shell Scripts  To create and execute the shell script, following the steps given below :  Open any editor like as vi, pico, emacs or any other one.  Write script instruction in a file and save the file with “.sh’ extension example, suppose the file is given name “example.sh.”  By default, any newly created file does not contain execute permissions.

Creating & Executing Shell Scripts  So allocate execution permission to the owner of a file.  For this purpose chmod command can be use as – $ chmod u+x example.sh  Invoking Scripts with sh :To use the sh command. $ sh example.sh  Making Files Executable : To make the script file an executable file.

sh Command  The sh command invokes a new copy of the shell. You can run your script files using sh command.

sh Command Options of sh Command -n : Read commands, but does not execute them. -v : Prints the input to the shell as the shell reads it. -x : Prints command lines and their arguments as they are executed. This option is used mostly for debugging.

Accepting Inputs  There are two methods available to accept input from users.  These methods are as follows:  Interactive Method  Non-Interactive Method

Interactive Method  It uses read command to read input values in variables.  The syntax of read command is: Read variable_name  When script encounters this command, it pauses at that point to take input from the keyboard.  Any value entered by the user is stored in the variable specified with the read command.

Non-Interactive Method  It is uses command line arguments.  User can provide arguments as the command prompt while executing the shell script.  Such arguments can be provided along with the script name.  For example, following copyfile program takes two argument as file names $ copyfile source.txt dest.txt

Non-Interactive Method  When arguments are specified with a shell script, they are assigned to certain special variable called positional parameters.  The first argument is read by the shell into the parameter $1 and the second in $2 and so on.  In our example, $1 contains source.txt while $2 contains dest.txt.

Non-Interactive Method  Along with these positional parameters, there are a few other special parameters used by the shell.  They are given as below;  $0 : holds the command name (or program name) itself.  $* : holds the complete set of positional parameters as a single string. In our example, it contains ‘source.txt, dest.txt’

Non-Interactive Method  $# : holds the number of arguments specified in command line. In our example, it holds 2.  $? : holds the exit status of last command. (if exit status is 0, successful, else failure.)  $$ : holds the process ID(PID) of the current shell.  $! : holds the PID of the last background process.

Evaluate Expressions using ‘test’ & […]  The test & […] commands are used to evaluate conditional expressions with integers, strings and file attributes.  The syntax of test commands is: test expression  The syntax of […] commands is: [expression] Here, expression is the conditions to be evaluated. This expression must be enclosed by white spaces before and after it.

Evaluate Expressions using ‘test’ & […]  White space must also separate the expression argument the expression arguments and operators.  It the expression evaluates to true, then a zero exit status is returned, otherwise the expression evaluates to false and a non- zero exit status is returned.  The various operators that can be used with both of these commands.

Integer Related Operator ExpressionResult exp1 –eq exp2True if exp1 is equal to exp2 exp1 –ne exp2True if exp1 is not equal to exp2 exp1 –le exp2True if exp1 is less than or equal to exp2 exp1 –lt exp2True if exp1 is less than exp2 exp1 –ge exp2True if exp1 is greater than or equal to exp2 exp1 –gt exp2True if exp1 is greater exp2

File Related Operator ExpressionResult -b fileTrue if file exists & is a block special file -c fileTrue if file exists & is a character special file -d fileTrue if file exists & is a directory -f fileTrue if file exists & is a regular file -r fileTrue if file exists & is readable -w fileTrue if file exists & is a writable

File Related Operator ExpressionResult -x fileTrue if file exists & is executable -k fileTrue if file exists & is sticky bit is set -L fileTrue if file exists & is a symbolic link -s fileTrue if file exists & its size is grater than zero f1 -nt f2True if f1 is newer than f2 (supported by korn and bash shell only) f1 -ot f2True if f1 is newer than f2 (supported by korn and bash shell only) f1 -ef f2True if f1 is newer than f2 (supported by korn and bash shell only)

String Related Operator ExpressionResult -n stringTrue if length of string is not zero -z stringTrue if length of string is zero StringTrue if string of string is not set to null string1 = string2True if string1 is equal to string2

String Related test Operator ExpressionResult string1 != string2True if string1 is not equal to string2 string = patternTrue if string is matches pattern string != patternTrue if string does not match pattern

Other Operator ExpressionResult !expTrue if the given expression is false \(exp\)True if exp is true; used to group expressions exp1 –a exp2True if both exp1 and exp2 evaluate to true (AND operation) exp1 –o exp2True if either exp1 and exp2 evaluate to true (OR operation)

Branching  The shell support various programming constructs to provide branching functionality.  This include simple if, if..else, else-if ladder and case statement.

Simple if  Syntax : if command1 then commands fi  Usage : execute commands if command1 remains successful.  Example : if cp source.txt dest.txt then echo “file copied successfully….” fi

if…else  Syntax : if command1 then command2 else command3 fi  Usage : execute command2 if command1 remains successful, otherwise execute command3.

if…else  Example : if cp source.txt dest.txt then echo “file copied successfully….” else echo “some problem in copy operation” fi

else…if ladder  Syntax : if command1 then command elif command2 then command elif command-n then commands else commands fi

else…if ladder  Usage : whenever any command remains successful either with if or with elif, execute the commands associated with that block.  When all the ‘n’ commands remained unsuccessful, then final commands associated with else will be executed.

Case  Syntax : case expression in pattern1) command1;; pattern2) command2;; pattern3) command3;; … patternn) commandn;; esac

Case  Usage : execute commands associated with the pattern that matches expression.  Multiple patterns can be given but must be separated with a | character.  Each command list is terminated with a pair of semicolons, and the entire construct is closed with esac (reverse of case)

Case  Example : echo “Do u like shell-programming? (y/n)” read answer case $answer in y|Y) echo “wow…! It’s good!”;; n|N) echo “U should like it. It’s important for exams”;; esac

expr Command  Syntax : expr expression  Usage : evaluates the given mathematical expression.  Options : expression can be of the form : expr1 operator expr2  Here, operator can be +(plus), -(minus), \*(multiplication), /(division) or %(modulo).  Also, operator must be enclosed by white space on either side of it.  Examples : - expr expr $x \* $y

Looping  The shell supports various programming constructs to provide looping functionality.  This includes :  while  until  for

While  Syntax : while command1 do commands done  Usage : execute commands while command1 remains successful.

While  Example : echo “enter no:” read no i=1 while [ $i –le $no ] do echo “$i” i=`expr $i + 1`#not ‘ ‘ but ` ` done

until  Syntax : until command1 do commands done  Usage : execute commands while command1 remains UN-successful.

until  Example : echo “enter no:” read no i=1 until [ $i –gt $no ] do echo “$i’ i=`expr $i + 1` done

for  Syntax : for variable in list of argument do commands done  Usage : here, list contains white space separated words, such as- test1 test2test3test4  Commands are executed until list is exhausted.  Each time single word is assigned to variable during loop execution.

for  Example : for i in do echo “$i’ done