1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.

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.
2006-Jan-231 Shell Scripts Jacob Morzinski
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.
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.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
LINUX Shell Scripting Advanced Issues
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.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
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
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.
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)
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
(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.
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 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.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell Control Statements and More
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
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]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
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 년 가을학기.
Shell Control Structures
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Lecture 9 Shell Programming – Command substitution
Unix Scripting Session 4 March 27, 2008.
Pepper (Help from Dr. Robert Siegfried)
CHAPTER 8 (skip , ) CHAPTER 10
Shell Control Structures
Shell Control Structures
Essential Shell Programming
Introduction to Bash Programming, part 3
Review.
Presentation transcript:

1 Operating Systems Lecture 3 Shell Scripts

2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex. ls prog?.c ls p*.c ls ???.* F ls prog[123].c ls [m-s]* ls [1-5A-Z] F ls [!ABC] [!a-z0-47] F Other characters: ~; ~user; ~-; ~+

3 More unix commands n grep (egrep) n find n head n tail n cut n paste n sed n tr n Tee n Jobs n fg %1 n !! !-n !cmd ^str1^str2^

4 Shell scripts n Typing on command line F One time task n Shell scripts F Executable ASCII file containing shell command F Can run like a program F Deal with complex task F Repeat it later

5 Shell script cont’d n A shell script, in its most basic form, is simply a collection of operating system commands put into a text file in the order they are needed for execution. F #!/bin/bash F rm -f /tmp/listing.tmp> /dev/null 2>&1 F touch /tmp/listing.tmp F ls -l [a-z]*.doc | sort> /tmp/listing.tmp F lpr -Ppostscript_1 /tmp/listing.tmp F rm -f /tmp/listing.tmp

6 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a comment. Comments run from # to the end of the line. 3. All shell scripts begin with the interpreter you want to use: #!/bin/bash Example: #!/bin/bash who | grep ycjiang exit 0

7 Running a shell script To run a UNIX script: 1)Type the script into a file. 2)Change the file permission to executable. 3)Execute it (by typing the filename at the prompt).

8 Shell Variables Shell variables are stored as strings: Example: #!/bin/bash x=1# Note: No spaces in assignment. # If space after x, thinks x is a command echo The value of x is $x # $x prints the value of variable x echo The home directory is $HOME echo The current shell is $SHELL (Note: to debug, use -x: sh -x scriptFileName This will list the commands as they are executed.)

9 Using Quotes Single quote: Groups together characters until end quote. $ is not processed. Example: #!/bin/sh grep Constance John /etc/passwd #Tries to open John as a file grep 'Constance John' /etc/passwd #Searches for Constance John #in passwd file x=1 echo $x#echos 1 echo '$x'#echos $x exit 0

10 Double Quotes Double quotes act like single quotes, except the $ is processed: #!/bin/bash x=1 echo $x#echos the value of x echo "$x"#echos the value of x address="College of the Engineering and Science" echo $address#echos College of the Engineering and Science echo "$address"#ditto exit 0

11 More Quotes Backslash (\): Places a single quote around a character: \> is the same as '>' Back quote (`): Tells shell to execute the enclosed command and insert the output here: #!/bin/bash echo There are `who | wc -l` users logged on exit 0 Try these examples out for yourself!

12 Evaluating Expressions Use expr to evaluate arithmetic expressions: Example: #!/bin/bash i=1 i=`expr $i + 1` #use back quote echo $i #prints out 2 exit 0

13 Common Mistakes Mistake 1: Leaving out expr: #!/bin/bash i=1 i=$i+1 echo $i #prints 1+1 exit 0 Mistake 2: Leaving out spaces around + #!/bin/bash i=1 i=`expr $i+1` echo $i #prints out 1+1 exit 0

14 Using Arguments with a Shell script Example command: >myShell arg1 arg2 arg3 arg1 is accessed by $1, arg2 is accessed by $2, etc. Question: What is accessed by $0 ? The number of arguments (not including the command) is given by $# $# for the above command line is 3.

15 example n #!/bin/bash n #source: prnargs n #shell pgm to print out its first 2 args n echo The first argument is: $1 n echo The second argument is: $2 n exit 0

16 To run: n First, make sure it is executable (has execute permissions for user), F /home/ycjiang> prnargs "Hello there" world 4 The first argument is: Hello there 4 The second argument is: world F Note: spacing is correct, it ignores fact that I lined up the 2 args in the echo n --could have used quotes in echo, F echo "The first argument is: " $1 F echo "The second argument is:" $2 F --or could have used echo backslash n -escape TAB char, \t (see man echo) F echo -e "The first argument is:\t" $1 F echo -e "The second argument is:\t" $2

17 The shift command The shift command shifts each variable down 1. $2 becomes $1, etc. $0 is not shifted. It always refers to the command name. Example command line: >cmd x y z Example script: #!/bin/sh echo $# $0 $1 $2 #prints 3 cmd x y shift echo $# $0 $1 $2 #prints 2 cmd y z shift echo $# $0 $1 $2 #prints 1 cmd z exit 0

18 The read command Use the read command to read in from the keyboard: Example: !/bin/bash read x y z echo "x = " $x echo "y = " $y echo "z = " $z read text echo "text = " $text exit 0 If type "I love shell programming!" at first read: $x is "I", $y is "love" and $z is "shell programming!" If type "I love shell programming!" at second read: $text is "I love shell programming" Use < to read in from a file: read x y z < myFile

19 Conditionals Example of a conditional statement: #!/bin/bash read name if [ "$name" = Joe ] then echo It is Joe else echo It is "$name", not Joe fi #fi marks the end of the statement exit 0

20 Integer comparisons -eq (or =)is equal to -gegreater than or equal to -gtgreater than -leless than or equal to -ltless than -nenot equal to Example: if [ num1 -lt num2 ] then... fi

21 for loops Example: #!/bin/bash for i in do for j in do echo $i$j done exit 0 Note: break command exits loop continue loops back to top, skipping the rest of the current iteration.

22 while loops Example: #!/bin/bash i=1 num=5 while [ $i -le $num ] do echo $i >> file_test i=`expr $i + 1` done exit 0 What does this do? Note: while read x loops until zero exit status not returned from read.

23 Defining Functions Example: #!/bin/bash nu() { who | wc -l } echo Number of users is `nu` echo Number of users is now `nu` exit 0

24 Writing Good Shell Scripts 1.Verify that the correct number of arguments is used: #!/bin/bash if [$# -ne 3 ] then echo "only $# parameters entered." echo "Usage: $0 arg1 arg2 arg3" exit 127 fi exit 0 2.Use good design and style (e.g. use functions and comments. 3.Return an exit status: exit 0 #things worked OK exit 127 #non-zero: error occurred. See /usr/include/sysexits.h for sample exit codes.

25 Tutorials on shell scripting n