Filenames with spaces in Bash script

  • Thread starter Thread starter Chrisas
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on handling filenames with spaces in a Bash script, specifically when processing input files generated by a Windows data recorder. The initial approach using a for loop fails due to improper handling of filenames containing spaces. The solution involves using a while loop with the read command, allowing for correct variable substitution to strip the file extension and generate output files without syntax errors. The final working script effectively processes files with spaces in their names.

PREREQUISITES
  • Understanding of Bash scripting
  • Familiarity with file manipulation in Unix-like systems
  • Knowledge of variable substitution in Bash
  • Experience with command-line tools like ls and awk
NEXT STEPS
  • Learn about using the read command in Bash for file processing
  • Explore advanced variable substitution techniques in Bash
  • Research best practices for handling filenames with spaces in scripts
  • Investigate other methods for file manipulation using tools like find and xargs
USEFUL FOR

Developers and system administrators who work with Bash scripts, particularly those dealing with file processing and manipulation in environments where filenames may contain spaces.

Chrisas
Messages
152
Reaction score
0
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.
 
Technology news on Phys.org
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 separate 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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
Replies
19
Views
4K