在 Bash 中将命令行参数转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12711786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert command line arguments into an array in Bash
提问by Suman
How do I convert command-line arguments into a bash script array?
如何将命令行参数转换为 bash 脚本数组?
I want to take this:
我想拿这个:
./something.sh arg1 arg2 arg3
and convert it to
并将其转换为
myArray=( arg1 arg2 arg3 )
so that I can use myArray for further use in the script.
这样我就可以使用 myArray 在脚本中进一步使用。
This previous SO post comes close, but doesn't go into how to create an array: How do I parse command line arguments in Bash?
以前的 SO 帖子很接近,但没有介绍如何创建数组:如何在 Bash 中解析命令行参数?
I need to convert the arguments into a regular bash script array; I realize I could use other languages (Python, for instance) but need to do this in bash. I guess I'm looking for an "append" function or something similar?
我需要将参数转换为常规的 bash 脚本数组;我意识到我可以使用其他语言(例如 Python),但需要在 bash 中执行此操作。我想我正在寻找“追加”功能或类似的东西?
UPDATE: I also wanted to ask how to check for zero arguments and assign a default array value, and thanks to the answer below, was able to get this working:
更新:我还想问如何检查零参数并分配默认数组值,感谢下面的答案,能够使这个工作:
if [ "$#" -eq 0 ]; then
myArray=( defaultarg1 defaultarg2 )
else
myArray=( "$@" )
fi
回答by kojiro
Actually your command line arguments are practicallylike an array already. At least, you can treat the $@
variable much like an array. That said, you can convert it into an actual array like this:
实际上,您的命令行参数实际上已经像一个数组。至少,您可以$@
像对待数组一样对待变量。也就是说,您可以将其转换为这样的实际数组:
myArray=( "$@" )
If you just want to type some arguments and feed them into the $@
value, use set
:
如果您只想输入一些参数并将它们输入到$@
值中,请使用set
:
$ set -- apple banana "kiwi fruit"
$ echo "$#"
3
$ echo "$@"
apple banana kiwi fruit
Understanding how to use the argument structure is particularly useful in POSIX sh, which has nothing else like an array.
了解如何使用参数结构在 POSIX sh 中特别有用,因为它没有数组之类的东西。
回答by Nahuel Fouilleul
Maybe this can help:
也许这可以帮助:
myArray=("$@")
also you can iterate over arguments by omitting 'in':
您也可以通过省略“in”来迭代参数:
for arg; do
echo "$arg"
done
will be equivalent
将是等价的
for arg in "${myArray[@]}"; do
echo "$arg"
done
回答by Nahuel Fouilleul
Actually the list of parameters could be accessed with $1 $2 ...
etc.
Which is exactly equivalent to:
实际上可以使用$1 $2 ...
等访问参数列表。
这完全等同于:
${!i}
So, the list of parameters could be changed with set,
and ${!i}
is the correct way to access them:
因此,可以使用 set 更改参数列表,
并且${!i}
是访问它们的正确方法:
$ set -- aa bb cc dd 55 ff gg hh ii jjj kkk lll
$ for ((i=0;i<=$#;i++)); do echo "$#" "$i" "${!i}"; done
12 1 aa
12 2 bb
12 3 cc
12 4 dd
12 5 55
12 6 ff
12 7 gg
12 8 hh
12 9 ii
12 10 jjj
12 11 kkk
12 12 lll
For your specific case, this could be used (without the need for arrays), to set the list of arguments when none was given:
对于您的特定情况,可以使用它(不需要数组),在没有给出参数时设置参数列表:
if [ "$#" -eq 0 ]; then
set -- defaultarg1 defaultarg2
fi
which translates to this even simpler expression:
这转化为这个更简单的表达:
[ "$#" == "0" ] && set -- defaultarg1 defaultarg2
回答by tu?rul altun
Here is another usage :
这是另一种用法:
#!/bin/bash
array=( "$@" )
arraylength=${#array[@]}
for (( i=0; i<${arraylength}; i++ ));
do
echo "${array[$i]}"
done
回答by runlevel0
Easier Yet, you can operate directly on $@
;)
更简单,您可以直接操作$@
;)
Here is how to do pass a a list of args directly from the prompt:
以下是如何直接从提示中传递 args 列表:
function echoarg { for stuff in "$@" ; do echo $stuff ; done ; }
echoarg Hey Ho Lets Go
Hey
Ho
Lets
Go
回答by Kirin
Side-by-side view of how the array and $@ are practically the same.
数组和 $@ 如何实际上相同的并排视图。
Code:
代码:
#!/bin/bash
echo "Dollar-1 : "
echo "Dollar-2 : "
echo "Dollar-3 : "
echo "Dollar-AT: $@"
echo ""
myArray=( "$@" )
echo "A Val 0: ${myArray[0]}"
echo "A Val 1: ${myArray[1]}"
echo "A Val 2: ${myArray[2]}"
echo "A All Values: ${myArray[@]}"
Input:
输入:
./bash-array-practice.sh 1 2 3 4
Output:
输出:
Dollar-1 : 1
Dollar-2 : 2
Dollar-3 : 3
Dollar-AT: 1 2 3 4
A Val 0: 1
A Val 1: 2
A Val 2: 3
A All Values: 1 2 3 4
回答by Jacob Wegelin
The importance of the double quotes is worth emphasizing. Suppose an argument contains whitespace.
双引号的重要性值得强调。假设一个参数包含空格。
Code:
代码:
#!/bin/bash
printf 'arguments:%s\n' "$@"
declare -a arrayGOOD=( "$@" )
declare -a arrayBAAD=( $@ )
printf '\n%s:\n' arrayGOOD
declare -p arrayGOOD
arrayGOODlength=${#arrayGOOD[@]}
for (( i=1; i<${arrayGOODlength}+1; i++ ));
do
echo "${arrayGOOD[$i-1]}"
done
printf '\n%s:\n' arrayBAAD
declare -p arrayBAAD
arrayBAADlength=${#arrayBAAD[@]}
for (( i=1; i<${arrayBAADlength}+1; i++ ));
do
echo "${arrayBAAD[$i-1]}"
done
Output:
输出:
> ./bash-array-practice.sh 'The dog ate the "flea" -- and ' the mouse.
arguments:The dog ate the "flea" -- and
arguments:the
arguments:mouse.
arrayGOOD:
declare -a arrayGOOD='([0]="The dog ate the \"flea\" -- and " [1]="the" [2]="mouse.")'
The dog ate the "flea" -- and
the
mouse.
arrayBAAD:
declare -a arrayBAAD='([0]="The" [1]="dog" [2]="ate" [3]="the" [4]="\"flea\"" [5]="--" [6]="and" [7]="the" [8]="mouse.")'
The
dog
ate
the
"flea"
--
and
the
mouse.
>