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]

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
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.
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford 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.
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.
#!/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.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
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.
LINUX programming UNIT-2 1. INDEX UNIT-IV PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Working with Bourne shell L1 1-2.
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.
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.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
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.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
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.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER 8: USER INPUT.
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.
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
Shell Programming (ch 10)
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

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]

Terminating Programs: The exit Exit will end the program and when a number is included, the exit code will be the number you assigned. Otherwise, a 0 exit code will reflect a successful exit and a non zero will mean a failed script or command. Use echo $? To display the last exit code.

Conditions and Tests The if-then construct The if-then-else Construct The if-then-elif Construct

Structure of the if-then construct if [condition] then commands … … last – command fi

Rules of Conditions and Tests 1. The if statement ends with the reserved word fi (if typed backward). 2. The indentation is not necessary, but it certainly makes the code more readable. 3. If the condition is true, then all the commands between the then and fi, what is called the body of the if, are executed. If the condition is false, the body of the if is skipped, and the line after fi is executed.

if – then – else Construct if [condition] then true – commands … last – true – command else false – commands … last – false – command fi

Control Structures case.. in..esac case expression in pattern { |pattern }) list :: esac

Example #! /bin/sh echo menu test program stop=0 # reset loop-termination flag. while test $stop -eq 0 # loop until done do cat << ENDOFMENU # display menu 1 : print the date. 2, 3 : print the current working directory. 4. : exit ENDOFMENU echo echo 'your chioce? \c' # prompt read reply # read response. echo case $reply in # process response. "1") date ;; "2"|"3") pwd # display working directory ;; "4") stop=1 # set loop termination flag. ;; *) # default. echo illegal chioce # error. ;; esac done #week14 “case_in_esac”

Example $ cat > simple # Sample # A simple script to test the read command # echo "Give me a long sentence:\c" read Word1 Word2 Rest echo " $Word1 \n $Word2 \n $Rest" echo " End of my act“ (week14 simple, simple2 – what is the differences?)

Example #!/bin/ksh ## The largest of three# # # This program accepts three numbers and shows the largest of them# echo -e "Enter three numbers and I will show you the largest of them>> \c" read num1 num2 num3 if test "$num1" -gt "$num2" -a "$num1" -gt "$num3" then echo "The largest number is: $num1" elif test "$num2" -gt "$num1" -a "$num2" -gt "$num3" then echo "The largest number is: $num2" else echo "The largest number is: $num3" fi exit 0

True or false: The test command # test command example echo "Are you okay?" echo "input Y for yes or N for no:\c" read answer if test "$answer" = Y then echo "Glad to hear that" else echo "Go home or the hospital!" fi (week14 test1 and improved test2)

Logical “and” “or” “not” -a ---- Logical AND -o ---- Logical OR ! ---- Logical NOT

Test command numeric test operators -eq --- number1 –eq number2 Is number1 equal number2? -ne --- -gt --- -ge --- -lt --- -le ---

String Values Testing Test command can also be used in string coma rations. However, different set of operators will be used. $ STRING5 [Enter] $ test –z “$STRING5” [Enter] Use echo $? To see the testing result True Non-zero number --- False Make R=12345 then test –z $R echo $? You will get a non zero exit code

Test Operator for string = ---string1 = string2 ; Does string1 match string2? != n ---- Does string contain characters(nonzero length?) -z ---- Is string an empty string (zero length)

Examples $DATE1=`date` $DATE2=`date` $if test “$DATE1”=“$DATE2” > then >echo “Stop! The computer clock is dead.” >else >echo “Everything is fine.” >fi (week14 dead_clock, alive_clock)

Test file characteristics -r ---test -r fileName ; Does file exist,and is it readable? -w --- ………………is it writeable? -s --- Does file exist, and a nonzero length? -f --- Does file exist, but is not a directory file? -d --- Does file exist, and is it a directory file?

Example #File testing script FILE=xyz if test -r "$FILE" then echo "READABLE" elif test -w "$FILE" then echo "WRITEABLE" else echo "Read and Write Access Denied" Fi (week14 fileTest We can run the program again after chmod 000 xyz

More about expr command $ x=100 echo $x x=$x+11 echo $x expr 123 \* 456 expr 10\%3 expr 10/2

let command (ksh) X=100 let X=X+1 echo $X let y=x*9 echo $y *** Pay attention the fact that no \ is needed

Redirecting the standard error 1. In C program under the Unix, it is not as easy as in Codewarrior. 2. Sometimes, we need to store the errors in a file to analyze it (this is especially true when dealing a large program). 3. Use cc fileName.c –o fileName 2> error to direct the errors to the file called error in your current directory. **1 > is the default and it is the same as >. 2 > is redirection of the error output.

Example For the bubble.c we saw before, we intentionally created some errors. cc bubble.c –o bb 2>error Will redirect the error message to the file called error in your current directory.

Debugging shell programs –sh It is easy to make mistakes when you write long and complex script files. 1. Because you do not compile script files. hence you do not have the luxury of compiler error checking. 2. Normally, for short script, one can run the script to try to decipher the error messages. 3. For long and complex script, sh command with some options will help.

sh command and its options sh –n --- Read the commands but does not execute them. sh –v --- Shows the shell input lines as they are read. sh –x --- Shows the commands and their arguments as they are executed.

Examples $sh –x BOX1 [Enter] (BOX1 week14) $sh –x BOX1 [Enter]