In one of the many new and interesting things that I learnt this week, I discovered that you can use the hash-bang operator not only to make a file run with a specific system binary (e.g. #!/bin/bash
) but with any executable file, even if it’s local, just like the example below:
$ cat myfile
#!./interpret
hai there
$ cat interpret
#!/usr/bin/env python
import sys
input = open(sys.argv[1]).read()
print("File contains %s" % (input.splitlines()[1:]));
$ ./myfile
File contains ["hai there"]
The same interpretor functionality in ruby would be something along the lines of
$ cat ruby_interpret
#!/usr/bin/ruby
input = File.readlines(ARGV[0])
puts "File contains: #{input[1..-1]}"
In both cases, the interpreter gets the command line argument via unix-black magic, reads in the file, and echos it’s contents to stdout, purposefully skipping the first line in both cases.
#TheMoreYouKnow