Bashで関数を書くには?

Bashで関数を書くには?

私はここにスペースを描いている。このスクリプトをどのように書くべきですか?

#!/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。はでありbashfalseなり1trueなる0。だから私は次のよう'N'に置き換えます。1'Y'0

ifまた、一致しなくなったため、'Y'ステートメントを変更できます0

if [ 'Y' == $(is_xyz_node $1) ] 

しなければならない

if is_xyz_node $1

関数は1orを返す必要があるため、or0に対応します。truefalse

おすすめ記事