Korn Shell Script Writing
Korn Shell Script Writing
TCS Internal
TCS Internal
Create using editor of choice Can include a #! construct in first line of script to override login shell
#!/bin/sh uses Bourne shell to execute script
TCS Internal
TCS Internal
TCS Internal
For methods 2 and 3 the shell runs another copy of itself as a sub process.
TCS Internal
Functions
Improves shell's programmability
already in memory (unless autoloaded) modular programing
Syntax:
function functname { shell commands }
or,
functname () { shell commands }
TCS Internal
Functions (cont.)
Delete a function definition with
$ unset -f functname
TCS Internal
Functions (cont.)
Two important differences between functions and shell scripts run by name (Method 2):
functions do not run in a subprocess; behave more like a script run by Method 1 functions have precedence over scripts
Function names should begin with a letter or underscore The closing brace (}) must appear on a line by itself
TCS Internal
Functions (cont.)
Example: Run a program and view the output
$ myrun () >{ >cd $WORKDIR > a.out < data.in > more data.out > /bin/rm data.in > mv data.out data.in >}
TCS Internal
10
Precedence of Commands
Now that we have discussed the various sources of commands, let us show their order of precedence: 1.Keywords such as function, if and for (see later) 2.Aliases 3.Shell built-ins 4.Functions 5.Scripts and executable programs, for which the shell searches in the directories listed in the PATH environment variable.
TCS Internal
11
eval
You can think of quoting as a way of getting the shell to skip some of the steps of command-line processing. Consider:
$ listpage="ls | more" $ $listpage ls: |: No such file or directory ls: more: No such file or directory
The shell evaluates variables after it has looked for metacharacters like |
TCS Internal
12
eval
The command $ eval $listpage works because the line is "rescanned" by the shell. eval is an advanced command that requires some cleverness to be used effectively
TCS Internal
13
Shell Variables
The Korn shell includes the following types of variables:
User-defined variables Special shell variables
User-defined variables can be declared, read and changed from the command line or from within a shell script. A variable name can consist of the following:
letters digits underscore character first character of a variable name must be a letter or an underscore character
TCS Internal
14
TCS Internal
15
TCS Internal
16
Null Variables
A variable can be set to a null value, even if previously assigned, using any of the following methods. There should be no spaces preceding or following the equal sign.
$ name= $ name='' $ name="" $ unset varname
TCS Internal
17
TCS Internal
18
$$? $$ $! $0 $* $@
TCS Internal
19
TCS Internal
20
TCS Internal
21
TCS Internal
22
TCS Internal
23
The variable $0 contains the name of the command (process) currently being executed.
$ cat cmd_name
$ new_name
The name of this script is new_name
TCS Internal
24
TCS Internal
25
TCS Internal
26
print Arg 1 is: $1 print Arg 2 is: $2 print Arg 3 is: $3 print Arg 4 is: $4 print Arg 5 is: $5
TCS Internal
27
TCS Internal
28
$newpos12 starting args are 1 2 number of args is 2 arg 1 is 1 arg 2 is 2 new args are 3 4 number of args is 2 arg 1 is 3 arg 2 is 4
TCS Internal
29
Use the shift command to perform a shift of the positional parameters n positions to the left. (Default n=1).
TCS Internal
30
TCS Internal
31
TCS Internal
32
Parameter Substitution
Notes on Variable Syntax
Syntax $name is not quite accurate ${name} is more general
$ file=new;cp $file ${file}copy
Curly braces can be omitted as long as name is followed by a character that isn't a letter, digit, or underscore This syntax allows for parameter substitution: replacement of the value of a variable with that of another based on specific conditions and events.
TCS Internal
33
TCS Internal
34
$ FOO=/usr/local/bin/xx $ print edit file ${FOO:=$HOME/xx} edit file /usr/local/bin/xx $ print $FOO /usr/local/bin/xx
TCS Internal
35
TCS Internal
36
TCS Internal
37
TCS Internal
38
TCS Internal
39
Command Substitution
Two ways seen thus far for getting values into variables:
assignment statements command-line arguments (positional parameters)
Command substitution is a third way: $(UNIX command) or `UNIX command` (archaic) Examples:
$ DAY=$(date +%A) $ FILE_LIST=$(ls) $ LOGGED_ON=$(who|cut -f1 -d' ') $ FILE=$(< filename)
40
Exercises
1. Write a script called lsc which executes the command ls -C. To execute this command you must give the full path name to your lsc script. Make the lsc shell script executable and run it. 2.Write a script called b which changes your current directory to /bin, displays the current directory, and then executes the lsc script created above. Make the b script executable and run it. What is your current directory when the b script is finished executing? Why? 3.Write a script called ex_on, which turn on execute permission on the first argument on the command line. 4.Modify ex_on to turn on execute permission on all arguments on the command line. 5.Write a script called 12 that prints the twelfth argument on the command line.
TCS Internal
41
TCS Internal
42
Flow Control
Flow control gives a programmer the power to specify that only certain portions of a program run, or that certain portions run repeatedly. Korn shell supports the following flow control constructs:
if/else Execute a list of statements if a certain condition is/is not true for Execute a list of statements a fixed number of times while Execute a list of statements repeatedly while a certain condition holds true until Execute a list of statements repeatedly until a certain condition holds true case Execute one of several lists of statements depending on the value of a variable select Allow the user to select one of a list of possibilities from a menu
TCS Internal
43
Exit Status
At the end of its execution, every UNIX command returns a status number to the process that invoked it. Indicates whether or not the command ran successfully. An exit status of zero is used to indicate successful completion. A nonzero exit status indicates that the program failed. Some exceptions exist (diff). The shell sets the $? variable to the exit status of the last foreground command that was executed.
TCS Internal
44
Exit Status
The constructs if, while, until and the logical AND (&&) and OR (||) operators use exit status to make logical decisions: 0 is a logical "true" (success) 1 is a logical "false" (failure)
There are built-in true and false commands which you can use.
TCS Internal
45
TCS Internal
46
TCS Internal
47
The logical constructs &&/|| are commonly used with if/else constructs
TCS Internal
48
if / else
Simplest type of flow control construct is the conditional embodied in Korn shell's if statement. Syntax:
if condition then statements [elif condition then statements . . .] [else statements] fi
TCS Internal
49
if / else (cont.)
The if statement uses an exit status to determine whether or not to execute the commands. Statements are executed only if the given condition is true. If one or more elifs are used, the last else clause is an "if all else fails" part.
TCS Internal
50
Condition Tests
The if construct can only test exit status but that doesn't limit you to checking only whether commands ran properly or not. Using the [[ ]] construct, many different attributes can be tested:
pattern matching on strings file attributes arithmetic conditionals
[[ condition ]] just returns an exit status that tells whether condition is true or not (fits within if construct's syntax of if statements). [[ ]] surround expressions that include various types of operators.
TCS Internal
51
TCS Internal
52
str = pat str matches pat. str != pat str does not match pat. str1 < str2 str1 is less than str2.* str1 > str2 str1 is greater than str2.* -n str str is not null (has length greater than 0). -z str str is null (has length 0). *based on the ASCII value of their characters
str refers to an expression with a string value, and pat refers to a pattern that can contain wildcards.
TCS Internal
53
TCS Internal
54
Test
-lt -le -eq -ge -gt -ne
TCS Internal
Comparison
less than less than or equal equal greater than or equal greater than not equal
55
TCS Internal
56
TCS Internal
57
TCS Internal
58
TCS Internal
59
for
Previous tests only allow reporting on single files since tests like -f and -x only take single arguments The for loop allows you to call a section of code a fixed number of times, e.g., once for each file given on the command line. During each time through the iteration, the loop variable is set to a different value.
TCS Internal
60
for
Previous tests only allow reporting on single files since tests like -f and -x only take single arguments The for loop allows you to call a section of code a fixed number of times, e.g., once for each file given on the command line. During each time through the iteration, the loop variable is set to a different value.
TCS Internal
61
for (cont.)
Syntax:
for name [in list] do statements that can use $name done
name is a variable which can be called anything (commonly called i) list is a list of names (defaults to "$@") name is set to each name in list, in order, for each iteration; the number of iterations equals the number of names in list.
TCS Internal
62
for (cont.)
Examples
$ cat simple
TCS Internal
63
for (cont.)
Examples
Check to see who is logged on the machines listed in the variable SYSTEMS="myrtle gull sandy newport daytona":
for sys in $SYSTEMS x do finger @$sys print done
TCS Internal
64
for (cont.)
list can contain shell wildcards and command substitution as well:
$ cat file1
for i in * do
print $i done $ file1 file1 file2
TCS Internal
65
case
Provides a multiple choice decision structure. Lets you test strings against patterns that can contain wildcard characters. Syntax:
case expression in pattern1) statements ;; pattern2) statements ;; ... esac
TCS Internal
66
Case (cont.)
If expression matches one of the patterns, its corresponding statements are executed If there are several patterns separated by pipes, the expression can match any of them in order for the associated statements to be run Patterns are checked in order for a match; if none is found, nothing happens
TCS Internal
67
Case (cont.)
Here's a simple script which moves C and fortran source files to one directory and object code files to another:
for file in $*; do case $file in *.c|*.f) /bin/mv $file ${HOME}/src ;; *.o ) /bin/mv $file ${HOME}/obj;; *) print $file not moved ;; esac done
68
Case (cont.)
The case statement is often used to specify options for a shell script. Here is a shell script called dt_fmat that allows the user to enter options that affect the way the date is displayed:
case $1 in -d) print -n Date: date +"%a %h %d" ;; -t) print -n Time: date +"%T" ;; -w) print -n Weekday: date +"%a" ;; -n) print -n Date: date +"%D" ;; -y) print -n Year: 19 date +"%y" ;; -m) print -n Month: date +"%h" ;;
TCS Internal
69
Case (cont.)
*) print -n Date: date +"%a %h %d" ;; print -n Time: date +"%T" ;; esac $ dt_fmat -y Year: 1996 $ dt_fmat * Date:Thu Oct 17 Time:14:33:36
TCS Internal
70
select
select allows you to generate simple menus easily.
Syntax:
select name [in list] do statements that can use $name done
This is the same syntax as the for loop except for the keyword select. As with for, in list defaults to "$@" if omitted. A menu is generated for each item in list, formatted with number for each choice.
TCS Internal
71
Select (cont.)
The selected choice is stored in name and the selected number in REPLY Executes the statements in the body Repeats the process forever; exit loop with break statement (or user can issue ctrl-d)
TCS Internal
72
Select (cont.)
The following script termselect allows you to select a terminal setting:
PS3='terminal? ' select term in vt100 vt220 xterm do if [[ -n $term ]]; then TERM=$term print TERM is $term break else print invalid choice fi done vt100 vt220 xterm
<--- the list can be expanded for clarity using continuation lines and quotes
TCS Internal
73
Select (cont.)
$ termselect 1) vt100 2) vt220 3) xterm terminal? 4 invalid choice terminal? 3 TERM is xterm
TCS Internal
74
As with if, the condition is really a list of statements that are run; the exit status of the last one is used as the value of the condition. [[ ]] can be used here as with if. Beware of creating an infinite loop (condition must become false at some point).
TCS Internal
75
a 1 Until allows a section of code to be run repetitively as long as a certain condition is false.
Just about any until can be converted to a while by simply negating the condition.
TCS Internal
76
Command-line Arguments
We want to expand on our ability to use command-line options to shell scripts Typical UNIX commands have the form
command [-options] arguments
meaning that there can be 0 or more options. A piece of code that handles a single option called -o and arbitrarily many arguments would be:
if [[ $1 = -o ]]; then process the -o option shift fi normal processing of arguments ...
TCS Internal
77
You want to write a program that prints the N types of coins of which you have the most. The default for N is 10. The program should take one argument for the name of the input file and an optional argument for how many lines to print. A simple implementation would be:
filename=$1 howmany=${2:-10} sort -nr $filename | head -$howmany
TCS Internal
78
TCS Internal
79
TCS Internal
80
TCS Internal
81
*
/ < > <= >= == !=
&&(||)
TCS Internal
TCS Internal
83
TCS Internal
84
$x 5 5 25 5
85
Exercises
1.Write a script called lis that uses a for loop to display all files and directories in the current directory. 2.Write a script called char that checks a single character on the command line, c. If the character is a digit, digit is displayed. If the character is an upper or lowercase alphabetic character, letter is displayed. Otherwise, other is displayed. Have the script print an error message if the argument c is more than one character in length. 3.Write a script called mycopy that copies a source file to multiple destinations. Add a check to see if the source file exists. If the source file does not exist, print an error message. 4.Write a script called mydir that prints the message File is a directory if the file is a directory.
TCS Internal
86
Exercises (cont.)
5.Write a script called ver that accepts one argument on the command line indicating the name of a file. The file is copied to another file with the same name with the addition of the extension .v2. Also, the line #Version 2 is added to the top of the file. 6.Execute the ver script on itself, creating a new version of the file called ver.v2. 7.Rewrite ver.v2 to accept a possible second argument. If two arguments are entered, the file specified by the first argument is copied to a file with the name of the second argument. If no second argument is entered, the file is copied to another file with the same name, adding the extension .v2. In either case, the line #Version2 is added to the top of the file.
TCS Internal
87