Decluttering
Following this that I wrote a long time ago.
Oh, I won't touch on the behavior on Windows.
A
$ cat hoge.pl
#!/bin/bash
find . -type f | perl -x $0
exit 0
#!/usr/bin/env perl -ln
print ;
$ perl hoge.pl
./bar
./foo
./hoge
.
.
.
If the #! Line does not contain the words "perl" or "indir", the program specified after the #! Will be executed instead of the Perl interpreter. This is a bit weird, but it works for those on machines that can't do #!; Perl is correct if you say that the SHELL you're using for your program is / usr / bin / perl Because it starts the interpreter. (Quoted from perlrun)
In other words
python
$ cat hoge.rb
#!/usr/bin/env ruby
puts("ruby")
$ perl hoge.rb
ruby
$ cat hoge.sh
#!/bin/bash
echo bash
$ perl hoge.sh
bash
If the script specified on the command line is a file that starts with
#!
and the line does not contain the stringruby
, the line will be skipped. If you find a line that contains the stringruby
in the string following#!
, Execute the line below that line as a Ruby script. (Quoted from rdoc)
B
$ cat hoge2.sh
#!/bin/bash
echo bash
ruby -x $0
exit 0
#!/usr/bin/env ruby
puts("ruby")
in the case of,
python
$ ruby hoge2.sh
ruby
If you specify the> -x switch, it first looks for a line that contains the strings #! And "perl" and starts parsing from there. This is useful when you want to embed a program in large text and run it. (In this case, the end of the program should be indicated by the token END.)
(Quoted from perlrun)
For ruby, the same thing is clearly stated in rdoc.
Therefore, the code of A holds.
Even in ruby B
Run B in bash.
$ bash hoge2.sh
bash
ruby
So you can also say this.
python
$ cat hoge3.sh
#!/bin/bash
echo bash
perl -x $0
ruby -x $0
exit 0
#!/usr/bin/env perl
print("perl\n") ;
__END__
#!/usr/bin/env ruby
puts("ruby")
__END__
Run with bash
$ bash hoge3.sh
bash
perl
ruby
Run with perl
$ perl hoge3.sh
bash
perl
ruby
Run with ruby
$ ruby hoge3.sh
ruby
I wrote that
If you read it again, in the case of ruby, there is no significance of the existence of -x? What is it for ... maybe for windows ...
tsuiki.sh
#!/bin/bash
ruby -x $0
exit 0
#!/usr/bin/env ruby
puts("ruby")
^D #Control+V, Contorol+Embed with D(mac terminal)
puts("test")
$ bash tsuiki.sh
ruby
$ ruby tsuiki.sh
ruby
In the case of python, although there is -x, it is clearly stated that "only one line is skipped" with considerable restrictions.
-x (original) Skip the first line of source to use #! cmd in non-Unix format. This is for DOS-only hacks only. (Quoted from Document)
However, if you overdo it,
$ cat hoge4.sh
find . -type f | python -x $0 ; exit 0
#!/usr/bin/env python
import sys
for line in sys.stdin:
print(line.strip())
Same thing as A
$ bash hoge4.sh
./bar
./foo
./hoge
.
.
.
It's not impossible to do something like that.
Recommended Posts