More Shell Programming

Slides:



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

More Shell Programming Software Tools. Slide 2 Keyword Shell Variables l The shell sets keyword shell variables. You can use (and change) them. HOME The.
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.
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
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
Bourne Shell1-1 Borne Shell  Background m Early Unix shell that was written by Steve Bourne of AT&T Bell Lab. m Basic shell provided with many commercial.
UNIX Utilities Software Tools. Slide 2 Getting Started on UNIX The machines in CS Lab2 are named csl2wk01 through csl2wk41. csl2wk01 means “CSLab2, workstation#1”
Perl Basics Software Tools. Slide 2 Control Flow l Perl has several control flow statements: n if n while n for n unless n until n do while n do until.
More Shell Programming Learning Objectives: 1. To learn the usage of environment (shell) variables in shell programming 2. To understand the handling of.
Perl Basics Learning Objectives: 1. To understand the commands available for control flow in Perl 2. To learn some useful operators in Perl.
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.
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.
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
LINUX Shell Scripting Advanced Issues
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where.
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.
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.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
Shell Programming. The UNIX Shell  The UNIX shell listens to what you type and executes commands at your request. User command: lpr file UNIX Shell UNIX.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Control Statements and More
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
More Shell Programming. Slide 2 Control Flow  The shell allows several control flow statements:  if  while  for.
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]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
#!/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.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Computer Programming Batch & Shell Programming 9 th Lecture 김현철 컴퓨터공학부 서울대학교 2009 년 가을학기.
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
The UNIX Shell Learning Objectives:
CSC 352– Unix Programming, Fall 2012
Shell Programming (ch 10)
Writing Shell Scripts ─ part 3
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
Linux Shell Script Programming
Shell Programming.
Presented by, Mr. Satish Pise
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Chapter 3 The UNIX Shells
Perl Control Flow Learning Objectives:
Introduction to Bash Programming, part 3
Presentation transcript:

More Shell Programming

The shell allows several control flow statements: if while for

if The if statement works mostly as expected: $ whoami clinton $ cat test7 #!/bin/sh user=`whoami` if [ $user = "clinton" ] then echo "Hi Bill!" fi $ test7 Hi Bill! However, the spaces before and after the square brackets [ ] are required.

if … then … else The if then else statement is similar: $ cat test7 #!/bin/sh user=`whoami` if [ $user = "clinton" ] then echo "Hi Bill!" else echo "Hi $user!" fi $ test7 Hi horner!

if … elif … else You can also handle a list of cases: $ cat test8 #!/bin/sh users=`who | wc -l` if [ $users -ge 4 ] then echo "Heavy load" elif [ $users -gt 1 ] echo "Medium load" else echo "Just me!" fi $ test8 Heavy load!

Boolean Expressions Relational operators: File operators: -eq, -ne, -gt, -ge, -lt, -le File operators: -f file True if file exists and is not a directory -d file True if file exists and is a directory -s file True if file exists and has a size > 0 String operators: -z string True if the length of string is zero -n string True if the length of string is nonzero s1 = s2 True if s1 and s2 are the same s1 != s2 True if s1 and s2 are different s1 True if s1 is not the null string

File Operator Example $ cat test9 #!/bin/sh if [ -f letter1 ] then echo "We have found the evidence!" cat letter1 else echo "Keep looking!" fi $ test9 We have found the evidence! Ms. Lewinski: It is getting late. Please order some pizza and stop by my office. We'll tidy up a few more things before calling it a night. Thanks! Bill

while (1) The while statement loops indefinitely, while the condition is true, such as a user-controlled condition: $ cat test11 #!/bin/sh resp="no" while [ $resp != "yes" ] do echo "Wakeup [yes/no]?" read resp done $ test11 Wakeup [yes/no]? no y yes $

while (2) while can also do normal incrementing loops: $ cat fac #!/bin/sh echo "Enter number: " read n fac=1 i=1 while [ $i -le $n ] do fac=`expr $fac \* $i` i=`expr $i + 1` done echo "The factorial of $n is $fac" $ fac Enter number: 5 The factorial of 5 is 120

break The break command works like in C++, breaking out of the innermost loop : $ cat test12 #!/bin/sh while [ 1 ] do echo "Wakeup [yes/no]?" read resp if [ $resp = "yes" ] then break; fi done $ test12 Wakeup [yes/no]? no y Wakeup [yrs/no]? yes $

Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where the shell looks for executables USER Your login name SHELL The name of the shell you are running PWD The current working directory PRINTER Can be loaded with your default printer

Keyword Example $ cat env #!/bin/sh echo "Hi $USER!" echo "Your home is: $HOME" echo "Your path is: $PATH" echo "Your current directory is: $PWD" echo "Your shell is: $SHELL" echo "Your printer is: $PRINTER" $ env Hi horner! Your home is: /homes/horner Your path is:/usr/bin:.:.:/homes/horner/Unix/bin:... Your current directory is: /homes/horner/111 Your shell is: /bin/csh Your printer is: csl3

Readonly Shell Variables $0 is the name the user typed to invoke the shell script: $ cat print1 #!/bin/sh echo "This script is called $0" $ print1 This script is called print1 $ ./print1 This script is called ./print1 $ ~/111/print1 This script is called /homes/horner/111/print1

Command Line Arguments – (1) The command line arguments that you call a script with are stored in variables $1, $2, ..., $9. $ cat args1 #!/bin/sh echo "The args are $1 $2 $3 $4 $5 $6 $7 $8 $9" $ args1 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The args are a1 a2 a3 a4 a5 a6 a7 a8 a9 With more than 9 arguments, they are still stored, but they have to be moved using the shift command before they can be accessed.

Command Line Arguments – (2) Example: How to write a command to swap two files? $ cat swap #!/bin/sh mv $1 /tmp/$1 mv $2 $1 mv /tmp/$1 $2 $ cat it1 contents of file1 $ cat it2 contents of file2 $ swap it1 it2 $

Command Line Arguments – (3) $* lists all the command line args: $ cat args2 #!/bin/sh echo "The args are $*" $ args2 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The args are a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 $# contains the number of args: $ cat args3 echo "The number of args is $#" $ args3 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The number of args is 10

Command Handling Argument - shift (1) The shift command promotes each command line argument by one (e.g., the value in $2 moves to $1, $3 moves to $2, etc.) $ cat shiftargs #!/bin/sh echo "The args are 0 = $0, 1 = $1, 2 = $2" shift $ shiftargs arg1 arg2 arg3 The args are 0 = shiftarg, 1 = arg1, 2 = arg2 The args are 0 = shiftarg, 1 = arg2, 2 = arg3 The args are 0 = shiftarg, 1 = arg3, 2 = The previous $1 becomes inaccessible

Command Handling Argument - shift (2) Example: How to write a general version of the swap command for two or more files? swap f1 f2 f3 ... fn_1 fn f1 <--- f2 f2 <--- f3 f3 <--- f4 ... fn_1 <--- fn fn <--- f1

shift Example (1) $ cat swap #!/bin/sh orig1=$1 mv $1 /tmp/$1 while [ $2 ] do mv $2 $1 shift done mv /tmp/$orig1 $1 $ cat it1 it2 it3 contents of file1 contents of file2 contents of file3 $ swap it1 it2 it3 1st Sample Run

shift Example (2) $ swap it1 it2 it3 $ cat it1 it2 it3 2nd Sample Run contents of file3 contents of file1 contents of file2 $ swap it1 it2 $ cat it1 it2 2nd Sample Run 3rd Sample Run 4th Sample run

Command Handling Argument - set The set command sets the args: $ cat set1 #!/bin/sh set yat yih saam echo "One is $1, two is $2, three is $3" $ set1 One is yat, two is yih, three is saam The set command is useful for moving the output of command substitution into the args: $ date Thu Feb 25 17:06:27 HKT 1999 $ cat day set `date` echo "Today is $3 $2 $6" $ day Today is 25 Feb 1999

Special variable - $$ (1) $$ is the process ID (PID) of the current process (the shell script PID, or the shell PID if interactive). $ cat pid #!/bin/sh echo $$ $ pid 1154 1156 1157 $ echo $$ 892 $ ps PID TTY TIME CMD 892 pts/0 0:01 csh

Special variable - $$ (2) It can be used for temporary file names: $ cat swap #!/bin/sh file=/tmp/tmp$$ mv $1 $file mv $2 $1 mv $file $1 $ cat it1 it2 contents of file1 contents of file2 $ swap it1 it2 $ cat it1 $

for The for statement executes a loop once for each of a list of possibilities: $ cat printall #!/bin/sh for file in * do if [ -f $file ] then echo "Print $file [y/n]? " read resp if [ $resp = "y" ] lpr –Pcll2a $file fi done $ printall Print it1 [y/n]? y Print it2 [y/n]? n

Looping using for The in clause of the for statement accepts as many parameters as you wish in many forms: $ for i in 1 2 3 ; do echo $i ; done 1 2 3 $ for pid in `ps -a | tail +2 | cut -c1-6 | sort` > do > kill -9 $pid > done kill: permission denied (you will then be logout!)

For example (2) for index in 1 2 3 4 5 6 7 8 9 10 do if [ $index –le 3 ]; then echo continue continue fi echo $index if [ $index –ge 8 ]; then echo “break” break done

Catch a signal: builtin trap Syntax: trap ‘commands’ signal-numbers Shell executes the commands when it catches one of the signals Then resumes executing the script where it left off. Just capture the signal, not doing anything with it trap ‘ ‘ signal_number Often used to clean up temp files Signals SIGHUP 1 disconnect line SIGINT 2 control-c SIGKILL 9 kill with -9 SIGTERM 15 default kill SIGSTP 24 control-z …

Example [ruihong@dafinn ~/cs3451]$ cat inter #!/bin/sh trap 'echo PROGRAM INTERRUPTED' 2 while true do echo "programming running." sleep 1 done