Breaking News
Loading...
Wednesday, August 28, 2013

Basic Shell Script

7:51 AM

1. Shell Script : -  A shell script is simply a text file containing commands or statements to be executed.

Why we need shell scripts ?
  • Automating commonly used commands
  • Performing system administration and troubleshooting
  • Creating simple applications ,Manipulation  of text or files.
For example , suppose every morning when you log in , you perform the following operations:
  1. Check the system date
  2. Look at the calender for the current month.
  3. Check your mail.
  4. Display the list of logged in users. 
Instead of entering the commands to perform steps 1-4 individually , you create a shell script containing those commands. Every morning you could enter just one command to perform thses steps.In additions , one of the key benefits to use Linux is its powerful , yet simple commands.These simple commands may be put togther in a script to perform complex operations or to automate procedures such as adding batches of users.

2. Creating Shell Scripts

Step 1: Use a text editior such as vi to create a text file containing commands
(First Line contains the magic “shbang”  ---> #! ).

#! /bin/bash
- comment your scripts ! (Comments start with a #)

The first line in a shell scripts should contain 'magic' , which is commonly referred as the she-bang.

This tells the operating system which interpreter to use in order to excute the script. Some of the examples are :
#!/bin/bash
Used for Bash Scripts(Most common on Linux)
#!/bin/sh 
Used for Bourne shell scripts (Common on all Unix-like system)
#!/bin/csh 
Used for C shell scripts(common on BSD derived system)
#!/usr/bin/perl 
Used for perl scripts(an advanced scripting & programming language)
#!/usr/bin/python
Used for python scripts(an object oriented programming langauge)
Step 2:  Make the script executable : with chmod command
# chmod a+x scriptfile.sh     or  chmod 750 scriptfile.sh
Step 3:  To execute the new script :  Place the script file in a directory in excuteable path
OR
Specify the absolute or relative path to the script  on the command line.
# sh scriptfile.sh   OR
# ./scriptfile.sh
Generating Output

*  Use “echo” to generate simple output
e.g  # echo 'Welcome  To NextStep Linux World'
#echon -n “Please Enter the file name:”

*  Use “printf” to generate formated output
e.g # printf “The result of your exam %0.2f\n” $RESULT

(Syntax similar to C printf()function)


Handling Input

Use “read” to assign an input value to a shell variable :

e.g # echo -n “enter the filename: “
# read FILENAME

read takes a line from standard input and breaks it down into individual words.(Usually a word is defined as a character string surrounded by whitespace such as spaces and tabs).The way the shell 
interprets words may be changed by settings the IFS variable (e.g IFS=':' will tell the shell that words are separated by colons instead of white space). The First word is assigned to the first variable & second word is assigned to the second variable   and so on. If there are more words than the variables . The last variable is assigned all the remaining words.

Example :

#!/bin/bash
echo  -n 'Enter the name (first last):'
read FIRST LAST
printf “your first name is %s and your last name is %s \n”  $FIRST $LAST

The(-p) option is used to display a prompt string . Place the quotes around the string if you need to prompt the user with a multiple -word command.
Example:
#!/bin/bash
read -p “enter several values :” value1 value2 value3
echo “value1 is = $value1”
echo “value2 is = $value2”
echo “value3 is = $value3”
Exit Status
  • Command exit with an exit status
 0  for success , 1to 255 for failure , exit status of most recently executed command is kept in the $? variable just like return values from shell functions
Shell Script may set an exit status with the exit command : exit 1 (Indicates an error)
 
Upon  completion , every command returns an exit status . The exit status will be a number in the range of 0 to 255 and it indicates whether or not the command ran successfully. A general rule , an exit status of 0 indicates success and an exit value in the range 1 to 255 indicates failure. To See the exit status of the most recently executed commmand , echo the $? variable. 
Example
test@localhost:~$ ls -d /tmp/
/tmp/
 test@localhost:~$ echo $?
 0
 test@localhost:~$ ls -d /tpm
 ls: cannot access /tpm: No such file or directory
test@localhost:~$ echo $?

0 comments:

Post a Comment

 
Toggle Footer