如何将命令行参数传递给 shell 脚本?

Pau*_*aul 414 command-line shell-script function

我知道 shell 脚本只是运行命令,就好像它们是在命令提示符下执行的一样。我希望能够像运行函数一样运行 shell 脚本......也就是说,将输入值或字符串带入脚本中。我该如何处理?

Bru*_*ger 308

shell命令和该命令的任何参数显示为编号的shell变量:$0有命令本身,类似的字符串值script./script/home/user/bin/script或什么的。任何参数显示为"$1""$2""$3"等等。参数的数量在 shell 变量中"$#"

处理这个问题的常见方法包括 shell 命令getoptsshift. getopts很像Cgetopt()库函数。shift移动$2to $1$3to 等的值$2$#递减。代码最终会查看 的值"$1",使用case...esac来决定一个动作,然后使用 ashift来移动$1到下一个参数。它只需要检查$1,也许$#

  • C `getopt()` 是一个非常成熟的标准,但是 `getopt` 可执行文件在发行版/平台上并不相同。我现在是一个完全转换的 `getopts` 用户,因为它是 POSIX 标准。它也适用于`dash`,这是我首选的脚本外壳 (7认同)
  • 另请注意,“$@”包含所有参数,如[此答案](/sf/answers/337724621/)中所述。 (5认同)

Kev*_*vin 279

您可以访问传递带参数$n,其中n是参数号码- 1, 2, 3, ...。您可以像使用任何其他命令一样传递参数。

$ cat myscript
#!/bin/bash
echo "First arg: $1"
echo "Second arg: $2"
$ ./myscript hello world
First arg: hello
Second arg: world
Run Code Online (Sandbox Code Playgroud)


Raf*_*rsk 98

在 bash 脚本上,我个人喜欢使用以下脚本来设置参数:

#!/bin/bash

helpFunction()
{
   echo ""
   echo "Usage: $0 -a parameterA -b parameterB -c parameterC"
   echo -e "\t-a Description of what is parameterA"
   echo -e "\t-b Description of what is parameterB"
   echo -e "\t-c Description of what is parameterC"
   exit 1 # Exit script after printing help
}

while getopts "a:b:c:" opt
do
   case "$opt" in
      a ) parameterA="$OPTARG" ;;
      b ) parameterB="$OPTARG" ;;
      c ) parameterC="$OPTARG" ;;
      ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
   esac
done

# Print helpFunction in case parameters are empty
if [ -z "$parameterA" ] || [ -z "$parameterB" ] || [ -z "$parameterC" ]
then
   echo "Some or all of the parameters are empty";
   helpFunction
fi

# Begin script in case all parameters are correct
echo "$parameterA"
echo "$parameterB"
echo "$parameterC"
Run Code Online (Sandbox Code Playgroud)

使用这种结构,我们不依赖参数的顺序,因为我们为每个参数定义了一个关键字母。另外,每次参数定义错误都会打印帮助函数。当我们有很多需要处理不同参数的脚本时,它非常有用。它的工作原理如下:

#!/bin/bash

helpFunction()
{
   echo ""
   echo "Usage: $0 -a parameterA -b parameterB -c parameterC"
   echo -e "\t-a Description of what is parameterA"
   echo -e "\t-b Description of what is parameterB"
   echo -e "\t-c Description of what is parameterC"
   exit 1 # Exit script after printing help
}

while getopts "a:b:c:" opt
do
   case "$opt" in
      a ) parameterA="$OPTARG" ;;
      b ) parameterB="$OPTARG" ;;
      c ) parameterC="$OPTARG" ;;
      ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
   esac
done

# Print helpFunction in case parameters are empty
if [ -z "$parameterA" ] || [ -z "$parameterB" ] || [ -z "$parameterC" ]
then
   echo "Some or all of the parameters are empty";
   helpFunction
fi

# Begin script in case all parameters are correct
echo "$parameterA"
echo "$parameterB"
echo "$parameterC"
Run Code Online (Sandbox Code Playgroud)

  • 凉爽的。它对我有用。但是,key 只需要一个字符。我浪费了将近一个小时来弄清楚这一点。 (4认同)

Bal*_*man 36

$/shellscriptname.sh argument1 argument2 argument3 
Run Code Online (Sandbox Code Playgroud)

您还可以将一个 shell 脚本的输出作为参数传递给另一个 shell 脚本。

$/shellscriptname.sh "$(secondshellscriptname.sh)"
Run Code Online (Sandbox Code Playgroud)

在 shell 脚本中,您可以使用数字访问参数,例如$1第一个参数和$2第二个参数等等。

更多关于 shell 参数


use*_*344 32

表格

$ ./script.sh "$@" 
Run Code Online (Sandbox Code Playgroud)

是最方便的argv。arg 分隔符是空格,但可以更改。不记得有多手忙脚乱了。然后使用

 $1, $2, shift, if [ $# -ne 3 ] 
Run Code Online (Sandbox Code Playgroud)

来控制流量。如果case ... esac足够的话,我通常不会接受 getopts 。


vim*_*ude 19

./myscript myargument
Run Code Online (Sandbox Code Playgroud)

myargument变成$1里面myscript


abc*_*abc 10

在 shell 脚本中;它成为变量名 $1 。第二个字变成变量名 $2 等等。

cat << 'EOF' > test
echo "First arg: $1"
echo "Second arg: $2"
echo "List of all arg: $@"
EOF
sh test hello world
Run Code Online (Sandbox Code Playgroud)

更多信息可以在http://heirloom.sourceforge.net/sh/sh.1.html的 shell (sh) 手册中找到

  • 欢迎来到 U&amp;L,这几乎不会为已经给出的答案添加任何内容。 (4认同)