4

Is it possible to safely ignore the aforementioned error message? Or is it possible to remove the null byte? I tried removing it with tr but I still get the same error message.

this is my script:

#!/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

when I run inotify-create.sh and I create a new file in "monitordir"

I get:

[@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
5
  • Not an answer, but instead of using echo ... | tr -d '\n', why not use echo -n ...?
    – DopeGhoti
    Dec 28, 2016 at 18:57
  • it's the --output-delimiter="" part of your cut invocation that's generating the null bytes, are you able to use a different delimiter? And besides to rid null bytes you need tr -d '\0' and not tr -d '\n'
    – iruvar
    Dec 28, 2016 at 19:00
  • You could possibly change the cut to awk '{ print $1$2$3 }', but I don't know what your input looks like. You also don't need to end every statement with ;. You only need to do that if two statements are written on the same line.
    – Kusalananda
    Dec 28, 2016 at 19:04
  • i need --output-delimiter="" because otherwise i get a space in between the directory and filename. The variable $newfile outputs /home/user/Monitor/ CREATE newfile with a newline at the end. What about basefile=$(echo ${newfile} | gawk -F " " '{print $1$3}')? Any reason not to use gawk (ie speed)?
    – jes516
    Dec 28, 2016 at 20:07
  • @jes516, in that case your read should be while read -r dir _ file and basefile then becomes ${dir}${file}
    – iruvar
    Dec 28, 2016 at 20:16

1 Answer 1

4

As for your exact question:

Can I safely ignore: “warning: … ignored null byte … ”?

The answer is yes, since you are creating the null byte with your own code.
But the real question is: Why do you need a "null byte"?

The inotifywait command will produce an output in the form of:

$dir ACTION $filename

Which, for your input, looks like this (for file hello4):

/home/user/Monitor/ CREATE hello4

The command cut will print fields 1 and 3, and using a null delimiter in --output-delimiter="" will produce an output with an embedded null, something like:

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

That is not what you need, because of the added null.

The solution turns out to be very simple.
Since you are using the command read already, do this:

#!/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

Use the default value of IFS to split on whitespace the input and just use the directory and filename to copy.

1
  • this is even better. now the man page makes sense watched_filename EVENT_NAMES event_filename aka dir action file ty
    – jes516
    Dec 28, 2016 at 22:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .