Monday 8 August 2022

THEORY -9 :- What are Shell Scripts? in LINUX

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 $SHELL  

Look 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:

 

  1. 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. 
  2. 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.
  3. 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.
  4. 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?




while [ condition ] do command1 .... done 4. Useful shell commands Purpose Command ‐ Syntax Example Translate a range of characters into another range of characters tr "pattern‐1" "pattern‐2" >tr "[a‐z]" "[A‐Z]" Pattern scanning and processing language awk 'pattern‐action' file >awk '/zebra/{ print $3 }' test Stream editor sed 'expression' file sed '{ expression1; expression2; }' file >sed '/zebra/s//monkey/g' test Report or filter out repeated lines in a file uniq file >sort test | uniq Search a file for a pattern grep 'pattern' file >grep –v 'zebra' test 5. Commands related with process In the following, there are the most commonly used command(s) related with process: Purpose Command Example To see currently running processes ps >ps To stop any process by PID, i.e. to kill a process kill {PID} >kill 1012 To stop processes by name, i.e. to kill a process killall{process‐name} >killall httpd To get information about all running processes ps –ag >ps ‐ag Shell Script Tutorial Paraskevi Raftopoulou p. 12/12 To stop all processes except your shell kill 0 >kill 0 For background processing command & >./myScript.sh & To display the owner of the processes along with the processes ps aux >ps aux To see if a particular process is running or not. ps ax | grep process‐  U‐want‐to see >ps ax | grep httpd To see currently running processes and other information like memory and CPU usage with real time updates. top Note that to exit from top command press q. >top To display a tree of processes pstree >pstree 




Popular Posts