Multi Line 'While Read' command issue when using sh -c

Hi,
I'm trying to run the following command using sh -c

ie

sh -c "while read EachLine 
do  
rm -f $EachLine ; 
done < file_list.lst;"

It doesn't seem to do anything.

When I run this at the command line, it does remove the files contained in the list so i know the command works
ie

while read EachLine 
do  
rm -f $EachLine ; 
done < file_list.lst;

What am i doing wrong when trying to run it with the sh -c command?

FYI. This is scripting for a etl tool that whenever you want to run a command, it always wraps the command in 'sh -c "..command"'

Any help would be great.

EachLine is expanded too early.

You can use:

sh -c "while read EachLine 
do  
rm -f \$EachLine ; 
done < file_list.lst;"

or

sh -c 'while read EachLine 
do  
rm -f $EachLine ; 
done < file_list.lst;'

or simply:

sh -c 'rm -f `cat file_list.lst`'

or even, depending on your sh implementation:

sh -c 'rm -f $(<file_list.lst)'

If the file is too large to fit in a shell variable some of it might get silently ignored when you use `` or $().

sh -c 'xargs -d "\n" rm -f < ile_list.lst'

I guess you mean "If the file list is too large ..."

I would expect the whole command to fail with an "Argument list too long" or similar error message.

-d "\n" might not be supported by xargs, being a Gnu extension. Beware there is a small typo in the file name too.

I tried those different ideas. The one that works the best for me is:

sh -c "rm -f `cat $PMSourceFileDir/dqa_file_list.lst`"

(FYI. The etl tool expands $PMSourceFileDir to the full path before it issues the command).

Thanks