[Bash]Parameters

Last update on July 17, 2009 08:17 AM by jak58
Published by deri58

[Bash]Parameters





Intro


You can provide a script on the command line and arguments also known as parameters for its implementation.
There are two categories of parameters: positional and special.

Positional parameters are only all the arguments situated under "settings" on the command line, the invocation of a script.

They are then assigned to the variables reserved 1,2,3 ... 9,10,11 ... and are displayed by using the expressions $ 1, $ 2 ...${ 10), $ (11) ...

Note: The Bourne shell is limited to the parameters from 0 to 9 only.

Example 1


Below is an example of a small script for showing you some of the arguments passed as parameters by position.
#! / bin / bash 
# Affiche_param.sh 

echo "The 1st parameter is: $ 1" 
echo "The 3rd parameter is: $ 3" 
echo "The 10th parameter is: $ (10)" 
echo "The 15th parameter is: $ (15)"

Simply invoke the script by passing a number of parameters: 
. / display_param.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 

The 1st parameter is: 1 
The 3rd parameter is: 3 
The 10th parameter is: 10 
The 15th parameter is: 15 
or: 
. / display_param.sh one 2 three 4 5 6 7 8 9 ten 11 12 13 14 fifteen 16 17 
The 1st parameter is a 
The 3rd parameter is three 
The 10th parameter is ten 
The 15th parameter is fifteen 

If certain parameters contain special characters or spaces, it 
must then "quote": 
. / display_param.sh a 2 "3rd" 4 5 6 7 8 9 ten 11 12 13 14 15th 16 17 
The 1st parameter is a 
The 3rd parameter is the 3rd 
The 10th parameter is ten 
The 15th parameter is the 15th 

Special parameters are reserved variables that allow for some preferencial treatments on the parameters themselves. 
These parameters are: 
$ 0 Contains the name of the script as used 
$ * All of the parameters in the form of a single argument 
$ @ All arguments, an argument by setting 
$ # The number of parameters passed to the script 
$? The return code of last command 
PID $ $ The su shell script that runs the 
$! The PID of last process started in background 

Example 2


Here's another little script implementing all special settings seen above.
#! / bin / bash 
# display_param_2.sh 

# Display script name su 
echo "The name of my script is: $ 0" 
# Display the number of parameters 
echo "You spent $ # parameters" 
# List of parameters (one argument) 
for param in "$ *" 
do 
echo "The parameters (a single argument): $ param" 
done 
# List of parameters (one parameter per argument) 
echo "The parameters (a parameter argument):" 
for param in "$ @" 
do 
Echo-e "\ tParameter: $ param" 
done 
# Display process 
echo "The PID of the shell running the script is: $ $" 
# Execute command running in background 
sleep 100 & 
# Display of the process launched in the background 
echo "The PID of the last command executed in the background is: $!" 
# Display the return code of last command echo " 
echo "return code from the preceding command is: $?" 
# Generate an error 
echo "Generating a mistake ..." 
# Display the wrong order 
echo "ls / etc / password 2> / dev / null" 
ls / etc / password 2> / dev / null 
# Display the return code of last command 
echo "return code from the preceding command is: $?" 
exit
What gives with the following invocation: 
./display_param_2.sh 1 2 3 Four 5 Six 

The name of my script is: ./display_param_2.sh 
You've spent 6 parameters 
The parameters (a single argument): 1 2 3 Four 5 Six 
The parameters (a parameter argument): 
         Parameter: 1 
         Parameter: 2 
         Parameter: 3 
         Parameter: four 
         Parameter: 5 
         Setting: six 
The PID of the shell running the script is: 6165 
The PID of the last command executed in the background is: 6166 
The return code from the preceding command is: 0 
Generating a mistake ... 
ls / etc / password 2> / dev / null 
The return code from the preceding command is: 1 

Initialize parameters


- The "set" --

It is possible to directly assign parameters to the shell with the command "set".
A simple command such as:
set param1 param2 param3
automatically initialize the positional parameters "$ 1, $ 2, $ 3 with the values" param1, param2, param3 ", thus deleting the old values if they existed. Special parameters "#, * @ and" are automatically updated accordingly.

Examples


$ Set param1 param2 param3 
$ Echo "Number of parameters: $ #" 
   Number of parameters: 3 
$ Echo "The second parameter is: $ 2" 
   The second parameter is: param2 
$ Echo "The parameters are: $ @" 
   The parameters are: param1 param2 param3 

$ Set fishing apple 
$ Echo "Number of parameters: $ #" 
   Number of parameters: 2 
$ Echo "The parameters are: $ @" 
   The parameters are: fishing apple 

This feature can be useful in file processing line 
by line to isolate each word (field), and to format the output. 
$ IFS =":"; set $ (grep $ USER / etc / passwd) 
$ Echo-e "login: \ t $ 1 \ nNom: \ t $ 5 \ nID: \ t $ 3 \ ngroup: \ t $ 4 \ nShell: \ t $ 7" 

Login: JM 
Name: John Mackay
ID: 500 
Group: 500 
Shell: / bin / bash 

- The "shift" --

The internal command "shift" as it allows to shift the parameters.
The value of 1st parameter ($ 1) is replaced by the value of the 2nd parameter ($ 2), the 2nd parameter ($ 2) by the 3rd parameter ($ 3), etc ...
It may be mentioned as an argument (shift n) the number of steps (position) to be offset settings.

Example 3


This is an implementation of the use of the internal shift. 
#! / bin / bash 
# Decale_param.sh 

echo 
echo "Number of parameters: $ #" 
echo "The 1st parameter is: $ 1" 
echo "The 3rd parameter is: $ 3" 
echo "The 6th parameter is: $ 6" 
echo "The 10th parameter is: $ (10)" 
echo "=============================================" 
echo "Offset of a step with the \" shift \ "" 
shift 
echo "Number of parameters: $ #" 
echo "The 1st parameter is: $ 1" 
echo "The 3rd parameter is: $ 3" 
echo "The 6th parameter is: $ 6" 
echo "The 10th parameter is: $ (10)" 
echo "=============================================" 
echo "Offset of four steps with the \" shift 4 "" 
shift 4 
echo "Number of parameters: $ #" 
echo "The 1st parameter is: $ 1" 
echo "The 3rd parameter is: $ 3" 
echo "The 6th parameter is: $ 6" 
echo "The 10th parameter is: $ (10)" 
echo 
And the result: 
. / decal_param.sh 1 2 3 4 5 6 7 8 9 10 

Number of parameters: 10 
The 1st parameter is: 1 
The 3rd parameter is: 3 
The 6th parameter is: 6 
The 10th parameter is: 10 
============================================= 
Lag a step with "shift" 
Number of parameters 9 
The 1st parameter is: 2 
The 3rd parameter is: 4 
The 6th parameter is: 7 
The 10th parameter is: 
============================================= 
Lag of four steps with the command "shift 4" 
Number of parameters: 5 
The 1st parameter is: 6 
The 3rd parameter is: 8 
The 6th parameter is: 
The 10th parameter is:
Best answers for « Parameters » in :
BIOS optimisation Show What is the BIOS? The BIOS (Basic Input/Output System) is a small memory chip located on the motherboard containing data that define the system parameters. As some BIOS data are written in a ROM, it is not possible to change them, however, certain...
Display Oracle parameters Show Display Oracle parameters In addition to the file init.ora, it is possible for you to consult the database to be check out parameters such as max_open_cursors ,size of a block ... Simply run the following command: SELECT name,...
Managing SQL parameters Show Managing SQL parameters SQL has a number of parameters at Oracle level that can be viewed it through the command: SHOW ALL To change the value of a parameter you can use the command: SET NAME_PARAM VALUE
Error Codes in Windows ShowError Codes in Windows The list below details the error codes displayed in the dialog boxes in Windows: Code Description _________________________________________________________________________ 1 Incorrect function....
Create and Open a RAR file ShowCreate and Open a RAR file *How to create a RAR file *How to open a RAR file RAR is an archive file format that is used during data compression, error recovery and file spanning. RAR is the acronym for Roshal Archive, inspired by...
Make a portable application with Reg Rapper ShowMake a portable application with Reg Rapper What is Reg Rapper? Limitations of the program Saving the Parameters How to create the parameters What is Reg Rapper? Reg Rapper will allow you to save the settings for a program and...
Download MP3 CD Extractor ShowMp3 EXTRACTOR CD is an application of extraction of audio lane. He inserts a simple interface and offers the definition of parameters of encoding. He allows to extract one or several audio lanes. Lanes are then converted to format mp3 or wav. Ripples...
Download Magic Transfer ShowThe most part of the users have several workstations, notably in the work and in the home. But it is not easy to have exactly the parameters which they have in two different computers. It is even more difficult to have these data in day. Magic...
Book disk ShowSystem disk A system disk, also sometimes called a boot disk, is a disk that will allow you to start a computer when the operating system no longer responds, as the result of a virus for example. This disk contains special information that makes it...
Rundll32 - rundll32.exe Showrundll32 - rundll32.exe rundll32.exe (rundll32 stands for Run a DLL as a 32-bit application) is a Windows NT/2000/XP generic process used for loading dynamic link libraries (DLLs) in memory so that other programs can use them. The file that...
Linux - The shell ShowIntroduction to the shell The command interpreter is the interface between the user and the operating system, hence the name "shell". The shell therefore acts as an intermediary between the operating system and the user thanks to command lines...