The basics of KSH scripting

iKnow-IT banner

Here document

su - oracle -c /usr/ksh <<BLOCK
     ORACLE_SID=...
     exp system/manager ... ...
BLOCK


Parameters

$0 => Command-name
$# => contains the number of parameters for this shell is started
$1 $2 ... => parameter 1 parameter 2 ...
$? => check the return code
$@ => includes all the parameters that this shell is started


$0 => command-name

$ cat test.sh
echo "This is : "$0

$ ./test.sh
This is : test.sh

$

$# => contains the number of parameters for this shell is started

$ cat test2.sh
echo The number of parameters : $#

$ ./test2.sh 1 2 3 4 
The number of parameters : 4 

$ ./test2.sh a b 
The number of parameters : 2

 

$1 $2 … => parameter 1 parameter 2 …

$ cat test3.sh
echo $1 $2 $3
shift
echo $1 $2 $3 

$ ./test3.sh 1 2 3 4
1 2 3
2 3 4

$? => check the return code 
$ date
Thu Apr 15 17:12:05 DFT 1999
$ echo $?
0
$
$ dte
ksh: dte: not found
$ echo $?
127

$@ => includes all the parameters that this shell is started 

$ Makenewusers piet /home/piet
$echo $@
piet /home/piet
$

Variables

  • export command
  • . (dot) command
  • read command
  • unset command

export command

"An example of using variable without exporting"
$ VAR=“This is a variable”
$ echo $VAR
This is a variable
$ ksh
$ echo $VAR

$ exit
$ echo $VAR
This is a variable
$
"An example of using variable with exporting"
$ VAR=“This is a variable”
$ export VAR
$ echo $VAR
This is a variable
$ ksh
$ echo $VAR
This is a variable
$ exit
$ echo $VAR
This is a variable
$

. (dot) command

 .. 

 

read command 

read VAR
echo $VAR

ls | while read X
do
     echo My file : $X
done 

unset command

"unset" removes the content of variable
$ cat test4.sh
VAR1=no
echo ${VAR1}
...
unset VAR1
echo ${VAR1}
$ ./test4.sh
no
$
 

Jump and Repeat Instructions

  • for-loop
  • ‘case’
  • Test and jump
  • while-loop

for-loop

$ cat For_loop1.sh
for SHELLVAR in 1 2 3
do
    echo $ SHELLVAR
done
$ ./For_loop1.sh
1
2
3
$
$cat write_all.sh
echo Give message
echo Close with: \^D
MESSAGE1=`cat`
NAMES=`who | awk ‘{print $1}’`
for name in $NAMES
do
    echo to: $name
    write $name <<BLOCK
   $MESSAGE1
BLOCK
   echo "Message Send"
done

case statement

case $VAR in
value1)
    ....
    ....
    ;;
value2)
    ....
    ....
    ;;
*)
    ....     ....
esac  

read answer
case $answer in
[jJ]a)     
    …….     
    …….        
    ;;
[nN]ee)     
    …….      
    ;;
*)   
    echo Wrong answer

esac

read antwoord
case $antwoord in
j|J|ja|Ja|JA)          
    …….          
    …….          
    ;;
n|N|nee|Nee|NEE)          
    …….           
    ;;
*)          
    echo Fout antwoord
esac

 

Test and jump

if [ test_condition ] then
…. 
….
fi
if [ test_condition ] then
…. 
….
else
…. 
….
fi
Test and jump (test_conditions)
-r filename => Is the file readable 
-w filename => Is the file writable 
-f filename => The file exists and is not a directory 
-d filename => The file exists and is a directory 
-s filename => The file exists and is greater than 0 
-z string1 => Is "string1" the null string, length = 0 
-n string1 => Is "string1" not "null string", length <> 0 
string1 = string2 => Is "string1" equals "string2" 
string1 != string2 => Is “string1” not equals "string2” 
… -a … => The "logical AND" operator 
… -o … => The "logical OR" operator 
! … => The “NOT” operator 
var1 -comparing var2 comparing can be: eq, ne, gt, ge, lt, le

Suppose is "/home/jasper" a directory AND I may in writing

if [ -d /home/jasper -a -w /home/jasper ] then
….
fi

Suppose: shell variable NUM is a number greater than 0 and less than or equal to 10

if [ $NUM -gt 0 -a $NUM -le 10 ] then
fi

While loop

Long as the condition is true run commands between do and done

while condition_true
do      
   …..      
   …..
done      

 

while expr $count \> 0
do    
   ….    
   count=`expr $count - 1`
done  

 

ls | while read Y
do    
    ….     
    echo My file is: $Y
done
 

Functions: (- reuse pieces of code, - structuring)

NotNum()
{    
   ….    
   ….
}     read INPUT
NotNum
if [ … ]    
    echo "Invoer onjuist, alleen numerieke waarde toegestaan !\c”
fi  

 

Filters

  • The filter “cut”
  • The filter “grep”
  • The programmable filter AWK

"cut"

$ cat /etc/passwd
root:!:0:0:Super User:/:/bin/ksh
….
Jasper:!:200:1:System Administor:/home/jasper:/bin/ksh
….
$ cut -d : -f 1,5 /etc/passwd
root:Super User
….
Jasper:System Administrator
...
$ cut -c 1-6 /etc/passwd
root:!
….
Jasper
...

"grep"

grep -E '[Dd]ec|[Nn]ov' file

grep -i dec file

 

AWK (Aho - Weinberger - Kernighan "The programmable filter")

$ cat /etc/passwd
root:!:0:0:Super User:/:/bin/ksh ….
Jasper:!:200:1:System Administor:/home/jasper:/bin/ksh ….
$ cat /etc/passwd | awk ’/jasper/ { print $0 }’
Jasper:!:200:1:System Administor:/home/jasper:/bin/ksh
cat /etc/passwd |
awk ’
BEGIN { print “all user accounts with ksh and csh“ }
   /ksh/ { print $0 }
   /csh/ { print $0 }
END { print “These were …” } ’
$ ls -l | awk ’
BEGIN { print ”Custom directory list” }
{ b+=$5; print $9 ” \t” $5 ” \t bytes” }
END { print ”Total: ” b ” bytes ” } ’

Custom directory list
file1.dat              109   bytes
myawk.sh          302   bytes
myawk.out       2034   bytes
Total: 2445 bytes $

$ awk ’
BEGIN { FS=”:” ; print ” \t Account \t Name ” }
{ print NR ”. \t” $1 ”\t” $5 }’ /etc/passwd

          Account       Name

1.      root               Super User
2.     daemon        System Daemon
..
..
16.     guest           Guest user

 $

$ ls -l

-rw-r--r-- 1   oracle   dba   123  Mar 09  19:32  init.ora
-rw-r--r-- 1   oracle   dba   231  Mar 09  19:48  inittest.ora
-rw-r--r-- 1   oracle   dba   95    Mar 06  14:20  test.sga

$ ls -l | awk ‘
{ if ( Grootste < $5 ) { Grootste = $5; Grootste_file = $9 } }
END { print “Grootste file is “ Grootste_file “ “ Grootste “ bytes“ }’

Grootste file is inittest.ora 231bytes
$

$ SED (stream-editor)

-rw-r--r--1   oracle   dba   123   Mar 09 19:32 init.ora
-rw-r--r--1   oracle   dba   231   Mar 09 19:48 inittest.ora
-rw-r--r--1   oracle   dba      95  Mar 06 14:20 test.sga

$ ls -l | sed 's/  */^/g'

 

-rw-r--r--^1^oracle^dba^123^Mar^09^19:32^init.ora
-rw-r--r--^1^oracle^dba^231^Mar^09^19:48^inittest.ora
-rw-r--r--^1^oracle^dba^95^Mar^06^14:20^test.sga