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.

Slides:



Advertisements
Similar presentations
Shell Scripting Printing; Loops and Logic printf: formatted print printf is a more standardized method to print text to the screen. It can be used instead.
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.
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.
Perl Basics Learning Objectives: 1. To understand the commands available for control flow in Perl 2. To learn some useful operators in Perl.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
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 Control Structures CSE 2031 Fall August 2015.
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.
Chapter 6: Shell Programming
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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 Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
CS465 - UNIX The Bourne Shell.
#!/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.
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.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
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 Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Shell Control Statements and More
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
More Shell Programming. Slide 2 Control Flow  The shell allows several control flow statements:  if  while  for.
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.
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.
Linux Administration Working with the BASH 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 년 가을학기.
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.
Shell Control Structures
Department of Computer Engineering
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CSC 352– Unix Programming, Fall 2012
Lecture 9 Shell Programming – Command substitution
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Shell Programming.
Presented by, Mr. Satish Pise
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
More Shell Programming
Introduction to Perl Learning Objectives:
Perl Control Flow Learning Objectives:
Introduction to Bash Programming, part 3
Presentation transcript:

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 language, to write shell scripts (your own custom commands)

Slide 3 Shell Scripts l A shell script is just a file containing shell commands, but with a few extras: n The first line of a shell script should be a comment of the following form: #!/bin/sh for a Bourne shell script. Bourne shell scripts are the most common, since C Shell scripts have buggy features. n A shell script must be readable and executable. chmod u+rx scriptname n As with any command, a shell script has to be “in your path” to be executed. –If “.” is not in your PATH, you must specify “./scriptname ” instead of just “ scriptname ”

Slide 4 Shell Script Example l Here is a “hello world” shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh # comment lines start with the # character echo " Hello world " $ hello Hello world $ The echo command functions like a print command in shell scripts.

Slide 5 Shell Variables l The user variable name can be any sequence of letters, digits, and the underscore character, but the first character must be a letter. l To assign a value to a variable: number=25 name= " Bill Gates " l There cannot be any space before or after the “=“ l Internally, all values are stored as strings.

Slide 6 Shell Variables l To use a variable, precede the name with a “$”: $ cat test1 #!/bin/sh number=25 name= " Bill Gates " echo " $number $name " $ test1 25 Bill Gates $

Slide 7 User Input Use the read command to get and store input from the user. $ cat test2 #!/bin/sh echo "Enter name: " read name echo "How many girlfriends do you have? " read number echo "$name has $number girlfriends!" $ test2 Enter name: Bill Gates How many girlfriends do you have? too many Bill Gates has too many girlfriends!

Slide 8 User Input read reads one line of input from the keyboard and assigns it to one or more user-supplied variables. $ cat test3 #!/bin/sh echo "Enter name and how many girlfriends:" read name number echo "$name has $number girlfriends!" $ test3 Enter name and how many girlfriends: Bill Gates 63 Bill has Gates 63 girlfriends! $ test3 Enter name and how many girlfriends: BillG 63 BillG has 63 girlfriends! $ test3 Enter name and how many girlfriends: Bill Bill has girlfriends! l Leftover input words are all assigned to the last variable.

Slide 9 $ l Use a backslash before $ if you really want to print the dollar sign: $ cat test4 #!/bin/sh echo "Enter amount: " read cost echo "The total is: \$$cost" $ test4 Enter amount: The total is $18.50

Slide 10 $ l You can also use single quotes for printing dollar signs. l Single quotes turn off the special meaning of all enclosed dollar signs: $ cat test5 #!/bin/sh echo "Enter amount: " read cost echo ‘ The total is: $ ’ "$cost" $ test5 Enter amount: The total is $ 18.50

Slide 11 expr l Shell programming is not good at numerical computation, it is good at text processing. However, the expr command allows simple integer calculations. l Here is an interactive Bourne shell example: $ i=1 $ expr $i To assign the result of an expr command to another shell variable, surround it with backquotes: $ i=1 $ i=`expr $i + 1` $ echo " $i " 2

Slide 12 expr The * character normally means “all the files in the current directory”, so you need a “\” to use it for multiplication: $ i=2 $ i=`expr $i \* 3` $ echo $i 6 expr also allows you to group expressions, but the “(“ and “)” characters also need to be preceded by backslashes: $ i=2 $ echo `expr 5 + \( $i \* 3 \)` 11

Slide 13 expr Example $ cat test6 #!/bin/sh echo "Enter height of rectangle: " read height echo "Enter width of rectangle: " read width area=`expr $height \* $width` echo "The area of the rectangle is $area" $ test6 Enter height of rectangle: 10 Enter width of rectangle: 5 The area of the ractangle is 50 $ test6 Enter height of rectangle: 10.1 Enter width of rectangle: 5.1 expr: non-numeric argument Does not work for floats!

Slide 14 Backquotes : Command Substitution l A command or pipeline surrounded by backquotes causes the shell to: n Run the command/pipeline n Substitute the output of the command/pipeline for everything inside the quotes l You can use backquotes anywhere: $ whoami gates $ cat test7 #!/bin/sh user=`whoami` numusers=`who | wc -l` echo "Hi $user! There are $numusers users logged on." $ test7 Hi gates! There are 6 users logged on.

Slide 15 Control Flow l The shell allows several control flow statements: n if n while n for

Slide 16 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! l However, the spaces before and after the square brackets [ ] are required.

Slide 17 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!

Slide 18 if elif else l 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 ] then echo "Medium load" else echo "Just me!" fi $ test8 Heavy load!

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

Slide 20 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! How much would it cost to buy Apple Computer? Best, Bill

Slide 21 And, Or, Not l You can combine and negate expressions with: -aAnd -oOr !Not $ cat test10 #!/bin/sh if [ `who | grep gates | wc -l` -ge 1 -a `whoami` != “gates" ] then echo "Bill is loading down the machine!" else echo "All is well!" fi $ test10 Bill is loading down the machine!

Slide 22 while 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 Wakeup [yes/no]? y Wakeup [yes/no]? yes $

Slide 23 while 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

Slide 24 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 Wakeup [yes/no]? y Wakeup [yes/no]? yes $