How to avoid a useless use of cat when parsing a file?

AProgrammer's suggestion of using xargs is often best, but another option is to use redirection into a while loop, which allows additional commands to be made and variables to be set:

while read -r dir; do mkdir $dir; done < myfile

An example of a more complicated structure would be:

now=`date +%Y%m%d.%H%M%S`
while read -r dir; do
    newdistfile="/tmp/dist-`echo $dir | tr / _`.tgz"
    mv $dir ~/backups/$dir.$now &&
        mkdir $dir &&
        tar xzfC $newdistfile $dir
done < myfile

This is not something that xargs could do without writing a 'helper program'.


At least in bash, as long as there are no filenames containing spaces and newlines, this:

mkdir $(< myfile) 

works. So we have a useless use of for, xargs too.

< does not start a new process in bash, in contrast to cat, but I don't know for ksh.


I'd do something like

xargs mkdir < myfile

Tags:

Shell

Ksh

Cat