Features and benefits of using shell scripts:

*Quick prototyping and no need to compile
*Create your own new commands
*Combine long and repetitive sequences of commands into one single command
*Automate task and reduce risk of errors

Shell script commands can be divided into three categories :

  • Compiled applications like rm, ls
  • Built-in bash commands like cd, pwd
  • Other scripts

Command interpreter is used to execute statements that follow in the script. Few used are .usr/bin/per1 , /usr/bin/python

The Hello World :
> cat > exscript.sh
>#!/bin/bash
>echo “HELLO”
>echo “WORLD”
Next press ENTER and CTRL-D to save the file exscript.sh which we just created.
To make the file executable for all users run the command >chmod +x exscript.sh .
Now , to run the command type >./exscript.sh

Few characters are used for special purpose in bash scripts :

# : used to add comment except when \# or #! (to start a script).
; : tells that next coming command is code i.e. putting multiple command on single line. (can be further refined with && and || )
\ : used to end a line and continue to next in same command i.e. splitting long commands over multiple lines.
$ : what follows this is variable.

Creating a code block or function:

Declaration syntax :
function_name () {
command…
}

Environment Variables:

It is a variable with a name and a value which can be used by one or more applications.It must be prefixed with $ symbol. For eg. to display value stored in the PATH variable command is >echo $PATH .

For exporting variables within a script which is in sub-shell export statements are used.
eg. export VAR=value or VAR=value ; export VAR

Output redirection:

Output obtained from a script can be diverted to a file using > character.
Input Redirection:
Input can be obtained from a file using < character

Conditionals :

If statement SYNTAX :

if condition
then
statements
else
statements
fi

More on bash script will be uploaded soon ! 🙂

Leave a Reply