私はここにスペースを描いている。このスクリプトをどのように書くべきですか?
#!/bin/sh
XZY_NODES="\
vl-ocnt-1 \
vl-ocnt-2 \
vl-onet-1 \
vl-onet-2 \
vl-ocmp-1 \
vl-oomp-2 \
"
is_xyz_node () {
host=$1
for xyznode in $XZY_NODES
do
if [ $host == $xyznode ]
then
return 'Y'
fi
done
return 'N'
}
if [ 'Y' == $(is_xyz_node $1) ]
then
echo "$1 is a xyz node"
else
echo "$1 is NOT a xyz node"
fi
このスクリプトを実行すると、次のような結果が出力されます。
$ ./test.sh not-xyz
./test.sh: line 22: return: N: numeric argument required
./test.sh: line 25: [: Y: unary operator expected
not-xyz is NOT a xyz node
$ ./test.sh vl-ocnt-2
./test.sh: line 19: return: Y: numeric argument required
./test.sh: line 25: [: Y: unary operator expected
vl-ocnt-2 is NOT a xyz node
ベストアンサー1
戻り値は整数でなければならないので、1
または0
。はでありbash
、false
なり1
、true
なる0
。だから私は次のよう'N'
に置き換えます。1
'Y'
0
if
また、一致しなくなったため、'Y'
ステートメントを変更できます0
。
if [ 'Y' == $(is_xyz_node $1) ]
しなければならない
if is_xyz_node $1
関数は1
orを返す必要があるため、or0
に対応します。true
false