如何传递和解析Linux Bash脚本参数和参数

Bash is a powerful scripting language provides by various Linux distributions, Unix and BSD. If we need to make our script dynamic we generally use arguments. Arguments are provided to the script through the command line. In this tutorial, we will examine different use cases of argument passing and examples.

Bash是由各种Linux发行版,Unix和BSD提供的功能强大的脚本语言。 如果我们需要使脚本动态化,则通常使用参数。 通过命令行将参数提供给脚本。 在本教程中,我们将研究参数传递和示例的不同用例。

句法 (Syntax)

In order to use externally provided values inside the bash script, we should provide them after the script name. Following syntax assumes the script is executable.

为了在bash脚本中使用外部提供的值,我们应该在脚本名称之后提供它们。 以下语法假定脚本是可执行的。

myscript.sh PARAM1 PARAM2 ...

Or we can use bash in order to interpret our script like below and provide parameters.

或者我们可以使用bash来解释我们的脚本,如下所示并提供参数。

bash myscript.sh PARAM1 PARAM2 ...

提供论据 (Provide Arguments)

We have learned the syntax of the arguments. In this example, we will look use cases about argument passing. We will provide two arguments for example.

我们已经学习了参数的语法。 在此示例中,我们将研究有关参数传递的用例。 例如,我们将提供两个参数。

$ myscript.sh 1 3

If we need to provide a string with multiple words we can use single or double-quotes. In this example, we will prove 10 and Hello Poftut as two separate arguments.

如果需要提供包含多个单词的字符串,则可以使用单引号或双引号。 在此示例中,我们将证明10Hello Poftut是两个单独的参数。

$ myscript.sh 10 "Hello Poftut"

读取脚本内部参数 (Read Argument Inside Script)

Now the real work starts now. We have learned how to provide arguments from the shell or command line. In this part, we will look at how to read them inside the script. Bash provides $1 , $2 ,  … like usage for reading input arguments inside script. the first argument can be accessed from the variable name $1 , the second one $2 and so … In this example, we will provide two-argument Hello and Poftut to script.

现在,真正的工作开始了。 我们已经学习了如何从Shell或命令行提供参数。 在这一部分中,我们将研究如何在脚本中读取它们。 Bash提供了$1$2 ……来读取脚本中的输入参数。 可以从变量名$1访问第二个参数,第二个变量从$2等等访问……在此示例中,我们将为脚本提供两个参数的HelloPoftut

#!/bin/bash 
 
echo $1 $2
Read Argument Inside Script
Read Argument Inside Script
读取脚本内部参数

检测命令行参数(Detecting Command Line Arguments)

If some command-line arguments are provided for our script we can use the $1, $2, $3, … variables in order to detect command line arguments. We can use the if statement to check specific input like below. Below we will check the positional parameter 1 and if it contains some value the value will be printed to the screen.

如果为脚本提供了一些命令行参数,则可以使用$ 1,$ 2,$ 3,...变量来检测命令行参数。 我们可以使用if语句来检查特定的输入,如下所示。 在下面,我们将检查位置参数1,如果它包含某个值,该值将被打印到屏幕上。

#!/bin/bash

if [ "$1" != "" ]; then
    echo "Positional parameter 1 contains value"
echo $1 else echo "Positional parameter 1 is empty" fi

将提供的参数分配给Bash变量 (Assign Provided Arguments To Bash Variable)

Another use case for bash variables is assigning them new variables with meaningful names. This will give us the ability to change provided values and use them properly with their names. In this example, we will put Hello and Poftut into variables named salute and name .

bash变量的另一个用例是为它们分配具有有意义名称的新变量。 这将使我们能够更改提供的值,并正确使用其名称。 在此示例中,我们将HelloPoftut放入名为salutename变量中。

#!/bin/bash 
 
salute=$1 
name=$2 
 
echo $salute $name
Assign Provided Arguments To Bash Variable
Assign Provided Arguments To Bash Variable
将提供的参数分配给Bash变量

使用For或While读取多个参数(Read Multiple Arguments with For or While)

Some times some arguments will not fit our condition and we may need a lot of arguments to provide and use them in an iterative way. In this condition, we can use bash loop mechanisms for and while . We can iterate over given arguments like an array or list with for and while . In this example, we will iterate over provided arguments and print them to the shell. We will use [email protected] to specify the list of provided arguments and put each item into a variable named var in each step.

有时某些参数不适合我们的条件,我们可能需要大量参数才能以迭代方式提供和使用它们。 在这种情况下,我们可以for and while使用bash循环机制。 我们可以使用forwhile遍历数组或列表等给定参数。 在此示例中,我们将遍历所提供的参数并将其打印到外壳中。 我们将使用[email protected]来指定所提供参数的列表,并在每个步骤中将每个项目放入名为var的变量中。

#!/bin/bash 
 
for var in "[email protected]" 
do 
  echo $var 
done
Read Multiple Arguments with For or While
Read Multiple Arguments with For or While
使用For或While读取多个参数

读取参数名称(Read With Parameter Names)

As regular Linux applications, we can use parameter names for arguments and parse them accordingly. We will use getops function which will parse provided options in an array. In this example, we will use while loop with case structure. We will parse the provided parameter names u which is username and p password.

作为常规Linux应用程序,我们可以使用参数名称作为参数,并相应地解析它们。 我们将使用getops函数,该函数将解析数组中提供的选项。 在这个例子中,我们将使用while循环和case结构。 我们将解析提供的参数名称u ,即用户名和p password。

#!/bin/bash 
 
while getopts u:p: option 
do 
 case "${option}" 
 in 
 u) USER=${OPTARG};; 
 p) PASSWORD=${OPTARG};; 
 esac 
done 
 
echo "User:"$USER 
echo "Password:"$PASSWORD
Read With Parameter Names
Read With Parameter Names
读取参数名称

使用getopts函数读取Bash参数(Read Bash Parameters with getopts Function)

Bash provides different functions to make reading bash input parameters. getopts is a function where it can be used to read specified named parameters and set into the bash variables in a easy way. getopst will read every input parameter and look for the options to match and if match occrus the parameter value set to given variable name.

Bash提供了不同的功能来读取bash输入参数。 getopts是一个函数,可用于读取指定的命名参数并以简单的方式将其设置为bash变量。 getopst将读取每个输入参数并查找要匹配的选项,如果匹配则将参数值设置为给定的变量名。

while getopts u:a:l: option
do
case "${option}"
in
u) USER=${OPTARG};;
a) AGE=${OPTARG};;
l) LOCATION=${OPTARG};;
esac
done


echo "Given user name is: "$USER
echo "Given age is: "$DATE
echo "Given location is: "$LOCATION

We can run this script like below but before running is we have to make the script runable with the chmod command.

我们可以像下面那样运行该脚本,但是在运行之前,我们必须使用chmod命令使该脚本可运行。

$ chmod u+x getopts_example.sh

$ ./getopts_example.sh -u ismail -a 36 -l Turkey
Read Bash Parameters with getopts Function
使用getopts函数读取Bash参数

获取传递的参数数量(Get The Number Of Arguments Passed)

Bash provides the number of the arguments passed with the $# variable. We can get the number of the arguments passed and use for different cases where below we will print the number of the arguments passed to the terminal.

Bash提供了与$#变量一起传递的参数数量。 我们可以获取传递的参数数量,并用于不同的情况,在下面的情况下,我们将打印传递给终端的参数数量。

#!/bin/bash

echo "You provided $# arguments"
Get The Number Of Arguments Passed
获取传递的参数数量

打印所有参数的值(Print Values Of All Arguments)

We can also print all provided arguments or values with a single variable [email protected].

我们还可以使用单个变量[email protected]打印所有提供的参数或值。

#!/bin/bash

echo "All Arguments values:" [email protected]
Print Values Of All Arguments
打印所有参数的值
LEARN MORE  How To Redirect Stderr To Stdout In Linux Bash?
了解更多如何在Linux Bash中将Stderr重定向到Stdout?

翻译自: https://www.poftut.com/how-to-pass-and-parse-linux-bash-script-arguments-and-parameters/

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值