Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.

Similar presentations


Presentation on theme: "Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell."— Presentation transcript:

1 Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell / Korn Shell Redirection / Continued… Redirecting Standard Output / Standard Error Redirecting within a script (Here Document)

2 Positional Parameters Positional parameters can be used in other commands to accomplish tasks when creating a shell script. The following slides provide a “set of tools” in order to more effectively use these parameters for maximum benefit when creating your scripts

3 Positional Parameters $0 is name of command used to call script $1 to $9 are first nine positional parameters $* represents all positional parameters as one long string "$*" will put single pair of double quotes around string $@ same as $*, except "$@" will put double quotes around each parameter $# contains number of parameters

4 Positional Parameters can also access beyond $9 by using set braces, eg. ${10} will access 10th argument set, etc... ${var} can be used instead of $var - useful if followed by alpha-numerics $? is the exit status of the last command, 0 means successful script can be ended with exit n where n is the exit status or condition code of the script

5 Positional Parameters shift shifts parameters left, so that the second one which was $2 becomes the first which is $1, etc., allowing parameters beyond the first nine to be accessed Arguments "falling off" the left side are no longer available, so should be saved in other variables if they are required later in the script shift nn, where nn is a number, has the same effect as that number of shift commands

6 Command Substitution Command substitution is the process of using standard output from a command as the argument or arguments of another command. For Example (Store arguments as positional paramaters): set `date` These positional parameters can also be contained as arguments in your scripts myscript arg1 arg2 arg 3 arg4

7 Command Substitution Other Example Assume our current directory contains only the following files: a1, a2, and a3 cat `ls` Is the same as: cat a1 a2 a3 but saves time, and automatically adjusts to files created or removed in current directory

8 Command Substitution Symbols Command Subsitution for various shells: Bourne Shell `command` Bash and Korn Shells $(command)

9 Expressions (expr) The Bourne Shell cannot automatically perform math calculations on numbers. Therefore, the expr command to used to convert text into numbers for mathematical operations (expr is performed automatically in c, korn, and bash shells) When the mathematical operation is performed, the result is converted back to a text string

10 expr Example: echo “Enter first number \c” read num1 echo “Enter second number \c” read num2 result=`expr $num1 + $num2` echo “$num1 + $num2 = $result”

11 expr Operators used with expr command (use backslash before operator symbols): Math operators: +, -, *, /, % Comparison of strings (text): =, > Compounds & (and), | (or) Other symbols such as -lt, -le, -eq, -ne, -gt, -ge may be needed to perform numerical comparisons, with “and” as -a and “or” as -o for compounds...

12 expr Other common features: `expr length “$var”` (counts character size of var) `expr substr “$var” 2 7` (Displays text in var from position 2 to 7 characters (eg if var=january, expression would give anuary) `expr index “$var1” “$var2”` (Displays position number of string 1 of first match or “occurrence” of same character)

13 Math Processing Symbols Bourne Shell result=`expr calculation` Bash Shell result=$[calculation] Korn Shell result=$((calculation))

14 Redirecting the Standard Error Since standard output can be modified to meet the user’s needs in UNIX via pipes and filters, there is a need to separate error messages from getting mixed into standard output for information processing This is very useful for redirecting errors to a file for later analysis (trouble-shooting) or to prevent errors from displaying

15 Redirecting the Standard Error When using the > symbol to redirect output, errors still appear on the terminal 1> file.name1 or > file.name1 is used to redirect standard output to file.name1 2> file.name2 is used to redirect the standard error message to file.name2. 2>&1 is used to redirect a standard error message to standard output (terminal or file) 1>&2 is used to send standard output to the terminal (or file if redirected) as a standard error message

16 Redirecting the Standard Error Examples: if [ $# = 0 ] then echo “type an argument after script”1>&2 exit 1 fi (assume file x does not exist) cat x y 1> hold1 2> hold2 cat x y 1> hold1 2>&1 Trap error message to file hold2 Combine both standard output & error to hold1

17 The “Here Document ” In this document the symbol “<<“ is used to read from within the script itself. The plus sign “+” is used as a delimiter to end the file, as well as to instruct the shell in the first line to read until delimiter found… See example on the next screen

18 The “Here Document ” Script call b_day as follows: cat <<+ MurrayJune 15 JoanApril 2 RajinderSeptember 23 AnthonyMay 31 + To run script type b_day. If you check the online lab scripts, you can see how the “here document” is used Redirect into cat command (i.e. display) + sign used to indicate end of file


Download ppt "Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell."

Similar presentations


Ads by Google