agenda positional parameters / continued... command substitution bourne shell / bash shell / korn...

18
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)

Upload: nickolas-kennedy

Post on 18-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

Command Substitution Symbols

Command Subsitution for various shells:

Bourne Shell `command`

Bash and Korn Shells $(command)

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

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

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

expr Example:

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

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

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...

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

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)

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

Math Processing Symbols Bourne Shell

result=`expr calculation` Bash Shell

result=$[calculation] Korn Shell

result=$((calculation))

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

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

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

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 errormessage 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

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

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

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

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

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

The “Here Document ” Script call b_day as follows:

cat <<+ Murray June 15 Joan April 2 Rajinder September 23 Anthony May 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