What are Shell Scripts?
In the simplest terms, a shell script is a file containing a
series of commands. The shell reads this file and carries out the commands as
though they have been entered directly on the command line.
The shell is somewhat unique, in that it is both a powerful
command line interface to the system and a scripting language interpreter. As
we will see, most of the things that can be done on the command line can be
done in scripts, and most of the things that can be done in scripts can be done
on the command line.
How to determine Shell
You can get the name of your shell prompt, with following
command :
Syntax:
echo $SHELLLook at the above snapshot, with
the help of above command we got the name of our shell which is 'bash'.
The $ sign stands for a shell variable, echo will return the text whatever you typed in
What are "Commands?"
Commands
can be one of 4 different kinds:
- An executable program like all those files we saw in /usr/bin. Within this category, programs can be compiled binaries such as programs written in C and C++, or programs written in scripting languages such as the shell, Perl, Python, Ruby, etc.
- A command built into the shell itself. bash provides a number of commands internally called shell built-ins. The cd command, for example, is a shell built-in.
- A shell function. These are miniature shell scripts incorporated into the environment. We will cover configuring the environment and writing shell functions in later lessons, but for now, just be aware that they exist.
- An alias. Commands that we can define ourselves, built from other commands. This will be covered in a later lesson.
shell as a programming language.
The following steps are required to write shell script:
1. Use any editor like pico or vi to write shell script.
2. After writing shell script, set execute permission for your script file as follows: >chmod u+x your‐script‐name
3. Execute your script as:
>./your‐script‐name
>sh your‐script‐name
>bash your‐script‐name
Now, open an editor (for example by using pico), give the name first.sh to your file and write in this file the following commands:
# My first shell script
clear
echo "Hello World"
After saving the above script, you can run it as follows:
>./first.sh
Just notice what happens.
>bash your‐script‐name
Now, open an editor (for example by using pico), give the name first.sh to your file and write in this file the following commands: # My first shell script
clear echo "Hello World"
After saving the above script, you can run it as follows:
>./first.sh Just notice what happens.
Tip: For shell script file try to give file extension such as .sh or .scr, which can be easily identified by you as shell script.
In shell script,
there are two types of variable:
1. System variables ‐ Created and maintained by Unix/Linux itself. This type of variables is defined in CAPITAL LETTERS.
2. User defined variables (UDV) ‐ Created and maintained by user. This type of variables is defined in lower letters. You can see system variables by giving the command
>set To define UDV use the following syntax variable_name=value A value value is assigned to the given variable name. The value must be on right side
of the sign =.
Syntax: expr op1 math‐operator op2
Examples: expr 1 + 3
expr 2 ‐ 1
expr 10 / 2
expr 20 % 3
expr 10 \* 3
echo `expr 6 + 3`
>echo "Today is date"
Can't print message with today's date.
>echo "Today is `date`"
It will print today's date as Today is Fri April... Can you see that the `date` statement
uses back quote?
Try the following command:
>ls grate_stories_of It will print message‐ grate_stories_of:
No such file or directory
ls is the name of a command, which will be executed by the shell. What do we mean by commands? Which are the most used commands?
Post a Comment
If you have any doubts, please let me know