Linux Shell Programming

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 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
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
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.
Lecturer: Prof. Andrzej (AJ) Bieszczad
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
1.1 Command Substitution The backquote “`” is different from the single quote “´”. It is used for command substitution: `command`  $ LIST=`ls` $ echo.
Introduction to Shell Script Programming
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.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
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 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.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
#!/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.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
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.
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.
Writing Scripts Hadi Otrok COEN 346.
1.1 The shell of Linux Linux has a variety of different shells:  Bourne shell (sh), C shell (csh), Korn shell (ksh), TC shell (tcsh), Bourne Again shell.
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.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
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]
 Prepared by: Eng. Maryam Adel Abdel-Hady
 Prepared by: Eng. Maryam Adel Abdel-Hady
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
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.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Chapter 14: Writing Shell Scripts
CSC 352– Unix Programming, Fall 2012
CAP652 Lecture - Shell Programming
Shell Scripting March 1st, 2004 Class Meeting 7.
Lecture 9 Shell Programming – Command substitution
Agenda Control Flow Statements Purpose test statement
LING 408/508: Computational Techniques for Linguists
Copyright © – Curt Hill Bash Scripting Fundamentals Copyright © – Curt Hill.
LING 408/508: Computational Techniques for Linguists
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Linux Shell Programming

The shell of Linux Linux has a variety of different shells: Bourne shell (sh), C shell (csh), Korn shell (ksh), TC shell (tcsh), Bourne Again shell (bash). Certainly the most popular shell is “bash”. Bash is the shell that will appear in the GNU operating system. Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use.

Programming or Scripting ? bash is not only an excellent command line shell, but a scripting language in itself. Shell scripting allows us to use the shell's abilities and to automate a lot of tasks that would otherwise require a lot of commands. Difference between programming and scripting languages: Programming languages are generally a lot more powerful and a lot faster than scripting languages. Programming languages generally start from source code and are compiled into an executable. This executable is not easily ported into different operating systems. A scripting language also starts from source code, but is not compiled into an executable. Rather, an interpreter reads the instructions in the source file and executes each instruction. Interpreted programs are generally slower than compiled programs. The main advantage is that you can easily port the source file to any operating system. bash is a scripting language. Other examples of scripting languages are Perl, Lisp, and Tcl.

The first bash program We must know how to use a text editor. There are two major text editors in Linux: vi, emacs (or xemacs). So fire up a text editor; for example: $ vi & and type the following inside it: #!/bin/bash echo “Hello World” The first line tells Linux to use the bash interpreter to run this script. We call it hello.sh. Then, make the script executable: $ chmod 700 hello.sh $ ls –l -rwx------ hello.sh

To execute the program: The first bash program To execute the program: $ hello.sh -bash: hello.sh: command not found The home directory (where the command hello.sh is located) is not in the variable PATH echo $PATH :bin:/usr/bin:… We must specify the path of hello.sh $/home/srinaldi/Scripts/hello.sh $./hello.sh

The second bash program We write a program that copies all files into a directory, and then deletes the directory along with its contents. This can be done with the following commands: $ mkdir trash $ cp * trash $ rm -rf trash

Variables We can use variables as in any programming languages. Their values are always stored as strings, but there are mathematical operators in the shell language that will convert variables to numbers for calculations. We have no need to declare a variable, just assigning a value to its reference will create it. Example #!/bin/bash STR=“Hello World!” echo $STR Line 2 creates a variable called STR and assigns the string "Hello World!" to it. Then the value of this variable is retrieved by putting the '$' in at the beginning.

Single and Double Quote When assigning character data containing spaces or special characters, the data must be enclosed in either single or double quotes. Using double quotes (partial quoting) to show a string of characters will allow any variables in the quotes to be resolved $ var=“test string” $ newvar=“Value of var is $var” $ echo $newvar Value of var is test string Using single quotes (full quoting) to show a string of characters will not allow variable resolution $ var=’test string’ $ newvar=’Value of var is $var’ Value of var is $var

Read command The read command allows you to prompt for input and store it in a variable. Example (read.sh) #!/bin/bash echo -n “Enter name of file to delete: ” read file echo “Type 'y' to remove it, 'n' to change your mind ... ” rm -i $file echo "That was YOUR decision!" Line 3 creates a variable called file and assigns the input from keyboard to it. Then the value of this variable is retrieved by putting the '$' in at its beginning.

Read command Options Example read –s (does not echo input) read –nN (accepts only N characters of input) read –p “message” (prompts message) read –tT (accepts input for T seconds) Example $ read –s –n1 -p “Yes (Y) or not (N)?” answer Yes (Y) or not (N) ? Y $ echo $answer Y

Command Substitution The backquote “`” is different from the single quote “´”. It is used for command substitution: `command` $ LIST=`ls` $ echo $LIST hello.sh read.sh PS1=“`pwd`>” /home/rinaldi/didattica/> We can perform the command substitution by means of $(command) $ LIST=$(ls) rm $( find / -name “*.tmp” ) ls $( pwd ) ls $( echo /bin )

Arithmetic Operators + plus - minus * multiplication / division ** exponentiation % modulo Example $ a=(5+2)*3 $ echo $a $ b=2*3 $ echo $a+$b

Arithmetic Evaluation The let statement can be used to do mathematical functions: $ let X=10+2*7 $ echo $X 24 $ let Y=X+2*4 $ echo $Y 32 An arithmetic expression can be evaluated by $[expression] or $((expression)) echo $((123+20)) 143 VALORE=$[123+20] echo $[123*$VALORE] 17589 $ echo $[2*3] $ echo $[8%3] Not necessary to use $X to refer to the value of X

Arithmetic Evaluation Example (operations.sh) #!/bin/bash echo -n “Enter the first number: ”; read x echo -n “Enter the second number: ”; read y add=$(($x + $y)) sub=$(($x - $y)) mul=$(($x * $y)) div=$(($x / $y)) mod=$(($x % $y)) # print out the answers: echo “Sum: $add” echo “Difference: $sub” echo “Product: $mul” echo “Quotient: $div” echo “Remainder: $mod”

Conditional Statements Conditionals let we decide whether to perform an action or not, this decision is taken by evaluating an expression. The most basic form is: if [expression]; then statements elif [expression]; else fi the elif (else if) and else sections are optional

Expressions An expression can be: String comparison, Numeric comparison, File operators and Logical operators and it is represented by [expression]: String Comparisons: = compare if two strings are equal != compare if two strings are not equal -n evaluate if string length is greater than zero -z evaluate if string length is equal to zero Examples: [ s1 = s2 ] (true if s1 same as s2, else false) [ s1 != s2 ] (true if s1 not same as s2, else false) [ s1 ] (true if s1 is not empty, else false) [ -n s1 ] (true if s1 has a length greater then 0, else false) [ -z s2 ] (true if s2 has a length of 0, otherwise false)

Expressions Number Comparisons: Examples: -eq compare if two numbers are equal -ge compare if one number is greater than or equal to a number -le compare if one number is less than or equal to a number -ne compare if two numbers are not equal -gt compare if one number is greater than another number -lt compare if one number is less than another number Examples: [ n1 -eq n2 ] (true if n1 same as n2, else false) [ n1 -ge n2 ] (true if n1greater then or equal to n2, else false) [ n1 -le n2 ] (true if n1 less then or equal to n2, else false) [ n1 -ne n2 ] (true if n1 is not same as n2, else false) [ n1 -gt n2 ] (true if n1 greater then n2, else false) [ n1 -lt n2 ] (true if n1 less then n2, else false)

Expressions #!/bin/bash # if0.sh echo -n “Enter your login name: " read name if [ “$name” = “$USER” ]; then echo “Hello, $name. How are you today ?” else echo “You are not $USER, so who are you ?” fi #!/bin/bash # if1.sh echo -n “Enter a number 1 < x < 10: " read num if [ “$num” -lt 10 ]; then if [ “$num” -gt 1 ]; then echo “$num*$num=$(($num*$num))” echo “Wrong insertion !”

Expressions Files operators: Examples: -d check if path given is a directory -f check if path given is a file -s check if path given is a symbolic link -e check if file name exists -s check if a file has a length greater than 0 -r check if read permission is set for file or directory -w check if write permission is set for a file or directory -x check if execute permission is set for a file or directory Examples: [ -d fname ] (true if fname is a directory, otherwise false) [ -f fname ] (true if fname is a file, otherwise false) [ -e fname ] (true if fname exists, otherwise false) [ -s fname ] (true if fname length is greater then 0, else false) [ -r fname ] (true if fname has the read permission, else false) [ -w fname ] (true if fname has the write permission, else false) [ -x fname ] (true if fname has the execute permission, else false)

Example #!/bin/bash if [ -f /etc/fstab ]; then cp /etc/fstab . echo “Done.” else echo “This file does not exist.” exit 1 fi

Expressions Logical operators: ! negate (NOT) a logical expression -a logically AND two logical expressions -o logically OR two logical expressions #!/bin/bash # if3.sh echo -n “Enter a number 1 < x < 10:” read num if [ “$num” -gt 1 –a “$num” -lt 10 ]; then echo “$num*$num=$(($num*$num))” else echo “Wrong insertion !” fi

Expressions Logical operators: && logically AND two logical expressions || logically OR two logical expressions #!/bin/bash # if4.sh echo -n "Enter a number 1 < x < 10: " read num if [ “$number” -gt 1 ] && [ “$number” -lt 10 ]; then echo “$num*$num=$(($num*$num))” else echo “Wrong insertion !” fi

Case Statement Used to execute statements based on specific values. Often used in place of an if statement if there are a large number of conditions. Value used can be an expression each set of statements must be ended by a pair of semicolons; a *) is used to accept any value not matched with list of values case $var in val1) statements;; val2) *) esac

Example #!/bin/bash ( case.sh ) echo -n “Enter a number 1 < x < 10: ” read x case $x in 1) echo “Value of x is 1.”;; 2) echo “Value of x is 2.”;; 3) echo “Value of x is 3.”;; 4) echo “Value of x is 4.”;; 5) echo “Value of x is 5.”;; 6) echo “Value of x is 6.”;; 7) echo “Value of x is 7.”;; 8) echo “Value of x is 8.”;; 9) echo “Value of x is 9.”;; 0 | 10) echo “wrong number.”;; *) echo “Unrecognized value.”;; esac

Iteration Statements The for structure is used when you are looping through a range of variables. for var in list do statements done statements are executed with var set to each value in the list. #!/bin/bash let sum=0 for num in 1 2 3 4 5 let “sum = $sum + $num” echo $sum

Iteration Statements: <list> #!/bin/bash lista=“antonio michele paolo luca” for x in $lista do echo “The value of variable x is: $x” sleep 1 done # The value of variable x is antonio # The value of variable x is michele # The value of variable x is paolo # The value of variable x is luca

Iteration Statements: <list> #!/bin/bash for x in * do ls -l “$x” sleep 1 done # Lists all files in current directory for x in /bin # Lists all files in /bin

Operations on vabiables ……. let “index += 5” #increment index by 5 …… += #increment variable -= # decrement variable *= # multiply variable /= # divide variable

Using Arrays with Loops In the bash shell, we may use arrays. The simplest way to create one is using one of the two subscripts: pet[0]=dog pet[1]=cat pet[2]=fish pet[4]=apple pet=( dog cat fish apple ) We may have up to 1024 elements. To extract a value, type ${arrayname[i]} $ echo ${pet[0]} dog $ echo ${pet[2]} fish

Arrays To extract all the elements, use an asterisk as: echo ${arraynames[*]} To see how many elements are in the array: echo ${#arraynames[*]} We can combine arrays with loops using a for loop: for x in ${arrayname[*]} do echo ${arrayname[$x]} done

A C-like for loop An alternative form of the for structure is for (( EXPR1 ; EXPR2 ; EXPR3 )) do statements done First, the arithmetic expression EXPR1 is evaluated. EXPR2 is then evaluated repeatedly until it evaluates to 0. Each time EXPR2 is evaluates to a non-zero value, statements are executed and EXPR3 is evaluated. $ cat for2.sh #!/bin/bash echo –n “Enter a number: ”; read x let sum=0 for (( i=1 ; $i<$x ; i=$i+1 )) ; do let “sum = $sum + $i” echo “the sum of the first $x numbers is: $sum”

Debugging Bash provides two options which will give useful information for debugging -v : displays each line of the script as typed before execution -x : displays each line before execution (abbreviated) Usage: #!/bin/bash –v, or #!/bin/bash –x $ cat for3.sh #!/bin/bash –x echo –n “Enter a number: ”; read x let sum=0 for (( i=1 ; $i<$x ; i=$i+1 )) ; do let “sum = $sum + $i” done echo “the sum of the first $x numbers is: $sum”

While Statements The while structure is a looping structure. Used to execute a set of commands while a specified condition is true. The loop terminates as soon as the condition becomes false. If condition never becomes false, loop will never exit. while expression do statements done $ cat while.sh ( while.sh ) #!/bin/bash echo –n “Enter a number: ”; read x let sum=0; let i=1 while [ $i –le $x ]; do let “sum = $sum + $i” i=$i+1 echo “the sum of the first $x numbers is: $sum”

Continue Statements The continue command causes a jump to the next iteration of the loop, skipping all the remaining commands in that particular loop cycle. #!/bin/bash LIMIT=19 echo echo “Printing Numbers 1 through 20 (but not 3 and 11)” a=0 while [ $a -le “$LIMIT” ]; do a=$(($a+1)) if [ “$a” -eq 3 ] || [ “$a” -eq 11 ] then continue fi echo -n “$a ” done

Break Statements The break command terminates the loop (breaks out of it). #!/bin/bash LIMIT=19 echo “Printing Numbers 1 through 20, but something happens after 2 … ” a=0 while [ $a -le “$LIMIT” ]; do a=$(($a+1)) if [ “$a” -gt 2 ] then break fi echo -n “$a ” done echo; echo; echo exit 0

Until Statements The until structure is very similar to the while structure. The until structure loops until the condition is true. So basically it is “until this condition is true, do this”. until [expression] do statements done $ cat countdown.sh #!/bin/bash #countdown.sh #echo “Enter a number: ”; read x echo ; echo Count Down until [ “$x” -le 0 ]; do echo $x x=$(($x –1)) sleep 1 echo ; echo GO !

$ echo ${#st} Manipulating Strings Bash supports a surprising number of string manipulation operations. Unfortunately, these tools lack a unified focus. ${#string} gives the string length ${string:position} extracts sub-string from $string at $position ${string:position:length} Extracts $length characters of sub-string from $string at $position Example $ st=0123456789 $ echo ${#st} 10 $ echo ${st:6} 6789 $ echo ${st:6:2} 67

Advanced operations on strings ${string/substring/replacement},strips the first match of substring in string with replacement. pippo=abbcaabccbcabcdbcdabab echo ${pippo/ca/11} # abb11abccbcabcdbcdabab #replace the first match echo ${pippo//ca/11} # abb11abccb11bcdbcdabab # replaces all matches

Functions Functions make scripts easier to maintain. Basically it breaks up the program into smaller pieces. A function performs an action defined by you, and it can return a value if you wish. #!/bin/bash hello() { echo “You are in function hello()” } echo “Calling function hello()…” hello echo “You are now out of function hello()” In the above, we called the hello() function by name by using the line: hello . When this line is executed, bash searches the script for the line hello(). It finds it right at the top, and executes its contents.