Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading null delimited strings through a Bash loop

I want to iterate through a list of files without caring about what characters the filenames might contain, so I use a list delimited by null characters. The code will explain things better.

# Set IFS to the null character to hopefully change the for..in
# delimiter from the space character (sadly does not appear to work).
IFS=$'\0'

# Get null delimited list of files
filelist="`find /some/path -type f -print0`"

# Iterate through list of files
for file in $filelist ; do
    # Arbitrary operations on $file here
done

The following code works when reading from a file, but I need to read from a variable containing text.

while read -d $'\0' line ; do
    # Code here
done < /path/to/inputfile
like image 765
Matthew Avatar asked Dec 30 '11 08:12

Matthew


2 Answers

The preferred way to do this is using process substitution

while IFS= read -r -d $'\0' file; do
    # Arbitrary operations on "$file" here
done < <(find /some/path -type f -print0)

If you were hell-bent on parsing a bash variable in a similar manner, you can do so as long as the list is not NUL-terminated.

Here is an example of bash var holding a tab-delimited string

$ var=$(echo -ne "foo\tbar\tbaz\t"); 
$ while IFS= read -r -d $'\t' line ; do \
    echo "#$line#"; \
  done <<<"$var"
#foo#
#bar#
#baz#
like image 111
SiegeX Avatar answered Nov 14 '22 17:11

SiegeX


Pipe them to xargs -0:

files="$( find ./ -iname 'file*' -print0 | xargs -0 )"

xargs manual:

-0, --null
    Input items are terminated by a null character instead of
    by whitespace, and the quotes and backslash are not
    special (every character is taken literally).
like image 4
Maltigo Avatar answered Nov 14 '22 17:11

Maltigo