0

Below shows what I'm getting when I'm in the shell trying to reference a filename that has a space in the name.

 #===================================
 $>ls *2021.txt
 $> 'JAN 2021.txt'  'FEB 2021.txt'  'MAR 2021.txt'
 $> cat files.txt
 JAN 2021.txt
 FEB 2021.txt
 MAR 2021.txt

 $> cat ascript.txt
 for i in `cat files.txt`
   do
   echo $i
   done

 $> ./ascript.txt
 JAN
 2021.txt
 FEB
 2021.txt
 MAR
 2021.txt

 $>
 #===================================

As you can see, the variable $i in the shell script uses the space in the file name as a delimiter. Thus, I cannot get the entire filename in the variable $i in the for loop for each row in the files.txt file. Anyone know how I can reference every part of the filename in the for loop per each line?

I'm ultimately trying to determine from the filename the month and the year within the for loop to create variables relating to the month and the year. Example: to evaluate the string JAN in the filename to '01', FEB to '02' and the 2021 to '2021', etc. These variables will then be used to create a date string that will be inserted as a column for each row in each of the respective files to denote that all records within a file is from that dated file.

Sorry for the lengthy post, but, I'm trying to be as clear as possible.

Thanks in advance!!

  • Suppose you have a name with an embedded newline. How will you handle that case? If you want to put lists of names in a text file, the only sane approach is to disallow certain characters. (You can separate names with a null character, but then your file is not a text file.) IMO, you should only allow names with characters from the POSIX portable file name character set. – William Pursell Sep 16 '22 at 19:35
  • Try `for i in $(cat files.txt)` ? – Zak Sep 16 '22 at 19:36
  • how did the filenames get into `files.txt`? – jhnc Sep 16 '22 at 20:09
  • 1
    @Zak, that's exactly the same problem. [Don't read lines with `for`](https://mywiki.wooledge.org/DontReadLinesWithFor) – glenn jackman Sep 16 '22 at 20:12
  • ...so you want `for i in *.txt; do echo "$i"; done` to loop over the filesystem, or `printf '%s\0' *.txt >files.nsv` to save a list of files to disk and `while IFS= read -r -d '' i; do echo "$i"; done – Charles Duffy Sep 16 '22 at 20:15
  • @Zak, in addition to the link glenn provided above, see also [BashPitfalls #1](https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29). – Charles Duffy Sep 16 '22 at 20:20
  • @WilliamPursell, why is "not a text file" a bad thing? If you simply declare by fiat "this application is not intended to handle files with names not from the portable set", then someone doing security analysis needs to worry about how it _actually does_ handle them in practice, because anyone doing development with "doesn't have to" as part of their mindset presumably didn't think about it earlier. And having input that's _possible_ but that the developers of a system didn't think they needed to care about... well, if you're going to have bugs, that's a place they're likely to lurk. – Charles Duffy Sep 16 '22 at 20:22
  • @WilliamPursell, ...I think you've been here long enough to have read my war story about the data loss event caused by filenames that "couldn't possibly" have characters that weren't hexadecimal. Deciding to declare things out-of-scope, alas, doesn't mean they're impossible. – Charles Duffy Sep 16 '22 at 20:24
  • I wrote some logic as a work around by reading every 2nd line to get the month and year. However, the link "Don't read lines with" had the perfect answer. $ IFS=$'\n'; set -f; for i in $( – DataFanatik Sep 16 '22 at 20:24
  • @CharlesDuffy "not a text file" is not necessarily a bad thing, but it is problematic when the objective is to store names in a text file. – William Pursell Sep 16 '22 at 20:24
  • @WilliamPursell, ...huh. I don't find the word "text" anywhere in the question except the comments. – Charles Duffy Sep 16 '22 at 20:25
  • 1
    @CharlesDuffy The original question has files named `*.txt`. Having a non-text file with a name `*.txt` is a source of confusion. – William Pursell Sep 16 '22 at 20:26
  • I agree that files should not have any weird characters in the filename. To clarify, I was just using 2021*.txt to display only the files with names ending with 2021. The files with the space in the filename was created by another system. – DataFanatik Sep 16 '22 at 20:35

0 Answers0