How to Pass Arguments to a Bash-Script Using Linux

You can write a bash script such that it receives arguments specific when the script is referred to as from the command line. This technique is used whilst

You can write a bash script such that it receives arguments specific when the script is referred to as from the command line. This technique is used whilst a script has to carry out a barely specific feature depending on the values of enter parameters (the arguments).

As an example, you could have a script known as “stats.Sh” that plays a specific operation on a file, consisting of counting its words. If you need that allows you to use that script on many documents, it’s far nice to pass the record name as an issue, so that you can use the identical script for all of the files to be processed.

As an example, if the call of the report to be processed is “songlist”, you will enter the following command line:

sh songlist

Arguments are accessed internal a script using the variables $1, $2, $3, and so forth., wherein $1 refers to the first argument, $2 to the second argument, and so on. That is illustrated in the following example:

wc $FILE1

For clarity, assign a variable with a descriptive name to the value of the first argument ($1), after which name the word rely application (wc) in this variable ($file1).

When you have a variable wide variety of arguments, you may use the “[email protected]” variable, that’s an array of all of the input parameters. This indicates you can use a for-loop to iteratively method every one, as illustrated in the following instance:

for FILE1 in "[email protected]"
wc $FILE1

Right here is an instance of how to call this script with arguments from the command line:

sh songlist1 songlist2 songlist3

If an argument has spaces, you need to enclose it with single charges. For instance:

sh 'songlist 1' 'songlist 2' 'songlist 3'

Often a script is written such that the user can pass in arguments in any order the use of flags. With the flags approach, you may additionally make a number of the arguments optional.

READ  How to Use iTunes Using Linux

Permit say you have a script that retrieves statistics from a database primarily based on particular parameters, which include “username”, “date”, and “product”, and generates a document in a distinct “layout”. Now you want to jot down your script so you can bypass in those parameters while the script is known as. It’d appear like this:

makereport -u jsmith -p notebooks -d 10-20-2011 -f pdf

Bash permits this functionality with the “getopts” feature. For the above example, you could use getopts as follows:

while getopts u:d:p:f: option
case "${option}"

This is some time-loop that uses the “getopts” function and a so-called “optstring”, in this situation “u:d:p:f:”, to iterate thru the arguments. The whilst-loop walks thru the optstring, which contains the flags that may be used to bypass arguments, and assigns the argument value furnished for that flag to the variable “choice”. The case-assertion then assigns the price of the variable “option” to a international variable that may used after all of the arguments had been examine.

The colons in the optstring imply that values are required for the corresponding flags. In the above instance all flags are observed with the aid of a colon: “u:d:p:f:”.

This means, all flags want a fee. If, for instance, the “d” and “f” flags were not predicted to have a cost, the optstring could be “u:dp:f”.

A colon at the start of the optstring, for instance “:u:d:p:f:”, has a completely distinctive meaning. It allows you to handle flags that are not represented within the optstring. If so the value of the “option” variable is about to “?” and the fee of “optarg” is ready to the sudden flag. The allows you to display a appropriate errors message informing the user of the mistake.

READ  LINUX - Compress a folder with tar?

Arguments that aren’t preceded with the aid of a flag are left out with the aid of getopts. If flags distinctive inside the optstring are not furnished while the script is called, not anything takes place, except you in particular deal with this situation on your code.

Any arguments no longer handled through getops can nevertheless be captured with the regular $1, $2, and so on. Variables.

About the author

Wikitechy Editor

Wikitechy Editor

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

Add Comment

Click here to post a comment