I have some c files and common.txt in "Abb/test" folder and temp.txt in "Abb" folder.I want to copy content of common.txt in header of all the c files. I am using the following unix shell script code:-
for i in 'find test -name *.c'; do cat test/common.txt $i > temp.txt && mv temp.txt $i; done
but it is giving error "cat: invalid option -- 'a' "
can someone help me out.
You have several grave problems in your code.
Your code with newlines for readability:
for i in 'find test -name *.c'
do
cat test/common.txt $i > temp.txt && mv temp.txt $i
done
Problem: wrong quotes for shell substitution.
for i in `find test -name '*.c'`
do
cat test/common.txt $i > temp.txt && mv temp.txt $i
done
Problem: not quoting $i
.
for i in `find test -name '*.c'`
do
cat test/common.txt "$i" > temp.txt && mv temp.txt "$i"
done
Problem: using for loop to loop over filenames.
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
后问题解决了。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.