IPSETの存在を検出するスクリプトを作成しています。
#!/bin/bash
str=$(/usr/sbin/ipset test IPsetName 1.1.1.1)
echo "$str" #outputs blank line
if [[ $str = *"The set with the given name does not exist"* ]]; then
echo "IPsetName not found"
fi
このスクリプトを実行すると、次の結果が表示されます。
ipset v6.29: The set with the given name does not exist
その後、空行があり、echo "$str"
if文の予想出力は表示されません。
ipsetコマンドの出力を変数に保存するには?
ベストアンサー1
ありがとう @StephenHarris
ipsetコマンドの出力はstdoutではなくstderrから生成され、2>&1
出力は変数にキャプチャされます。
str=$(/usr/sbin/ipset test IPsetName 1.1.1.1 2>&1)
if [[ $str = *"The set with the given name does not exist"* ]]; then
echo "IPsetName not found"
fi
今ifステートメントが期待どおりに動作します!