Tuesday, 26 January 2016

SHELL Scripts


When a group of commands have to be executed regularly, they should be stored in a file and the file itself executed as shell script or shell program.Though its not mandatory, we normally use the .sh extension for shell scripts. this makes to match them with wild cards.Shell scripts are eecuted in a separate child shell process.

Read : Making  script interactive

A common use for user-created variables is storing information that a user enters in response to a prompt. Using read , scripts can accept input from the user and store that input in variables. The
read builtin reads one line from standard input and assigns the words on the line to one or more variable.

Example:

$ cat file1
echo -n "Enter Name: "
read name
echo "Your Name is $name"

$ ./read1
Enter Name: Sample
Your Name is Sample

Multiple inputs read:

$ cat read3
read -p "Enter something: " word1 word2 word3
echo "Word 1 is: $word1"
echo "Word 2 is: $word2"
echo "Word 3 is: $word3"
$ ./read3
Enter something: this is something
Word 1 is: this
Word 2 is: is
Word 3 is: something

Command line arguments:

UNIX commands, shell scripts also accept arguments from the command line.
They can, therefore, run non interactively and be used with redirection and
pipelines.

Positional Parameters:

Arguments are passed from the command line into a shell program using the
positional parameters $1 through to $9. Each parameter corresponds to the
position of the argument on the command line.

The first argument is read by the shell into the parameter $1, The second
argument into $2, and so on. After $9, the arguments must be enclosed in
brackets, for example, ${10}, ${11}, ${12}.Some shells doesn't support this
method. In that case, to refer to parameters with numbers greater than 9, use
the shift command; this shifts the parameter list to the left. $1 is lost,while
$2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter
becomes $9 and can then be referred to.

Example:

#!/bin/bash
# Call this script with at least 3 parameters, for example
# sh scriptname 1 2 3

echo "first parameter is $1"

echo "Second parameter is $2"

echo "Third parameter is $3"

exit 0

Output:
[root@localhost ~]# sh parameters.sh 47 9 34

first parameter is 47

Second parameter is 9

Third parameter is 34

[root@localhost ~]# sh parameters.sh 4 8 3

first parameter is 4

Second parameter is 8

Third parameter is 3


 In addition to these positional parameters, there are a few other special
parameters used by shell.Their significance is noted bellow.

$* - It stores the complete set of positional parameters as a single string.
$@ - Same as $*, But there is subtle difference when enclosed in double quotes.
$# - It is set to the number of arguments specified.This lets you design scripts
     that check whether the right number of arguments have been entered.
$0 - Refers to the name of the script itself.

$?- Eixt status of last command
$$- PID of Current Shell
$!- PID of last background job.



Exit and Exit status of the command

exit 0   used when everything is went fine.
exit 1   used when something went wrong

test 100 -gt 99 ; echo $?

0                --------- result is success

test 98 -gt 99  ; echo $?

1                    ----- -result is failed

The LOGICAL OPERATORS && AND || -- CONDITIONAL EXECUTION

cmd1 && cmd2   (command2 is executed when command1 is success)
cmd1  || cmd2   (command2 is executed when command1 is failed.)

Example:
ls sample.txt && echo "sample.txt file  found"
ls sample.txt || echo "sample not found"






No comments:

Post a Comment