SHELL SCRIPTING

 

What is a Script?

What is a Script?
 The script is a text file which will contain:

  • Shell commands you normally use.
  • Shell flow control constructs (e.g., if-then-else, etc.)‏
  • Heavier use of variables than you normally would use from the command line.

What is the need of writing script?

What is the need of writing script?
Any task you do (by typing command) more than twice should probably be wrapped into a script.

Sequences of operations that you perform often can be placed into a script file and then executed like a single command (script name). So you can escape writing complex commands every time.

Automation using shell scripting ( Executing scripts of your choice at any desired time helps in saving time and better system maintenance,  without human intervention).

Working knowledge of shell scripting is essential to anyone wishing to become reasonably proficient at System Administration.

How to Write Scripts?

HOW to write Scripts?
1. Write your commands in a file using a text editor. Commands are separated by a newline or semicolon. The first line of the script can tell the system if the user wants to execute a particular shell for executing the script.
2.  Save the file and exit from the text editor program.
3.  Switch on the executable permission of script
4. If your directory (where the script resides) exists in the PATH variable then you can execute your script with its name otherwise execute the script using absolute or relative path reference.
5. When a script is run, the system determines which shell the script was written for and then executes the shell using the script as its standard input.

In which shell, the system will execute your script?

In which shell, the system will execute your script?
The system decides which shell the script is written for by examining

the first line of the script.

Rules that system used to make this decision
1. If the first line of the script is just a pound sign(#), then the script is interpreted by        the shell from which you executed this script.

2. If the first line of the script is of the form #!pathname, then the executable program pathname is used to interpret the script.

3. If neither rule1 nor rule2 applies, then the script is interpreted by a Bash shell (ie.,        default shell of Linux).

A sample: Hello world bash script

 A sample: Hello world bash script
    Write the below commands in helloworld.sh file using a text editor
     #!/bin/bash

     echo “Hello world!  Hi all ! “

     echo “This is our first shell script”

     # I am just a comment, the interpreter will ignore me

•    Save the commands and exit from a text editor.

•    Switch on the executable permission for helloworld.sh.

     chmod u+x  helloworld.sh

•     Execute the helloworld.sh (We are showing using Relative path referencing)

      ./helloworld.sh

      In which shell, the system will execute your script?

Here the first line of the script is of the form #!pathname, then the executable program /bin/bash is used to interpret the script. So this script will be executed by bash shell

       echo command at 2nd and 3rd line will print the message on the terminal

        # symbol without ! sign at other places is not treated in a special way.

        It is used for commenting a line otherwise.

The “.sh” extension of the scripts are used only for clarity, scripts don’t even require an extension

Hello world bash script
Hello world bash script

 

 

 

Another Example

Another example
  #!/bin/bash

  echo “Welcome to host $HOSTNAME running $OSTYPE .“

  echo “You are user $USER and your home directory is $HOME.”

  echo “The version of bash running on this system is $BASH_VERSION.”

  sleep 1

  echo “This script has been running for $SECONDS second(s).”

  exit

 

Example of Bash Shell Scripting
Example of Bash Shell Scripting