本文介绍了如何将数组参数传递给 Bash 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
令我惊讶的是,我在搜索了 1 小时后没有找到答案.我想像这样将数组传递给我的脚本:
It is surprising me that I do not find the answer after 1 hour search for this. I would like to pass an array to my script like this:
test.sh argument1 array argument2我不想把它放在另一个 bash 脚本中,如下所示:
I DO NOT want to put this in another bash script like following:
array=(a b c) for i in "${array[@]}" do test.sh argument1 $i argument2 done 推荐答案Bash 数组不是一流的值"——你不能像一个东西"一样传递它们.
Bash arrays are not "first class values" -- you can't pass them around like one "thing".
假设 test.sh 是一个 bash 脚本,我会这样做
Assuming test.sh is a bash script, I would do
#!/bin/bash arg1=$1; shift array=( "$@" ) last_idx=$(( ${#array[@]} - 1 )) arg2=${array[$last_idx]} unset array[$last_idx] echo "arg1=$arg1" echo "arg2=$arg2" echo "array contains:" printf "%s\n" "${array[@]}"并像调用它一样
test.sh argument1 "${array[@]}" argument2更多推荐
如何将数组参数传递给 Bash 脚本
发布评论