How To Write First UNIX Korn Shell Script Program

Can you tell me how to write a UNIX korn shell script program? I’m new to UNIX scripting and HP-UX UNIX operating system.

Korn shell scripting under UNIX / Linux can be used to automate lots of stuff. It easy to write a shell script. You must know how to use a text editor such as vi to write a script.

Advertisement

Writing your first Korn shell script

Let us write a shell program to print knowledge is power on screen. Type the following command to open file:
vi hello.ksh
The first line should be as follows:

#!/bin/ksh

It is called a shebang. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/ksh. All scripts under UNIX execute using the interpreter specified on a first line.
Next append code as follows:

# A shell script to print message
# Written by Tom - Jan/13/2008
print "Knowledge is power"

Save and close the file. At the end your script should look like as follows:

#!/bin/ksh
# A shell script to print message
# Written by Tom - Jan/13/2008
# ------------------------------
print "Knowledge is power"

Set executable permission

Type the following command to set executable permission:
chmod +x hello.ksh

Run your korn shell script

Type the following command:
./hello.ksh
Sample output:

Knowledge is power

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

14 comments… add one
  • Kekgabile Gone Madibela Apr 25, 2014 @ 10:42

    Write a shell program for the below actions using case construct
    a)Display the menu below
    Menu
    List_ _ _ _1
    Display content _ _ _ _ _ _ _ 2
    Copy file _ _ _ _ _ _ _ _ _ 3
    Exit
    b)Based on the selected value the program should execute. For eg. If the user enter 1, then display the files and directories. If the user enter 2, it should accept the file name and display its content. If the user enter 3, it should accept 2 file names and copy the file1 to file2.
    thank you in advance!!

  • Lisa Aug 20, 2014 @ 19:31

    Yaay! I wrote my first Korn Shell Script Program. Great instructions!

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.