ksh

  1. Command-line arguments
  2. Special variables
  3. Constants
  4. Variables
  5. Functions
  6. Control flows
    1. Error handling
  7. Arithmetic
  8. Comparisons
  9. Strings
  10. Output
    1. Standard
    2. Data redirection
  11. File handling
  12. Notes

Command-line arguments

What How Details
Program name, including the path if started from another directory $0  
Number of arguments $#  
Check for zero arguments if [[ $# -eq 0 ]];then
print "No Arguments"
exit
fi
 
Get the _n_th argument $n  
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 "$@"  

Special variables

Note that the positional parameters are special variables too. | What | How | Details | |—|—|—| | Exit status of last command | $? | | | Process id of current program | $$ | | | Process id of last background job | $! | |

Constants

What How 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!
What How Details
Get variable value $var  
Get variable value with default value value ${var:-value}  
Get variable and set it with default value if it is not set ${var:=value}  
Get variable value but throw error if not set ${var:?"Error! var not set!"}  
Check if variable is set ${var:+1} Returns 1 if set, else nothing
Set value for variable var=value  
Set value from user input read var  
Set command output for variable var=command args``  
Declare local variable typeset var  
Set local variable typeset var = value  

Functions

Use the local statement to define local variables.

What How Details
Define function function foo {
return $name
}
 
Define function with arguments function foo {
typeset a = $1
return $a
}
 
Call function foo  
Call function with arguments foo arg1 arg2  

Control flows

See http://www.bolthole.com/solaris/ksh-basics.html

What How Details
Chain (pipe) commands command1 | command2 | command3  
If if [[ $value -eq 7 ]];then
print "$value is 7"
fi
 
If-else if [[ $value -eq 7 ]];then
print "$value is 7"
else
print "$value is not 7"
fi
 
If-elseif if [[ $value -eq 7 ]];then
print "$value is 7"
elif [[ $value -eq 8 ]];then
print "$value is not 7 but 8"
else
print "$value is neither 7 or 8"
fi
 
Switch case $var in
john|fred) print $invitation;;
martin) print $declination;;
*) print "Wrong name...";;
esac
There is no “fall through” with ;;. You hit only one set of commands.. UNLESS you use “;&” instead of “;;’. You can use WILDCARDS to match strings.
For for foo in $(ls);do
print "\$count is $count"
(( count -= 1 ))
done | Use continue to skip the loop. Use break` to exit the loop.
 
Until    
Pause for n seconds sleep n  

Error handling

What How Details
Stop execution with unspecified error exit 1  
Throw error with message echo "Error!" 1>&2
exit 2
 
Run command conditional on success of former command1 && command2  
Run command only if first fails command1 || command2  
Run code on error trap "echo whoops" ERR  
Run code on interupt trap "echo whoops" INT e.g., Ctrl+C
Run code on error or interrupt trap "echo whoops" ERR INT  
Run code on process termination trap "echo whoops" TERM  
Run cleanup function on error trap cleanup ERR For a previously defined cleanup function

Arithmetic

What How Details
Increment variable (( a += 1))  
Increment variable let a += 1  
Sum variables sum = $(( a + b ))  

Comparisons

What How Details
And operator &&  
Or operator ||  
Equal string str $var = "str"  
Not equals string str $var != "str"  
Equals number num $var -eq num  
Not equals number num $var -ne num  
Greater than number num $var -gt num  
Less than number num $bar -lt num  

Strings

What How Details
Length "${#var}"  
Get filename from path ${name##*/}  
Get parent path ${name%/*}  

Output

Standard

What How Details
Print message echo "Hello world!"  
Print variable echo $var  
Formatted printing printf "counting %d days" $days  

Data redirection

What How Details
Write to new file or overwrite file command > file  
Append file command >> file  
Redirect error output command 2> file  
Redirect to normal output command 2>&1  
Discard errors command 2>/dev/null  
Discard all output command > /dev/null 2>&1  
File as input to command command < file  

File handling

What How Details
Check if directory exists [ -d "$DIRPATH" ]  
Check if directory is missing [ ! -d "$DIRPATH" ]  
Check if file exists [ -f "$FILEPATH" ]  
Create directory mkdir "$DIRPATH"  
Count the number of files in a directory count=find $dir -maxdepth 1 -name "*.txt" -type f | wc -l`  

Notes

  • Start script with: #!/bin/ksh
  • Enable executation for file by running chmod u+x script.sh