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.

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.
Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
More about Shells1-1 More about Shell  Shells (sh, csh, ksh) are m Command interpreters Process the commands you enter m High-level programming languages.
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
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.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
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.
An Introduction to Unix Shell Scripting
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 8 Shell.
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The 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
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
CS465 - UNIX The Bourne Shell.
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)
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
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.
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.
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,
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.
#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
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.
Shell Control Structures
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Pepper (Help from Dr. Robert Siegfried)
LING 408/508: Computational Techniques for Linguists
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
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.
Presentation transcript:

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 Scripting

© Copyright IBM Corporation 2008 Unit Objectives After completing this unit, you should be able to: Invoke shell scripts in three separate ways and explain the difference Pass positional parameters to shell scripts and use them within scripts Implement interactive shell scripts Use conditional execution and loops Perform simple arithmetic

© Copyright IBM Corporation 2008 $ pwd $ date $ ls -l $ cat script1 pwd date ls -l $ What Is a Shell Script? A shell script is a collection of commands stored in a text file.

© Copyright IBM Corporation bash bash date Start of subshell Running a command from script1 $ cat script1 date $ bash script1 Invoking Shell Scripts (1 of 3) The script does not have to be marked executable, but must be readable. bash invokes a script in a child shell.

© Copyright IBM Corporation bash bash date Start of subshell Running a command from script1 $ chmod 755./script1 $./script1 Invoking Shell Scripts (2 of 3) Use the chmod command to make the script executable. Then run the script as if it were a command. The script is run in a child shell.

© Copyright IBM Corporation 2008 date script1 -bash $. script1 $ source script1 The date command is invoked by your current shell Invoking Shell Scripts (3 of 3) Use the. (dot) or source command to execute the script in your current shell environment. Scripts executed with the dot command can change your current environment.

© Copyright IBM Corporation 2008 Invoking Shell Scripts in Another Shell To make sure the shell script always runs in the shell where it was intended for (sh, bash, csh), use the following on the first line of your script: #!/bin/bash The script now always runs in bash - even if the users default shell is something else. With this, the. (dot) or source invocation is no longer possible.

© Copyright IBM Corporation 2008 Typical Shell Script Contents Handling of shell script arguments Complex Redirection Conditional Execution Repeated Execution User Interfacing Arithmetic

© Copyright IBM Corporation 2008 $ cat ascript #!/bin/bash echo First parameter: $1 echo Second parameter: $2 echo Number of parameters: $# $ ascript ant bee First parameter: ant Second parameter: bee Number of parameters: 2 Shell Script Arguments Parameters can be passed to shell scripts as arguments on the command line. These arguments are stored in special shell variables: –$1, $2, $3... refers to each of the arguments is "$1" "$2" "$3" –$* is "$1 $2 $3" –$# is the number of parameters

© Copyright IBM Corporation 2008 To redirect fixed text into a command, use << END. To avoid large argument lists, use xargs. –Avoids large argument lists by running the command multiple times, if necessary $ cat cities Atlanta Chicago END $ rm *.txt -bash: /bin/rm: Argument list too long $ find. -name "*.txt" | xargs rm $ Complex Redirection

© Copyright IBM Corporation 2008 command(s) this command(s) that command(s) if true false do returns Conditional Execution The return code from a command or group of commands can be used to determine whether to start the next command.

© Copyright IBM Corporation 2008 The test command allows you to test for a given condition. Syntax: test expression...or: [ expression ] Expressions to test file status: -f file is an ordinary file -d file is a directory -r file is readable -w file is writable -x file is executable -s file has non-zero length $ test -f myfile.txt $ echo $? 0 The test Command (1 of 2)

© Copyright IBM Corporation 2008 The test Command (2 of 2) String tests: -n string is not empty -z string is empty == strings are equal != strings are not equal Arithmetic tests: -eq equals -ne not equal -lt less than -le less than or equal -gt greater than -ge greater than or equal

© Copyright IBM Corporation 2008 $ [ -f testfile ] && rm testfile $ [ -f lockfile ] || touch lockfile $ [ "$TERM" = "xterm" ] && echo This is no tty $ cat doesnotexist 2>/dev/null || echo \ > "Oh boy, this file does not exist." The && and || commands && and II (2 vertical bars) can be used to conditionally execute a single command: command1 && command2 if (command1 successful) then do (command2) command1 || command2 if (command1 not successful) then do (command2)

© Copyright IBM Corporation 2008 The structure of the basic if statement is: if statement is true then carry out this set of actions else carry out this set of actions fi $ cat myscript if [ "$MY_VALUE" -eq 10 ] then echo MY_VALUE contains the value 10 else echo MY_VALUE is not 10 fi $ The if Command

© Copyright IBM Corporation 2008 command 1 command 2 command 3 if is true, repeat this set of commands if is false, leave this set of commands Command Repetition A loop is a set of commands that is executed over and over: –Until or while a certain condition is true –Or for each item from a list

© Copyright IBM Corporation 2008 The syntax of the while command: while expression is true do command(s) done $ cat myloop while true do echo "It is now $(date)" echo "There are `ps aux | wc -l` processes" sleep 600 done $ Note that the expression true is always true! The while Command

© Copyright IBM Corporation 2008 The structure of the for loop is: for identifier in list do command(s) to be executed on $identifier done $ cat my_forloop for file in /tmp/mine_* do cp $file /other_dir/$file done $ The for Command

© Copyright IBM Corporation 2008 $ cat make_backup while [ $# -gt 0 ] do cp $1 $1.bak shift done $ list of arguments:arg1arg2arg3arg4arg5(count) Variables:$1$2$3$4$5$# = 5 after first loop:del.$1$2$3$4$# = 4 after second loop:del.del.$1$2$3$# = 3 Shifting Shell Script Arguments If you expect a large number of shell arguments (for example, filenames), use the shift command in a while loop to handle them all.

© Copyright IBM Corporation 2008 $ cat delfile #!/bin/bash #Usage delfile echo Please enter the file name: read name if [ -f $name ] then rm $name else echo $name is not an ordinary file - echo so it is not removed fi User Interaction: The read Command The read command reads one line from stdin and assigns the values read to a variable.

© Copyright IBM Corporation 2008 If your shell does not support $(()) or let, then use the expr command for integer arithmetic. –Not a shell built-in, so about 10 times slower Same operators as let Beware of the shell metacharacters *, ( and )! $ echo `expr 3 + 5` 8 $ expr 3 * ( ) bash: syntax error near unexpected token `(´ $ expr 3 \* \( \) 24 The expr Command

© Copyright IBM Corporation 2008 $ let x=2+3 $ echo $x 5 $ echo $(( 2+3 )) 5 $ let x=3*(3+5) $ echo $x 24 $ let x=3*3+5 echo $x 14 $ x=$(( 3 * ( ) )) Arithmetic Using let The bash shell can perform simple arithmetic on integers using the builtin let command or the $(( expr )) notation. Operators: *, /, +, -, %

© Copyright IBM Corporation 2008 Qualified pathname PATH variable Function Built-in command Alias Reserved Word Command Search Order

© Copyright IBM Corporation 2008 Checkpoint (1 of 2) 1.What will the following piece of code do? TERMTYPE=$TERM if [ -n "$TERMTYPE" ] then if [ -f /home/tux1/custom_script ] then /home/tux1/custom_script else echo No custom script available! fi else echo You don't have a TERM variable set! fi

© Copyright IBM Corporation 2008 Checkpoint (2 of 2) 2.Write a script that will multiply any two numbers together.

© Copyright IBM Corporation 2008 Unit Summary Positional Parameters are used to pass to scripts the values of variable. To test for a particular condition, the test command can be used. This feature is frequently coupled with the if statement to control the flow of a program and allow for conditional execution within scripts. The read command can be used to implement interactive scripts. The while and for commands are used to create loops in a script.