How to Write a Script in K Shell

In the UNIX/Linux environment, there are many shell programs available. To run a Script in Unix you need shell programming to be installed in your Linux/Unix Os. Why I chose this topic is we are in the automation age and the automated things that run in the form of Script.


The list of different shell programs

  1. Bourne Shell
  2. Bourne again shell (Bash)
  3. C Shell
  4. T Shell
  5. K shell

I have recently installed CentOs in my Vmware player and created Virtual machine. I installed light version of CentOs. In that, by default, only bourn shell available.

I want to install ‘Korn Shell’, so installed KSH using the below command. You can learn Linux scripting quickly without missing a single point here.

$ yum install  ksh

The below script I have created as a part of my learning.

$ vi myfirstscript.sh

Then, it goes to edit mode…Always better write your code only in small case letters. Since some interpreters, does not support Capital case letters.

The first symbol ‘#!’ indicates the location of your ‘ksh’ program. After entering into edit mode you need to press ‘Ins’, else, it will not allow you to edit.

The ‘;;’ indicates that end statement for each case.

The ‘*’ indicates that when other condition.

#My first menu script
#!/bin/ksh
echo " Enter Your Option Here:"

select menu_selections in Y N Q
do
  case $menu_selections in 
  Y) echo "Happy Birthday "
     ;;
  N) echo "Not Your Birthday "
     ;;
  Q) echo "Later tater "
     ;;
  *) echo "Invalid user"
     ;;
  esac
done
#End of script.

After entering the above pres ‘Esc’ and at the end give ‘:wq”. This means that quit and save.

$ ./myfirstscript.sh

Initially you will get an error, because you do not have execution permission. So, first you need to give permission. See more on file permissions.

$chmod 775 myfirstscript.sh

Then

$./myfirstscript.sh

The display comes as…

Enter Your Option Here:

1)Y
2)N
3)Q
#?

So, just give 1, 2, or 3 and any other number and verify the result. This is the first script I practiced. So I am sharing it with you.

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.