[RUBY] Note 2 for embedding the scripting language in a bash script

Decluttering

Following this that I wrote a long time ago.

Oh, I won't touch on the behavior on Windows.

perl and ruby

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
.
.
.

1. perl interprets the interpreter.

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

2. ruby skips to ruby if multiple interpreters are written.

If the script specified on the command line is a file that starts with #! and the line does not contain the string ruby, the line will be skipped. If you find a line that contains the string ruby 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

3. perl and ruby can also delay the start of analysis with -x.

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

Postscript

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

For python

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

A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
Change the list in a for statement
Run the Python interpreter in a script
Let's run a Bash script in Java
A useful note when using Python for the first time in a while
Use a scripting language for a comfortable C ++ life
Create an executable file in a scripting language
Register a task in cron for the first time
Check if it is Unix in the scripting language
Check if it is Unix in the scripting language
Machine language embedding in C language
A note on the default behavior of collate_fn in PyTorch
How to temporarily implement a progress bar in a scripting language
Create a script for your Pepper skill in a spreadsheet and load SayText directly from the script
Process the files in the folder in order with a shell script
[Note] A shell script that checks the CPU usage of a specific process in a while loop.
[Note] Import of a file in the parent directory in Python
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
I wrote a script that splits the image in two
Use a scripting language for a comfortable C ++ life 5 --Use the Spyder integrated environment to check numerical data-
Created gomi, a trash can tool for rm in Go language
Save the Pydrive authentication file in a different directory from the script
[Introduction to Python] How to use the in operator in a for statement?
Change the bash prompt to a simple color for easy viewing
Dockerfile with the necessary libraries for natural language processing in python
Read the config file in Go language! Introducing a simple sample
How to determine if a shell script was started in bash
A programming language that young people will need in the future
A note about get_scorer in sklearn
Get the script path in Python
A little script for malware self-defense
Switch the language displayed in Django 1.9
Start SQLite in a programming language
A note for writing Python-like code
A story about trying to improve the testing process of a system written in C language for 20 years
Add syntax highlighting for the Kv language to Spyder in the Python IDE
Let's use a scripting language for a comfortable C ++ life 2 Automatically generate C ++ source
Use a scripting language for a comfortable C ++ life 4-Use your own C ++ library from a scripting language-
Guidelines for reincarnating in the world of linux programming development (C / C ++ language)
AtCoder writer I wrote a script to aggregate the contests for each writer
Use a scripting language for a comfortable C ++ life 3-Leave graphing to matplotlib-
View the full path (absolute path) of a file in a directory in Linux Bash
What Java users thought of using the Go language for a day
In creating a model for discriminating tweet emotions with LSTM + Embedding, I reaffirmed the importance of preprocessing in NLP.
The most sought after programming language in 2020
Write the test in a python docstring
Find the dates for a jarring tournament
Documents on approaches for embedding in ARM
Cut out A4 print in the image
Looking back on 2016 in the Crystal language
MongoDB for the first time in Python
Get a token for conoha in python
Draw graphs in the programming language Julia
Use Python3's Subprocess.run () in a CGI script
A note about doing the Pyramid tutorial
Arrange the numbers in a spiral shape
Use something other than a <br> string for the <br> dict key in Python
I searched for the skills needed to become a web engineer in Python
Python script to get a list of input examples for the AtCoder contest
Turn multiple lists with a for statement at the same time in Python