SEDはシェルでは機能しますが、スクリプトでは機能しません。

SEDはシェルでは機能しますが、スクリプトでは機能しません。
 cat test.txt

Host01-pc.local

 i=host01-pc.local
 sed -i "/$i/d" ./test.txt
 cat test.txt

したがって、これはシェルで動作しますが、スクリプトで実行すると

readarray PC < ./test.txt

for i in "${PC[@]}";
do

        ping -c 1 -W 20 $i

        if [ $? -eq 0 ];
                then echo -e "$i is reachable";
                sed -i "/$i/d" ./test.txt

        else
                        
                        echo -e "$i no ping"

        fi
done

エラーメッセージが表示されます

sed: -e 式 #1、文字 26: 終了していないアドレス正規表現

ベストアンサー1

より大きなバージョンのフラッシュバージョンであるかどうかはわかりません。元のスクリプトで最小限の詳細のみを修正しました。 GNU Bashでテストされました。

#!/usr/bin/bash
readarray -t arr < test.txt
for i in "${arr[@]}"; do
    if ping -c 1 -W 20 "$i" > /dev/null 2>&1; then
        echo "$i is reachable"
        sed -i "/$i/d" test.txt
    else
        echo "$i no ping"
    fi
done

*-t追加されたフラグについてコメントしてくれた@steeldriverに感謝しますreadarray

おすすめ記事