Incomplete sheet
This sheet is incomplete and could use some attention. Please submit code snippet suggestions as an issue or PR here.
Korn shell
https://en.wikipedia.org/wiki/KornShell
Details
Notes:
- Start script with:
#!/bin/ksh
- Enable executation for file by running
chmod u+x script.sh
Command-line argument parsing
| Action |
Code |
Details |
|
Program name, including the path if started from another directory
|
|
|
|
Number of arguments
|
|
|
|
Get the _n_th argument
|
|
|
|
Get the _n_th argument, with default value
|
${n-"Default value here"}
|
|
|
Expand all arguments to a single word
|
|
|
|
Expand all arguments to single string
|
|
|
|
Expand arguments to separate words
|
|
|
Test
| Action |
Code |
Details |
|
Check for zero arguments
|
if [[ $# -eq 0 ]];then
print "No Arguments"
exit
fi
|
|
Special variables
Note that the positional parameters are special variables too.
| Action |
Code |
Details |
|
Exit status of last command
|
|
|
|
Process id of current program
|
|
|
|
Process id of last background job
|
|
|
Constants
| Action |
Code |
Details |
|
Declare constant
|
declare -r PASSWD_FILE=/etc/passwd
|
|
Variables
- Don't put dots in variable names!
- By default, all variables are global! Even within functions!
| Action |
Code |
Details |
|
Get variable value
|
|
|
|
Get variable value with default value value
|
|
|
|
Get variable value but throw error if not set
|
${var:?"Error! var not set!"}
|
|
|
Get variable and set it with default value if it is not set
|
|
|
|
Check if variable is set
|
|
Returns 1 if set, else nothing |
|
Set value for variable
|
|
|
|
Set value from user input
|
|
|
|
Set command output for variable
|
|
|
|
Declare local variable
|
|
|
|
Set local variable
|
|
|
Functions
Use the local statement to define local variables.
| Action |
Code |
Details |
|
Define function
|
function get_name {
return $name
}
|
|
|
Define function with arguments
|
function increment {
local x = $1
return (( $x + 1 ))
}
|
|
|
Call function
|
|
|
|
Call function with arguments
|
|
|
Control flows
See http://www.bolthole.com/solaris/ksh-basics.html
| Action |
Code |
Details |
|
Chain (pipe) commands
|
command1 | command2 | command3
|
|
|
If
|
if [[ $value -eq 2 ]];then
print "$value is 2"
fi
|
|
|
If-else
|
if [[ $value -eq 2 ]];then
print "$value is 2"
else
print "$value is not 2"
fi
|
|
|
If-elseif
|
if [[ $status -eq 200 ]];then
print "OK (status = 200)"
elif [[ $value -gt 400 ]];then
print "Error (status > 400)"
else
print "Not 200 but < 400"
fi
|
|
|
Switch
|
case $food in
apple|pear) print "fruit";;
onion) print "veggy";;
*) print "unknown";;
esac
|
Cases don't fall through, unless ;& is used instead of ;;
Strings can be matched using wildcards. |
|
For
|
for f in $(ls);do
(( count += 1 ))
done
print "Total: $count"
|
Use continue to skip the loop. Use break to exit the loop. |
|
Until
|
until [[ $stopnow -eq 1 ]] ; do
echo just run this once
stopnow=1;
echo we should not be here again.
done
|
|
|
Pause for n seconds
|
|
|
Error handling
| Action |
Code |
Details |
|
Throw error and stop execution, with unspecified error
|
|
|
|
Throw error with message
|
echo "Error!" 1>&2
exit 2
|
|
|
Run command only if the former command succeeds
|
|
|
|
Run command only if the former command fails
|
|
|
|
Run (cleanup) function on error
|
|
For a previously defined cleanup function |
|
Run code on error
|
|
|
|
Run code on interupt
|
|
e.g., when user presses Ctrl+C |
|
Run code on error or interrupt
|
trap "echo Oh no!" ERR INT
|
|
|
Run code on process termination
|
|
|
Number handling
| Action |
Code |
Details |
|
Increment variable
|
|
|
|
Increment variable
|
|
|
|
Sum variables
|
|
|
|
Equals number num
|
|
|
|
Not equals number num
|
|
|
|
Greater than number num
|
|
|
|
Less than number num
|
|
|
String handling
| Action |
Code |
Details |
|
Equals string str
|
|
|
|
Not equals string str
|
|
|
Path handling
| Action |
Code |
Details |
|
Get parent path
|
|
|
|
Get filename from path
|
|
|
Output
| Action |
Code |
Details |
|
Print message
|
|
|
|
Print variable
|
|
|
|
Formatted printing
|
printf "counting %d days" $days
|
|
Data redirection
| Action |
Code |
Details |
|
Write to new file or overwrite file
|
|
|
|
Append file
|
|
|
|
Redirect error output
|
|
|
|
Discard errors
|
|
|
|
Redirect to normal output
|
|
|
|
Discard all output
|
|
|
|
File as input to command
|
|
|
Filesystem handling
| Action |
Code |
Details |
|
Check if directory exists
|
|
|
|
Check if directory is missing
|
|
|
|
Create directory
|
|
|
|
Check if file exists
|
|
|
|
Count the number of files in a directory
|
count=find $dir -maxdepth 1 -name "*.txt" -type f | wc -l
|
|