dan*_*iel 3 scripting bash function arguments
我对什么应该是一个简单的 bash 脚本有问题。
我有一个完美运行的 bash 脚本:
function convert_to ()
x_max=2038
y_max=1146
x_marg=100
y_marg=30
x_grid=150
y_grid=150
if (("$x_pos" > "($x_marg+($x_grid/2))")); then
x_pos=$((x_pos-x_marg))
x_mod=$((x_pos%x_grid))
x_pos=$((x_pos/x_grid))
x_pos=$((x_pos*x_grid))
fi
Run Code Online (Sandbox Code Playgroud)
但是,我想更改将 4 个值作为参数传递给函数的脚本:
function convert_to ()
pos="$1"
marg="$2"
grid="$3"
max="$4"
# I verify that the inputs have arrived with this display
zenity --info --title "Info" --text "inputs: pos: $pos marg: $marg grid: $grid max: $max"
if (("$pos" > "($marg+($grid/2))")); then
pos=$((pos-marg))
mod=$((pos%grid))
pos=$((pos/grid))
pos=$((pos*grid))
fi
}
Run Code Online (Sandbox Code Playgroud)
然后我将按如下方式调用该函数:
x_pos="$(convert_coordinates $x_pos, $x_marg, $x_grid, $x_max)"
Y_pos="$(convert_coordinates $y_pos, $y_marg, $y_grid, $y_max)"
Run Code Online (Sandbox Code Playgroud)
但是,新脚本总是因语法错误而失败:预期操作数(错误标记为“,”)。
我也尝试了很多变体:
pos=$[[ $pos - $marg ]] ...... which results in syntax error: operand expected (error token is "[ 142, - 100, ]")
pos=[[ $pos - $marg ]] .......... fails with command not found
pos=$[[ "$pos" - "$marg" ]] ..... fails with command not found
pos=$(("$pos"-"$marg")) ......... syntax error: operand expected (error token is ""142,"-"100,"")
Run Code Online (Sandbox Code Playgroud)
工作脚本和非工作脚本之间的唯一区别是我在第二个脚本中传递参数......脚本一文不值).. 但是,现在函数中的计算工作没有错误。
所以我对我做错的事情感到茫然......我希望能够将参数传递给函数,然后使用传递的值进行数学计算。
在bash 中,参数分隔符是空格,所以:
代替 :
x_pos="$(convert_coordinates $x_pos, $x_marg, $x_grid, $x_max)"
Run Code Online (Sandbox Code Playgroud)
做
x_pos="$(convert_coordinates $x_pos $x_marg $x_grid $x_max)"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
304 次 |
最近记录: |