Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of passing command line parameters in Shell script

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

How to pass command-line arguments in Shell scripts? This problem may be our daily study or work often see. I hope you learned something from this question. The following is the reference content brought to you by Xiaobian. Let's take a look together!

Command-line arguments are passed after the program name in a command-line operating system (such as DOS or Linux) and from the operating system to the program. Shell scripts also accept command-line arguments similar to the nix command. Command-line arguments are useful for passing input data to scripts at runtime.

To pass command-line arguments, we simply write them after the script name separated by spaces. All command-line arguments can be accessed using $to access their position numbers. Example of passing command-line arguments to a shell script.

# sh myScript.sh 10 red admin.net

sh:Linux shell

myScript.sh: Linux shell scripts

10: First command-line parameter accessible for $1

red: second command-line argument, accessible via $2

admin.net:$3 third command-line parameter accessible

Access command-line parameters with position numbers

As shown above, command-line arguments can be made between $1,$2,$3,... 9,$10,$100, etc. The maximum length of a command-line argument is not defined by the shell, but by the operating system, in kilobytes.

$*: Store all command-line arguments

$@: Store all command-line arguments

$: Store count of command-line arguments

$0: Store name of script itself

$1: Store the first command-line argument

$2: Store the second command-line argument

$3: Store the third command-line argument

$9: Store the 9th command-line argument

$10: Store the 10th command-line argument

$99: Store the 99th command-line argument

Example 1:

Create a shell script to print all parameters using the script name and the total number of parameters passed. Creating the script file myScript.sh requires the following.

#vim myScript.sh#!/ bin/bashecho Script Name: "$0"echo Total Number of Argument Passed: "$#"echo Arguments List -echo 1. $1echo 2. $2echo 3. $3echo All Arguments are: "$*"

execute scripts

# sh myScript.sh 10 rahul tecadmin.netScript Name: myScrit.shTotal Number of Argument Passed: 3Arguments List -1. 102. red3. admin.netAll Arguments are: 10 red admin.net

Example 2:

Create a loop with all parameters in the shell script. To do this, create a shell script file myscript2.sh that contains the following.

# vim myScript2.sh#!/ bin/bashfor i in "$@"do echo Argument: $idone

execute scripts

# ./ myScript2.sh 10 rahul tecadmin.netArgument: 10Argument: redArgument: admin.net

Access command-line parameters by shifting

We can also access command-line arguments by changing their position in the shell script. For example, access the first command-line argument with $1. Now changing the parameter to 1 means that the second parameter is now in the first position, the same third is in the second position, and so on.

Create the shell script myscript3.sh using the following and execute it with parameters. Now observe how to move parameters in shell scripts using the "shift " command.

#!/ bin/bashecho First Argument is: $1echo " >> Shifting argument position by 1"shift 1echo Now first Argument is: $1echo " >> Now Shifting position with 2"shift 2echo Now first Argument is: $1echo " >> Now shifting position with 4"shift 4echo Now first Argument is: $1

Execute the script and watch closely the output of $1 in the script.

[root@tecadmin ~]# sh myScrit3.sh a friend in need is a friend indeedFirst Argument is: a >> Shifting argument position by 1Now first Argument is: friend >> Now Shifting position with 2Now first Argument is: need >> Now shifting position with 4Now first Argument is: indeed Thank each bit for reading! After reading this, do you have a general idea of how to pass command-line arguments in Shell scripts? I hope the content of this article is helpful to everyone. If you want to know more about related articles, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report