cat:无效选项 -- 'a'

cat: invalid option -- 'a'

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

for i in 'find test -name *.c'; do cat test/common.txt $i > temp.txt && mv temp.txt $i; done

但出现错误 "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

我在尝试使用 cp 命令执行自定义上传的 shell 脚本时遇到了类似的错误,经过大量时间寻找原因后,我终于发现我的脚本文件有 windows line endings 并将它们转换为 unix format 后问题解决了。