bash cat: 잘못된 옵션 -- 'r'

bash cat: 잘못된 옵션 -- 'r'

-rRedHat enterprise 7.7 Maipo (???) 서버에서 이 명령의 잘못된 옵션을 모두 검색했지만 cat성공하지 못했습니다.

전체 경로가 포함된 파일 이름 목록을 읽고 나열된 파일을 열고 filelist.log추가해 보세요.콘텐츠파일을 하나의 큰 파일로 병합합니다.

예를 들어 filelist.log5개의 파일 이름과 해당 전체 경로 목록이 있는 경우:

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. 

...그러면 생성된 파일은 30줄이 됩니다.

스크립트:

#!/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

어쩌면 이것은 잘못된 접근 방식일 수도 있습니다.

동일한 잘못된 옵션 -r오류가 발생하는 다른 시도:

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

과거에 xargs를 사용하여 5개 수준 폴더 이름 목록을 읽은 다음 다른 서버에 동일한 폴더 이름 집합을 만들었지만 비어 있으므로 여기서도 작동하는 것 같습니다.

답변1

루프 중에 cat로 시작하는 "$line"을 발견 하면 -r명령의 옵션으로 간주됩니다.

cat -rfoo

그래서 오류가 발생합니다

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

더 많은 옵션을 허용하지 않도록 명령에 지시하여 문제를 해결할 수 있습니다.--

cat -- "$line"

답변2

Remove IFS=이 경우에는 필요하지 않습니다.

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

작동합니다.

관련 정보