bash cat:無効なオプション - 'r'

bash cat:無効なオプション - 'r'

-rRedHat enterprise 7.7 Maipo(???)サーバーでこのコマンドの無効なオプションをすべて検索しましたが、cat成功しませんでした。

フルパスを含むファイル名のリストを読み、リストされたファイルを開いてfilelist.log追加してみてください。コンテンツファイルを1つの大きなファイルにマージします。

たとえば、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"

おすすめ記事