Other Options after #! (Shebang)

I want a script semacs that open emacs with what was sent to /dev/stdin in
the buffer. Example:

echo abc | semacs

Should open emacs with "abc" in the buffer. The idea is to exploit emacs' EasyPG assistant in many "primitive" mail readers through piping.

The following does not work:

Code:
#!/usr/opt/bin/emacs --insert /dev/stdin

But the following does work:

Code:
#!/bin/sh
exec /usr/opt/bin/emacs --insert /dev/stdin

and

Code:
#!/bin/sh
/usr/opt/bin/emacs --insert /dev/stdin

My question: why the first does not work? Does someone have a better idea?
 
I'm not entirely sure I understand the question ... are you expecting your system to somehow execute a plain text file?

Or did you just forget the #! on your first example ... in this case, the answer is probably quite simple: A script is fed to its interpreter (specified in the shebang line) as a command line argument, and this probably isn't a valid invocation of emacs any more (looking like /usr/opt/bin/emacs --insert /dev/stdin semacs)

Just use your second form, it has the little overhead of invoking /bin/sh, but the exec makes sure the process is replaced with emacs.
 
I forgot the #! in the forum-article (corrected). The script with the line

Code:
#!/usr/opt/bin/emacs --insert /dev/stdin

Does not work. It does execute emacs, but it does not insert "abc". I get in emacs' minibufer:

Code:
Unknown option `--insert /dev/stdin'

Perhaps it considers all what follows the first space as an (one) argument, even it there are more spaces.

The other scripts works only with X11. In the console I get:

Code:
emacs: standard input is not a tty

Is ttyv1 really not a tty?!
 
The shebang line of #!/usr/opt/bin/emacs --insert /dev/stdin doesn't work because of how FreeBSD (and many unicies) interpret shebang lines. It seems like you're sending two arguments, but in reality --insert /dev/stdin is sent as a single argument.

You can test this out with just a simple shell script. #!/bin/sh -x and #!/bin/sh -e and #!/bin/sh -xe work, but #!/bin/sh -x -e will not. The error (Illegal option -) will seem weird but you can get it more directly by running /bin/sh '-x -e'. It's essentially telling you that the space char is an illegal flag.

The history of shebang lines is rather interesting and it makes for good reading if you're interested:

https://www.in-ulm.de/~mascheck/various/shebang/
 
Back
Top