我想将一个可变数量的'元组'作为参数传递给一个bash脚本,并使用模式匹配在循环中完成它们,如下所示:
for *,* in "$@"; do
#do something with first part of tuple
#do something with second part of tuple
done
Run Code Online (Sandbox Code Playgroud)
这可能吗?如果是这样,我如何访问元组的每个部分?
例如,我想把我的脚本称为:
bash bashscript.sh first_file.xls,1 second_file,2 third_file,2 ... nth_file,1
由于bash没有元组数据类型(它只有字符串),您需要自己编码和解码它们.例如:
$ bash bashscript.sh first_file.xls,1 second_file,2 third_file,2 ... nth_file,1
Run Code Online (Sandbox Code Playgroud)
在bashscript.sh:
for tuple in "$@"; do
IFS=, read first second <<< "$tuple"
...
done
Run Code Online (Sandbox Code Playgroud)