小编dan*_*iel的帖子

函数参数

我对什么应该是一个简单的 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, …
Run Code Online (Sandbox Code Playgroud)

scripting bash function arguments

3
推荐指数
1
解决办法
304
查看次数

标签 统计

arguments ×1

bash ×1

function ×1

scripting ×1