3

I am looking for a method to add a shebang #!/bin/csh -f to first line of my file , which is actually getting created by a another set of program, since this script is auto-created, it should run from bash, when user clicks some button in my tool. I tried using sed but it didn't work.

sed ' 1 s/.*/\#!/bin/csh -f/' filename.

and awk

awk 'NR==1{printf "%s %s\n", $0, "#!/bin/csh -f"}' filename

both of these commands returns following

/bin/csh is not an event.

Please suggest a better method.

Dan

  • tried escaping the ! character in your attempts? as in \! instead of plain ! – Xen2050 Oct 27 '15 at 10:11
  • Thanks "Xen2050", i tried your suggestion and this works well. sed '1 i\#\!\/bin\/csh -f' filename > out.txt – StarCoder17 Oct 27 '15 at 10:16
  • I'll toss it in an answer then – Xen2050 Oct 27 '15 at 10:21
1

Try escaping the ! character, by using \! instead of plain !, should give better results. So try:

sed '1 i\#\!\/bin\/csh -f' filename > out.txt
  • If you put -i in the sed command you can modify the file instead to do the redirection. – Hastur Oct 27 '15 at 11:25
1

Awk alternatives

awk 'BEGIN{print "#!/bin/csh -f"} {print}' filename > out.sh

awk 'NR==1{print "#!/bin/csh -f"};{print}' filename > out.sh 

Variant of Xen2050 to modify the file inside instead of redirect it to out.sh

 sed -i '1 i\#\!\/bin\/csh -f' filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.