Skip to content Skip to sidebar Skip to footer

How to Pass Value to Read Bash

Parsing and Passing of Arguments into bash scripts/ beat scripts is quite similar to the way in which nosotros pass arguments to the functions within Bash scripts. We'll see the actual process of passing on the arguments to a script and also look at the style to access those arguments inside the script.

Passing arguments before running

We can laissez passer parameters just after the proper name of the script while running the bash interpreter command. You can laissez passer parameters or arguments to the file. But the command for running the script normally by adding the value of the parameters direct to the script. Every parameter is a space-separated value to laissez passer to the crush script.

bash scriptname.sh        

The in a higher place command volition just run the script without passing the parameters.

Whereas the command beneath will laissez passer the arguments to the script.

bash scriptname.sh parameter1 parameter2 parameter3 nth-parameter

Running fustigate script with passing parameters

The above screenshot displays the parameters passed to the script, how nosotros'll exercise that, which we'll explain in the next section. Only right now we can see we have passed in the parameters from exterior of the script using bash environment variables. You can even use strings and other data types but beware of any whitespace. White space will make the variable a split parameter. And so, for strings particularly, be careful to strictly environment them with quotation marks.

Detecting Command Line Arguments

At present, we'll encounter how we admission those parameters inside of the script. We'll use the number of the parameters passed in the order i.e for the first parameters passed, we'll parse(access) the parameter by using $i as the variable. The first parameter is stored in the $one variable. Furthermore, you can assign this variable to whatsoever other user-defined variable you like. For the nth parameter passed, you lot can use $northward to admission that particular parameter. Here, the variable name starts with 1 because the filename/ script proper noun is the 0th parameter. If you have more than 9 parameters, make sure to use { } around the number as without the parenthesis, bash will only see $10 as $1 and exclude the 0, then use ${10} and and then on instead of just $10.

#!/bin/bash  repeat "1st parameter = $1 " echo "2nd Parameter = $2 "

The in a higher place script can admission the parameters from the command line/ shell using the positional parameters, which are 1, ii, 3, and so on.

Accessing the arguments from the script.

As you tin encounter, we have used {} to access the parameter variable numbers from ten onwards. The script can be used for loops and while loops to iterate over the parameters, but nosotros will discuss it in further sections.

Assign Provided Arguments To Bash Variable

We can too assign it to other custom variables to brand the script more dynamic and mold it co-ordinate to the needs. Though the above script when run will only print two parameters, surely you tin admission more than parameters using the variable every bit the order of parameters in numbers. The script can access the positional variables from the command line and use them in the required places wherever needed within the script.

#!/bin/bash  a=$one b=$2 p=$(($a*$b)) echo "The production of $a and $b = $p"

Assign Provided Arguments To Fustigate Variable

The above script accesses the positional parameters i.east $1 and $two passed into the script and stores the user-defined variables to access them later and modify them accordingly.  We can also access more parameters using iterative methods as we'll meet in the upcoming sections.

We also accept the power to check for any NULL or empty parameters passed using the -z or -n flags. From this, nosotros can verify whether the parameters were passed or not.

#!/bin/bash  if [[ -z $1 ]]; then      echo "No parameter passed." else     echo "Parameter passed = $1" if

Checking for positional parameters passed in or not.

With this script, we tin can discover whether any positional parameters were passed in or nothing was passed.  The -z flag checks for any NULL or uninitialized variables in BASH. The -z flag returns truthful if the variable passed is NULL or uninitialized. Hence, we tin make use of basic If-else statements to detect the parameters passed.

Nosotros can also use -n flag which returns truthful if no parameters are passed, so nosotros have to make utilize of ! to reverse the status.

Such as follows:

#!/bin/fustigate  if [[ ! -north $1 ]]; then      echo "No parameter passed." else     echo "Parameter passed = $ane" if

This script volition also give the same output as well, but nosotros are making utilize of -n flag instead of -z.

Reading Multiple Arguments with For or While loop

Nosotros tin use "@" variable to admission every parameter passed to the script via the command line. It is a special variable that holds the array of variables in BASH. In this case, we are using it lone, so it contains the assortment of positional parameters passed in. We can use it to iterate over the parameters passed using loops or while loop too.

#!/bin/bash  for i in $@ exercise          echo -e "$i\n" done

Using loops and @ variable to access the parameters as array elements.

We used a range-based for loop to iterate over till there are elements in the @ array. We simply iterate over the array and print the chemical element. We tin just assign information technology, modify the values, and make the required changes to the parameters and arguments to achieve the desired outcome from the script.

OR

We tin likewise impress the arguments using the while loop and the ecology variables of Fustigate.

#!/bin/bash  i=$(($#-1)) while [ $i -ge 0 ]; practise     echo ${BASH_ARGV[$i]}     i=$((i-1)) washed

Using while loop to iterate over the passed parameters.

Nosotros are using the variable '#' as it holds the number of parameters passed in. We initialize the number of parameters and take away i every bit we are going to use an assortment to iterate over information technology. Then, as usual, the array's index starts from 0. Equally this assortment is initialized from the final element or parameter passed, we need to decrement the counter until 0 to print every parameter in the order information technology is passed. We just employ the BASH_ARGV array to admission the parameters and print its value. Likewise, at every iteration, we decrease the value of i- the iterator or counter by ane using the arithmetics double braces.  From this, we only print every parameter passed to the script using a while loop as shown from the output screenshot.

Reading With Parameter Names

Using getopts to parse arguments and parameters

We can use the getopts plan/ command to parse the arguments passed to the script in the control line/ last by using loops and switch-case statements.

#!/bin/fustigate  while getopts north:c: option do      case "${pick}"         in         n)nation=${OPTARG};;         c)code=${OPTARG};;     esac done  echo "Nation : $nation" echo "code   : $code"

Using getopts to parse arguments and parameters

Using getopts, we can assign the positional arguments/ parameters from the command line to the bash variables directly. This allows the states to manage the parameters nicely and in a systematic fashion. In the above script, we accept used two arguments to store the bash variables using the getopts syntax, while loops and switch-example statements.

Printing Values of All Arguments

We can print the arguments passed to the script by a elementary powerful variable '@' which stores all the parameters passed.

#!/bin/fustigate  repeat "The arguments passed in are : $@"

Printing Values of All Arguments

Accessing the number of Parameters passed

We can also use the variable '#' to admission the number of parameters passed from the command line. The # variable basically contains the number of parameters/ arguments which are passed into the script.

#!/bin/fustigate  echo "The number of arguments passed in are : $#"

Accessing the number of Parameters passed

The following were the process and specification of passing and parsing the variables in the bash script. The logic of shifting and making modifications to the variables is in the hands of the user. This was just a sit-in of passing in and parsing downwardly the arguments from the command line to the script to make them more than dynamic.

mccallburnournswes.blogspot.com

Source: https://www.geeksforgeeks.org/how-to-pass-and-parse-linux-bash-script-arguments-and-parameters/

Post a Comment for "How to Pass Value to Read Bash"