Showing posts with label kshell tutorials. Show all posts
Showing posts with label kshell tutorials. Show all posts

Ksh Tutorials :Command Line Arguments in kshell

Ksh Tutorials :Command Line Arguments in kshell
It is also called "positional parameters"
The number of command line arguments is stored in $# so one can check
for arguments with:
if [[ $# -eq 0 ]];then
print "No Arguments"
exit
fi
The single Arguments are stored in $1, ....$n and all are in $* as one string. The arguments cannot
directly be modified but one can reset the hole commandline for another part of the program.
If we need a first argument $first for the rest of the program we do:
if [[ $1 != $first ]];then
set $first $*
fi
One can iterate over the command line arguments with the help of the shift command. Shift indirectly removes the first argument.
until [[ $# -qe 0 ]];do
# commands ....
shift
done
One can also iterate with the for loop, the default with for is $*:
for arg;do
print $arg
done
The program name is stored in $0 but it contains the path also!

Ksh Tutorials :for loop in kshell programming

Ksh Tutorials :for loop in kshell programming
while do done
while [[ $count -gt 0 ]];do
print "\$count is $count"
(( count -= 1 ))
done
until do done
until [[ $answer = "yes" ]];do
print -n "Please enter \"yes\": "
read answer
print ""
done
for var in list do done
for foo in $(ls);do
if [[ -d $foo ]];then
print "$foo is a directory"
else
print "$foo is not a directory"
fi
done
continue...break
One can skip the rest of a loop and directly go to the next iteration with: "continue".
while read line
do
if [[ $line = *.gz ]];then
continue
else
print $line
fi
done
One can also prematurely leave a loop with: "break".
while read line;do
if [[ $line = *!(.c) ]];then
break
else
print $line
fi
done

Ksh Tutorials :if else Branching

Ksh Tutorials :if else Branching
if then fi
if [[ $value -eq 7 ]];then
print "$value is 7"
fi
or:
if [[ $value -eq 7 ]]
then
print "$value is 7"
fi
or:
if [[ $value -eq 7 ]];then print "$value is 7";fi
if then else fi
if [[ $name = "John" ]];then
print "Your welcome, ${name}."
else
print "Good bye, ${name}!"
fi
if then elif then else fi
if [[ $name = "John" ]];then
print "Your welcome, ${name}."
elif [[ $name = "Hanna" ]];then
print "Hello, ${name}, who are you?"
else
print "Good bye, ${name}!"
fi
Case esac
case $var in
john
fred) print $invitation;;
martin) print $declination;;
*) print "Wrong name...";;
esac

Ksh Tutorials :Basics of shell scripting

Kshell Tutorials :Basics of shell scripting
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/ksh
It is important that the path to the ksh is propper and that the line doesn not have more than 32 characters. The shell from which you are starting the script will find this line and and hand the whole script over to to ksh. Without this line the script would be interpreted by the same typ of shell as the one, from which it was started. But since the syntax is different for 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 script as an example for these types of lines:
#!/usr/bin/ksh
# Commentary......
file=/path/file
if [[ $file = $1 ]];then
command
fi
Start and End of Script
The script starts at the first line and ends either when it encounters an "exit" or the last line. 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 line with the first word after a";'.
A command ends either at the end of the line or whith a ";". So one can put several commands onto one line:
print -n "Name: "; read name; print ""
One can continue commands over more than one line with a "\" immediately followed by a 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 mus not have a name which is identical to a unix command: So the script must NOT be called "test"!
After saveing the file give it the execute permissions with: chmod 700 filename.