Can I safely ignore: "warning: command substitution: ignored null byte in input"?

500

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.

Share:
500

Related videos on Youtube

aperture
Author by

aperture

Updated on September 18, 2022

Comments

  • aperture
    aperture almost 2 years

    I'm using Angular to post some data to a Django endpoint with:

    $http({
      method: 'POST',
      url: '/api/projects/166/interval_sort/',
      data: JSON.stringify({ids: [251,250,249,235,233,234]})
    })
    

    And in my view method, request.POST is an empty <QueryDict: {}>

    def ProjectIntervalSort(request, pk):
      logger.debug("SORTABLE DATA: "+str(request.POST))
      return HttpResponse(status=204)
    

    Chrome's network inspector shows my JSON object being sent, but I don't seem to be able to access it from the Django side. Am I not formatting the data correctly or missing something in my view method? Thanks.

    • Pankaj Goyal
      Pankaj Goyal over 7 years
      Not an answer, but instead of using echo ... | tr -d '\n', why not use echo -n ...?
    • Mathias Begert
      Mathias Begert over 7 years
      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'
    • Kusalananda
      Kusalananda over 7 years
      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.
    • jes516
      jes516 over 7 years
      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)?
    • Mathias Begert
      Mathias Begert over 7 years
      @jes516, in that case your read should be while read -r dir _ file and basefile then becomes ${dir}${file}
  • aperture
    aperture over 8 years
    Ah, perfect. SO will let me accept this in like 10 minutes. Thanks.
  • jes516
    jes516 over 7 years
    this is even better. now the man page makes sense watched_filename EVENT_NAMES event_filename aka dir action file ty