How to add # Shebang in first line of a file which is an auto create script by another program

3

1

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

StarCoder17

Posted 2015-10-27T10:07:35.837

Reputation: 31

tried escaping the ! character in your attempts? as in \! instead of plain ! – Xen2050 – 2015-10-27T10:11:19.997

Thanks "Xen2050", i tried your suggestion and this works well.

sed '1 i#!/bin/csh -f' filename > out.txt – StarCoder17 – 2015-10-27T10:16:42.047

I'll toss it in an answer then – Xen2050 – 2015-10-27T10:21:32.843

Answers

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

Xen2050

Posted 2015-10-27T10:07:35.837

Reputation: 12 097

If you put -i in the sed command you can modify the file instead to do the redirection. – Hastur – 2015-10-27T11:25:17.387

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

Hastur

Posted 2015-10-27T10:07:35.837

Reputation: 15 043