Unix script to automatically insert shebang into perl file
I am practicing with some Perl scripting and instead of typing the shebang each time I decided to make a basic Unix script that would create a new file with .pl extension and the shebang and then open the file for editing. What I tried was:
alias ep "echo '#! /usr/bin/perl' >> perl/\!*.pl; emacs perl/\!*.pl;"
But this causes an error if the file doesn't already exist. I found that strange because I thought >> would append or create. So I changed the >> to > and this works but only if the file does not exist. If the file does exist it just crashes with an error. I tried adding an if statement but that also gave errors of syntax error and improper then. What I had for the if was:
alias ep "if [ ! -f perl/\!*.pl ] 开发者_如何学运维then echo '#! /usr/bin/perl' > perl/\!*.pl fi; emacs perl/\!*.pl;"
What am I missing? I am not too familiar with Unix scripting, but have a general knowledge.
It seems like you might have set -C
, also known as set -o noclobber
.
You can go ahead and clobber the file by using the redirection operator >|
, or by undoing the option with set +C
.
you don't want to do it like this; there are a lot of things to take account for (is that really a perl file if it exists already, etc)
you can do it instead directly with emacs
check this out: emacs skeletons and auto-insert
精彩评论