Reputation: 995
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.
Upvotes: 1
Views: 7818
Reputation: 8756
I had similar error when trying to use cp
command on execution of my custom uploaded shell script and after a lot of time searching for reason I finally find out my script file had windows line endings
and after converting them to unix format
the problem solved.
Upvotes: 0
Reputation: 27073
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
Upvotes: 3