[英]How to pass empty array as argument in bash
I am trying to write a simple function in bash which takes 2 arguments, a string and an array. 我试图在bash中编写一个简单的函数,该函数需要2个参数,一个字符串和一个数组。
At the very beggining of the function I check for the number of arguments 在函数的开始阶段,我检查参数的数量
function is2args() {
if [ $# -le 1 ]; then
return 1
fi
return 0
}
I then attempt the following 然后我尝试以下
arr=()
is2args "" "${arr[@]}" # returns 1
This triggers the if statement as bash for some reason thinks there is only one argument however if the list is an empty string it works 由于某种原因,这会将if语句触发为bash,因为它认为只有一个参数,但是如果列表为空字符串,则它可以工作
arr=()
is2args "" "" # returns 0
or filled with elements 或充满元素
arr=(
"element"
)
is2args "" "${arr[@]}" # returns 0
and default to an empty string 且默认为空字符串
arr=()
is2args "" "${arr[@]:-""}" # returns 0
I don't quite understand what is going on. 我不太了解发生了什么。 It is my understanding this is the correct way to pass a list however it seems that an empty list is ignored for whatever reason.
据我了解,这是传递列表的正确方法,但是无论出于何种原因似乎都忽略了一个空列表。
Should I really be setting a default empty string every time I send through an array or is there a way to catch this in the function itself? 我真的应该在每次通过数组发送时都设置一个默认的空字符串,还是有办法在函数本身中捕获它呢?
This is expected. 这是预期的。 The
"${arr[@]}"
form expands to put each element of the array on the command line, and there are no elements in the array. "${arr[@]}"
形式扩展为将数组的每个元素放在命令行上,并且数组中没有元素。
The "${arr[*]}"
form will expand to one string consisting of the elements concatenated with a space (by default). "${arr[*]}"
形式将扩展为一个字符串,该字符串由带空格的元素组成(默认情况下)。
You might try this to test the arguments passed: 您可以尝试通过以下方法测试传递的参数:
$ nargs () {
local i=0
for arg do printf "%d\t>%s<\n" $((++i)) "$arg"; done
}
$ nargs foo "${arr[@]}"
1 >foo<
$ nargs foo "${arr[*]}"
1 >foo<
2 ><
$ arr=(1 2 3)
$ nargs foo "${arr[@]}"
1 >foo<
2 >1<
3 >2<
4 >3<
$ nargs foo "${arr[*]}"
1 >foo<
2 >1 2 3<
$ IFS=,
$ nargs foo "${arr[*]}"
1 >foo<
2 >1,2,3<
If you want to pass a fixed argument and an array, shift
the first element off; 如果你想通过一个固定的参数和阵列,
shift
所述第一元件断开; what remains in "$@"
is the contents of your array. 保留在
"$@"
是数组的内容。 If the array is empty, $@
will be empty too. 如果数组为空,则
$@
也将为空。 (If $#
is 0 after the shift
, that means your array's length was 0 -- or no array was passed; the two cases are precisely identical). (如果
shift
后$#
为0,则意味着您数组的长度为0 -或未传递任何数组;这两种情况完全相同)。
myfn() {
local string_arg=$1; shift || { echo "ERROR: Initial argument missing" <&2; return 1; }
local -a array_args=( "$@" )
echo "Initial argument is: $string_arg"
echo "Array has length ${#array_args[@]}"
echo "Array elements:"
printf ' - %q\n' "${array_args[@]}"
}
array=( "first array member" "second array member" )
myfn "first argument" "${array[@]}"
You might want to pass the name of the array (note that the below requires bash 4.3): 您可能要传递数组的名称 (请注意,以下要求使用bash 4.3):
myfunc() {
local arg1=$1
local -n array=$2
printf "first arg: %q\n" "$arg1"
echo array:
declare -p array
}
arr=()
myfunc "foo" arr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.