Tuesday, April 7, 2009

Basic steps to write a shell script

Below is the step by step procedure of how to write shell script and to execute them.

Step 01 - Create a shell script:
Creating a shell script is done as you create any other plain text files. Normally one use any text editor like vi/vim or emacs to create a shell script. To make easy identification of shell script you can give extension of shell script as .sh or .bash
For example, to create a shell script named test.sh issue,

#vi test.sh
or,
#vim test.sh

Step 02 - Give executable permission to shell script:
After creating shell script you need to assign executable permission to the script. It is necessary step to execute the script. Based the executable permission user must have the read permission of the script who will run the script.
You can give the execute permission of test.sh by two ways.

#chmod +x test.sh
or,
#chmod 755 test.sh

Step 03 - Execute the script:
After giving execute permission to the script you can then execute it. With bash or sh or simple dot (.) you can run your shell script.
For example to run the test.sh you can issue,
#bash test.sh
or,
#sh test.sh
or,
#./test.sh

Debugging a script
While writing shell scripts you need to find errors in it i.e debug the shell script and need to correct the errors. The -v and -x option with sh/bash is really useful to debug the script.

The -v option is used to print shell commands as they are read along with the outcome of the script. So if after some command if see desired output is not coming you can note down that commands.

The -x option is used for expanding each command. It displays the command, system variable and then its expanded arguments.

Below is my sample test_echo.sh script which I see how debugging look like.

arju:/home/arju/test# cat test_echo.sh
#!/bin/bash
echo "Warning! You are installing the Fall Creek portal software"
echo -n "Enter the product installation directory: "
read InstallDir
if [ "$InstallDir" = "" ]; then

InstallDir=`pwd`
echo "$InstallDir is used as Fall Creek business portal installation directory."

fi
echo "$InstallDir"

With using -v option.

arju:/home/arju/test# sh -v test_echo.sh
#!/bin/bash
echo "Warning! You are installing the Fall Creek portal software"
Warning! You are installing the Fall Creek portal software
echo -n "Enter the product installation directory: "
Enter the product installation directory: read InstallDir

if [ "$InstallDir" = "" ]; then

InstallDir=`pwd`
echo "$InstallDir is used as Fall Creek business portal installation directory."

fi
pwd
/home/arju/test is used as Fall Creek business portal installation directory.
echo "$InstallDir"
/home/arju/test

With using both -v and -x option.

arju:/home/arju/test# sh -vx test_echo.sh
#!/bin/bash
echo "Warning! You are installing the Fall Creek portal software"
+ echo 'Warning! You are installing the Fall Creek portal software'
Warning! You are installing the Fall Creek portal software
echo -n "Enter the product installation directory: "
+ echo -n 'Enter the product installation directory: '
Enter the product installation directory: read InstallDir
+ read InstallDir

if [ "$InstallDir" = "" ]; then

InstallDir=`pwd`
echo "$InstallDir is used as Fall Creek business portal installation directory."

fi
+ '[' '' = '' ']'
pwd
++ pwd
+ InstallDir=/home/arju/test
+ echo '/home/arju/test is used as Fall Creek business portal installation directory.'
/home/arju/test is used as Fall Creek business portal installation directory.
echo "$InstallDir"
+ echo /home/arju/test
/home/arju/test

Related Documents
http://arjudba.blogspot.com/2009/04/what-is-kernel-shell-shell-script.html

No comments:

Post a Comment