【发布时间】:2013-02-08 14:47:34
【问题描述】:
我正在更改一个结构如下的 bash 脚本:
#somewhere in the code
sim_counts=#... some value
function_name()
{
set $sim_counts
for hostname in $linux_hostnames; do
if [ $1 -eq 0 ]; then # if sim_counts equal 0
shift # jump forward in sim_counts
continue
fi
# ... more code
shift
done
}
然后在脚本中调用:
function_name
我想给这个函数引入一个参数:
#somewhere in the code
sim_counts=#... some value
function_name()
{
ip=$1
set $sim_counts
for hostname in $linux_hostnames; do
if [ $1 -eq 0 ]; then # if sim_counts equal 0
shift # jump forward in sim_counts
continue
fi
# ... more code
shift
done
}
并按以下方式调用函数:
function_name 10.255.192.123
如何避免$1函数参数与set命令中的其他值冲突?
【问题讨论】:
-
$sim_counts代表什么? -
@JonahBishop 它是在特定机器上启动的进程的值。
-
由于您在调用
set之前将函数的参数保存到ip,因此我不确定您是否存在真正的冲突。也就是说,为什么你需要在这里使用set?你不能像使用$1一样使用sim_counts吗?
标签: bash shell parameters