• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
ksh Scripting Contents
 Principle of Script Variables Branching  Looping Command line ArgumentsComparisonsVariable Manipulationsksh Regular ExpressionsFunctions Data Redirection PipesCo-processes Read Input from User and from Files Special Variables Action on Success or Failure of a Command Trivial Calculations Numerical Calculations using "bc""grep""sed""awk""perl"
 
 Principle of Script 
Defining the Shell Type
To make a ksh script (which is a ksh program) crate a new file with a starting line like:#!/usr/bin/kshIt is important that the path to the ksh is proper and that the line doesn’t not have morethan 32 characters. The shell from which you are starting the script will find this line andhand the whole script over to ksh. Without this line the script would be interpreted by thesame typ of shell as the one, from which it was started. But since the syntax is differentfor all shells, it is necessary to define the shell with that line.
Four Types of Lines
A script has four types of lines: The shell defining line at the top, empty lines,commentary lines starting with a#and command lines. See the following top of a scriptas an example for these types of lines:
#!/usr/bin/ksh# Commentary......file=/path/fileif [[ $file = $1 ]];thencommand fi
Start and End of Script
The script starts at the first line and ends either when it encounters an "exit" or the lastline. All "#" lines are ignored.
Start and End of Command
A command starts with the first word on a line or if it's the second command on a linewith the first word after a";'.A command ends either at the end of the line or with a ";". So one can put severalcommands onto one line:
 print -n "Name: "; read name; print ""
One can continue commands over more than one line with a "\" immediately followed bya newline sign which is made be the return key:
grep filename | sort -u | awk '{print $4}' | \uniq -c >> /longpath/file
Name and Permissions of Script File
 
The script must not have a name which is identical to a unix command: So the script must NOT be called "test"!After saving the file give it the execute permissions with:chmod 700 filename.
Variables
Filling in
When filling into a variable then one uses just its name:state="US"and no blanks. Thereis no difference between strings and numbers: price=50.
Using
When using a variable one needs to put a$sign in front of it: print $state $price.
Arrays
Set and use an array like:
arrname[1]=4
To fill in
print ${arraname[1]}
To print out
${arrname[*]}
Get all elements
${#arrname[*]}
Get the number of elements
Declaration
There are happily no declarations of variables needed in ksh. One cannot have decimalsonly integers.
 Branching 
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...