機能パラメータ

機能パラメータ

単純な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

しかし、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
}

次に、次のように関数を呼び出します。

x_pos="$(convert_coordinates $x_pos, $x_marg, $x_grid, $x_max)"
Y_pos="$(convert_coordinates $y_pos, $y_marg, $y_grid, $y_max)"

ただし、新しいスクリプトは常に構文エラー:オペランドが必要(「、」とマークされたエラー)のため失敗します。

私も様々なバリエーションを試しました。

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,"")

作業スクリプトと機能しないスクリプトの唯一の違いは、2番目のスクリプトでパラメータを渡すことです...それで、パラメータ値を関数内の定数値に設定してみました。スクリプトは役に立ちません。)

だから私は何が間違っているのかわかりません...関数に引数を渡してから、渡された値を使用して計算を実行できるようにしたいです。

ベストアンサー1

存在する、パラメータ区切り文字は空白なので、次のようになります。

変える:

x_pos="$(convert_coordinates $x_pos, $x_marg, $x_grid, $x_max)"

する

x_pos="$(convert_coordinates $x_pos $x_marg $x_grid $x_max)"

おすすめ記事