「警告:コマンドの置き換え:入力からヌルバイトを無視する」を無視してもよいですか?

「警告:コマンドの置き換え:入力からヌルバイトを無視する」を無視してもよいですか?

上記のエラーメッセージは無視しても安全ですか?それとも、ヌルバイトを削除できますか?削除しようとしましたが、trまだ同じエラーメッセージが表示されます。

これは私のスクリプトです。

#!/bin/bash                                                                     

monitordir="/home/user/Monitor/"                                                
tempdir="/home/user/tmp/"                                                       
logfile="/home/user/notifyme"                                                   

inotifywait -m -r -e create ${monitordir} |                                     
while read newfile; do                                                          
    echo "$(date +'%m-%d-%Y %r') ${newfile}" >> ${logfile};                     
    basefile=$(echo ${newfile} | cut -d" " -f1,3 --output-delimiter="" | tr -d '\n');
    cp -u ${basefile} ${tempdir};                                               
done

実行しinotify-create.shて新しいファイルを作成するとき"monitordir"

私は得る:

[@bash]$ ./inotify-create.sh 
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
./inotify-create.sh: line 9: warning: command substitution: ignored null byte in input

ベストアンサー1

正確な質問は次のとおりです。

「警告:…ヌルバイト無視中…」を無視してもいいですか?

答えは「はい」です。自分のコードを使ってヌルバイトを生成するからです。
しかし、実際の質問は「ヌルバイト」がなぜ必要なのかということです。

このinotifywaitコマンドは、次の形式の出力を生成します。

$dir ACTION $filename

入力内容は次のとおりです(hello4ファイルの場合)。

/home/user/Monitor/ CREATE hello4

cutコマンドはフィールド1と3を印刷し、null区切り文字を使用すると、--output-delimiter=""次のようにnull値を含む出力が生成されます。

$'/home/user/Monitor/\0hello4\n'

null が追加されたため、これは必要ではありません。

解決策は非常に簡単であることがわかりました。
すでにこのコマンドを使用しているので、read次の手順を実行します。

#!/bin/bash
monitordir="/home/user/Monitor/"
tempdir="/home/user/tmp/"
logfile="/home/user/notifyme"

inotifywait -m -r -e create ${monitordir} |
    while read dir action basefile; do
        cp -u "${dir}${basefile}" "${tempdir}";
    done

IFSのデフォルト値を使用して入力をスペースに分割し、ディレクトリとファイル名のみを使用してコピーします。

おすすめ記事