# Korn Shell Notes

## 1 Useful Cmds

wc - display a count of lines, words and characters in a file.
It can be used to count the numbers together with other commands, such as ls, who and so on.

cut - cut out selected fields of each line of a file.
The name of each unique user that is logged on is displayed using this command:
$who | cut —f1 —d' ' | sort —u anatole root print is a Korn shell specialized command which is similar to echo. nl - line numbering filter. ## 2 Process Execution ### 2.1 Conditional Execution You can implement a simple if command by using the && and || operators together like this: command1 && command2 || command3 If command1 returns true, then command2 is executed, which causes command3 to not be executed. If command1 does not return true, then command2 is not executed, which causes command3 to be executed. ### 2.2 Grouping Commands Commands enclosed in {} are executed in current shell. If$ echo "This is file temp:" ; cat temp | nl
This is file temp:
1 The rain in Spain
2 falls mainly on the plain
then by using {}s, the output will be:
${ echo "This is file temp:"; cat temp ; } | nl 1 This is file temp: 2 The rain in Spain 3 falls mainly on the plain There must be whitespace after the opening {, and commands within the {}'s must be terminated with a semi-colon. Commands enclosed in () are executed in a subshell. Subshells are generated whenever you enclose commands in ()'s, perform command substitution, for background processes, and for coprocesses.A subshell is a separate copy of the parent shell, so variables, functions, and aliases from the parent shell are available to the subshell. However, subshells cannot change the value of parent shell variables, functions, or aliases. ## 3 Input/Output Redirection ### 3.1 Redirecting Standard Output By using the > symbol, the standard output of a command can be redirected to a file, instead of your terminal. You can also use the > command to create an empty file:$ > tmp
$ls —s tmp 0 tmp This is equivalent to touch tmp. Standard output can be appended to a file by using the >> redirect operator. Standard output is closed with the >&- redirect operator:$ echo "This is going nowhere" >&-
$set —o noclobber The >| operator is used to force overwriting of a file, even if the noclobber option is enabled. ### 3.3 Redirecting Standard Input Standard input to a command is redirected from a file using the < operator. ### 3.4 Redirecting Standard Error As with standard output, standard error is by default directed to your terminal, but it can be redirected using the standard error file descriptor (2) and the > operator, e.g. 2> ### 3.5 More With File Descriptors The >&n operator causes output to be redirected to file descriptor n. This is used when you want to direct output to a specific file descriptor.In the next command, the standard error and output are sent to ls.out by specifying multiple redirections on the same command line. First, >&2 causes standard output to be redirected to standard error. Then, 2>ls.out causes standard error (which is now standard output and standard error) to be sent to ls.out:$ ls tmp t.out 2>ls.out 1>&2
$cat ls.out tmp not found t.out If the output of the last command was redirected to a file using the > operator, then only the standard output would be redirected. The standard error would still be displayed on your terminal.$ {echo "This is going to stdout"} >&1;/
echo "This is going to stderr" >&2;} >out
This is going to stderr

#### 4.5.2 ?(pattern)

This format matches any zero or one occurrences of pattern. Here are some more patterns using this format:

#### 4.5.3 +(pattern)

This format matches one or more occurrences of pattern.

This format matches exactly one occurrence of pattern. Let's look for words beginning with Ala or Cla:     $match @([AC]la)* #### 4.5.5 !(pattern) This format matches anything except pattern. To match any string that does not end in .c, .Z, or .o: !(*.c|*.Z|*.o) or any string that does not contain digits: !(*[0-9]*) ### 4.6 Disabling File Name Substitution File name substitution can be disabled by setting the noglob option using the set command:$ set —o noglob
or
$set —f The -o noglob and -f options for the set command are the same. Once file name substitution is disabled, pattern-matching characters like *, ?, and ] lose their special meaning:$ ls a*
$ls b? b? not found Within [...] patterns, a / character is used to remove the meaning of the special pattern-matching characters. This means that the [/*/?]* pattern would match file names beginning with * or ?. ## 5 Command Substitution Command substitution is a feature that allows output to be expanded from a command. It can be used to assign the output of a command to a variable, or to imbed the output of a command within another command. The format for command substitution is:$(command)
where command is executed and the output is substituted for the entire $(command) construct. For example, to print the current date in a friendly format:$ echo The date is $(date) The date is Fri Jul 27 10:41:21 PST 1996 ### 5.1 Bourne Shell Compatibility For compatibility with the Bourne shell, the following format for command substitution can also be used: command  Using . . . command substitution, we could get the names of the files in the current directory like this:$ echo ls are in this directory
NEWS asp bin pc are this directory

### 5.2 Directing File Input

There is also a special form of the $(...) command that is used to substitute the contents of a file. The format for file input substitution is:$(<file)
This is equivalent to $(cat file) or cat file, except that it is faster, because an extra process does not have to be created to execute the cat command. ### 5.3 Arithmetic Operations Another form of the$(...) command is used to substitute the output of
arithmetic expressions. The value of an arithmetic expression is
returned when enclosed in double parentheses and preceded with a dollar
sign.
$((arithmetic-expression)) Here are a few examples.$ echo $((3+5)) 8$ echo \$((8192*16384%23))

9

### 5.4 Tilde Substitution

Tilde substitution is used to substitute the pathname of a user's home
directory for ~user. Words in the command line that start with the
tilde character cause the Korn shell to check the rest of the word up
to a slash. If the tilde character is found alone or is only followed
by a slash, it is replaced with the value of the HOME variable.