Linux Shell Scripting Tutorial (LSST) v1.05r3 | ||
Chapter 2: Getting started with Shell Programming | ||
|
Let us try to understand each step in depth, if you are already familiar with above four steps please skip to next section.
As discussed earlier shell scripts stored in plain text file, generally one command per line. You can use text editor such as vi or emacs. I recommend using vim (Vi Improved) as it is equipped with features such as syntax highlighting and indenting. Most modern Linux (or *BSD) distribution comes with vim. You can start vi or vim from shell prompt by typing any one of the following command:
$ vi myscript.bash $ vi myscript.sh $ vim myscript.bash
Make sure you use .bash or .sh file extension for each script. This ensures easy identification of shell script.
Once script is created, you need to setup executable permission on a script. Why to setup executable permission, might be next question in your mind, right?
Simple,chmod permission your-script-nameExamples:
$ chmod +x your-script-name $ chmod 755 your-script-name
Now your script is ready with proper executable permission on it. Next, test script by running it.
Syntax:bash your-script-name sh your-script-name ./your-script-nameExamples:
$ bash bar $ sh bar $ ./barIn last example, you are using . (dot) command which read and execute commands from filename in the current shell. If filename does not contain a slash, file names in PATH are used to find the directory containing filename. For example:
./barbar script executed from current directory. The specialty of dot (.) command is you do not have to setup an executable permission on script.
While programming shell sometimes you need to find out errors (bugs) in shell script and correct all errors (remove errors i.e. debug script). For this purpose you can pass -v and -x option to sh/bash command to debug the shell script. General syntax is as follows: Syntax:
sh option { shell-script-name }OR
bash option { shell-script-name }Where,
$ vi first |
After saving the above script, you can run the script as follows:$ ./first
This will not run script since we have not set execute permission for our script first; to do this type command$ chmod 755 first
$ ./first
First screen will be clear, then Knowledge is Power is printed on screen.
Script Command(s) | Meaning |
$ vi first | Start vi editor |
# # My first shell script # | # followed by any text is considered as comment. Comment gives more information about script, logical explanation about shell script. Syntax: # comment-text |
clear | clear the screen |
echo "Knowledge is Power" | To print message or value of variables on screen, we use echo command, general form of echo command is as follows syntax: echo "Message" |
How Shell Locates the file (My own bin directory to execute script)
Tip: For shell script file try to give file extension such as .sh, which can be easily identified by you as shell script.
Exercise:
1)Write following shell script, save it, execute it and note down the it's output.
$ vi ginfo |
Future Point: At the end why statement exit 0 is used? See exit status for more information.