1

Searched every for the invalid option -r for the cat command on our RedHat enterprise 7.7 Maipo (???) server, but no luck.

Attempting to read in a list of file names with full path, open the file listed in filelist.log, and append the contents of that file into one large file.

As an example, if filelist.log has a list of five file names and their full paths:

1st file in the list consists of 10 lines.                                                              
2nd file in list is 4 lines.                                 
3rd file in list is 7 lines                                 
4th file in list is 6 lines.                                        
5th file in list is 3 lines. 

... then the file created would have 30 lines.

script:

#!/bin/bash

while IFS= read -r line                                   
do echo "$line"  #works fine, echos line-by-line of file name and path to stdout.

cat "$line" >>sourcecode.txt #append CONTENTS of file, (throws invalid option 'r')                               
done < filelist.log

Maybe this is the wrong approach.

Other attempts that threw the same invalid option -r error:

cat $(grep -v '^#' filelist.log) >sourcecode.txt                      
sed '/^$/d;/^#/d;s/^/cat "/;s/$/";/' filelist.log | sh  > sourcecode.txt    
xargs < filelist.log cat >>sourcecode.txt

Have used xargs in past to read in a list of five levels of folder names, and then create a set of the same folder names on another server, but empty, so it seems that would work here also.

3
  • Might one of your files just be called -r or something like file -red or alike? read separates on whitespaces and newlines. Use an approach which delimits file names with the zero-byte.
    – FelixJN
    Commented Dec 3, 2019 at 14:09
  • 1
    Try with cat -- "$line" Commented Dec 3, 2019 at 14:13
  • Could you please include real code and a real filelist.log to work with? It is difficult to advise otherwise.
    – markgraf
    Commented Dec 3, 2019 at 14:43

2 Answers 2

2

During the loop, cat is encountering a "$line" that starts with -r, then it thinks is an option to the command:

cat -rfoo

so it throws the error

cat: invalid option -- 'r'
Try 'cat --help' for more information.

You can bypass it by telling the command to don't accept more options with --

cat -- "$line"
1
  • Thanks to everyone. Works now. I can build a code base out of program files saved all over the network, in hundreds of different directories then zip it up. SOOOOOOOOOOOOOOOO awesome Commented Dec 3, 2019 at 15:01
0

Drop the IFS=, you don't need that in this case.

while read -r line; do cat -- "$line" ; done < filelist >> outputfile

Will do the trick.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .