Argument in Script

Preview:

DESCRIPTION

This doc contains detail example of how to handle argument in shell script

Citation preview

http://www.bashguru.com/2009/11/how-to-pass-arguments-to-shell-script.htmlSunday, Noember 1!, 2009"ow to #ass $rguments to Shell Script Like UNIX commands, shell scripts also accept arguments from the command line.They can, therefore, run non interactively and be used with redirection andpipelines.Positional Parameters:rguments are passed from the command line into a shell program using thepositional parameters !" through to !#. $ach parameter corresponds to theposition of the argument on the command line.The first argument is read by the shell into the parameter !", The secondargument into !%, and so on. fter !#, the arguments must be enclosed inbrackets, for e&ample, !'"(), !'""), !'"%).*ome shells doesn+t support thismethod. In that case, to refer to parameters with numbers greater than #, usethe shift command, this shifts the parameter list to the left. !" is lost,while!% becomes !", !- becomes !%, and so on. The inaccessible tenth parameterbecomes !# and can then be referred to.Example:./0bin0bash. 1all this script with at least - parameters, for e&ample. sh scriptname " % -echo 2first parameter is !"2echo 2*econd parameter is !%2echo 2Third parameter is !-2e&it (Output:3root4localhost 56. sh parameters.sh 78 # -7first parameter is 78*econd parameter is #Third parameter is -73root4localhost 56. sh parameters.sh 7 9 -first parameter is 7*econd parameter is 9Third parameter is - In addition to these positional parameters, there are a few other specialparameters used by shell.Their significance is noted bellow.!: ; It stores the complete set of positional parameters as a single string.!4 ; *ame as !:, efers to the name of the script itself.Setting Values of Positional Parameters?ou can+t technically call positional parameters as shell variables because allvariable names start with a letter. @or instance you can+t assign values to !",!%.. etc. !"A"(( or !%Avenu is simply not done. There is one more way to assignvalues to the positional parameters, using the set command.$ set Helping hands are holier than praying lipsThe above command sets the value !" with BCelpingD , !% with BhandsD and so on.To verify, use echo statement to display their values.$ echo $1 $2 $3 $ $! $" $#Celping hands are holier than praying lips?ou can simply use !: or !4$ echo $$Celping hands are holier than praying lips$ echo $%Celping hands are holier than praying lips&sing Shift on Positional ParametersEe have used set command to set upto # words.

Recommended