Linux cat:无效选项--';a';

Linux cat:无效选项--';a';,linux,shell,Linux,Shell,我在“Abb/test”文件夹中有一些c文件和common.txt,在“Abb”文件夹中有temp.txt。我想复制所有c文件头中common.txt的内容。我正在使用以下unix shell脚本代码:- 但它给出了错误“cat:invalid option--'a'” 有人能帮我吗。您的代码中有几个严重的问题 您的代码带有换行符以提高可读性: for i in 'find test -name *.c' do cat test/common.txt $i > temp.txt &

我在“Abb/test”文件夹中有一些c文件和common.txt,在“Abb”文件夹中有temp.txt。我想复制所有c文件头中common.txt的内容。我正在使用以下unix shell脚本代码:-

但它给出了错误“cat:invalid option--'a'”

有人能帮我吗。

您的代码中有几个严重的问题

您的代码带有换行符以提高可读性:

for i in 'find test -name *.c'
do
  cat test/common.txt $i > temp.txt && mv temp.txt $i
done
问题:shell替换的引用错误

for i in `find test -name '*.c'`
do
  cat test/common.txt $i > temp.txt && mv temp.txt $i
done
问题:未引用
$i

for i in `find test -name '*.c'`
do
  cat test/common.txt "$i" > temp.txt && mv temp.txt "$i"
done
问题:使用for循环来循环文件名

find test -name '*.c' | while read -r filename
do
  cat test/common.txt "$filename" > temp.txt && mv temp.txt "$filename"
done

在执行自定义上传的shell脚本时,我尝试使用
cp
命令时出现了类似的错误,经过长时间的搜索,我终于发现我的脚本文件有
windows行结尾
,并将它们转换为
unix格式
解决了这个问题已解决。

甚至可能执行
查找-exec
而不是循环查找的结果
find test -name '*.c' | while read -r filename
do
  cat test/common.txt "$filename" > temp.txt && mv temp.txt "$filename"
done