Quantcast Filenames with spaces in Bash script Text - Physics Forums Library

PDA

View Full Version : Filenames with spaces in Bash script


Chrisas
Aug15-08, 10:33 AM
I get a bunch of input files from a Windows data recorder. It produces file names with spaces everywhere. I am trying to write a script to process these files. This process involves several steps with multiple output files generated. I want to strip off the extension of the input file and use the name with different extensions for the output files.

Example: I can manually type the file names with ' ' and this works
for FILE in 'file 1' 'file 2' 'file 3'
do
stuff1 < ${FILE}.in > ${FILE}.out1
stuff2 < ${FILE}.in > ${FILE}.out2
done

Example: I try to automatically get filenames
for FILE in `ls -1 *.in | awk -F. '{ print $1; }'`
stuff1 < ${FILE}.in > ${FILE}.out1
stuff2 < ${FILE}.in > ${FILE}.out2
done
This fails because FILE contains file 1 file 2 file 3 and so the loop uses just "file" not "file 1".

Example: I tried to use awk to add ' ' around the file names, something like:
for FILE in `ls -1 *.in | awk -F. '{ print "\'", $1, "\'"; }'`
But no matter what I try I get a syntax failure.

Any ideas?
Thanks.

Chrisas
Aug15-08, 01:00 PM
Solved: The fix was to use read in a while loop instead of using a for loop. I used variable substition instead of awk to seperate the file name from the extension

ls -1 *.in | while read FILE
do
FI=${FILE%.in}
stuff1 < "${FI}.in" > "${FI}.out1"
stuff2 < "${FI}.in" > "${FI}.out2"
done