Pages

Wednesday, December 30, 2015

Starting off with a shebang

Home

Sha-Bang is the character sequence consisting of the characters number sign and exclamation mark (that is, "#!") at the beginning of a script.

When a script with a She-Bang is run as a program, the “program loader” parses the rest of the script's initial line as an “interpreter directive” Lets understand a bit what exactly these terms mean.
#!interpreter [optional-arg]

  • The interpreter must be an absolute path to an executable program 
  • The optional‑arg should either not be included or it should be a string that is meant to be a single argument 

Some typical shebang lines:

  • #!/bin/sh — Execute the file using sh, the Bourne shell, or a compatible shell
  • #!/bin/csh -f — Execute the file using csh, the C shell, or a compatible shell, and suppress the execution of the user’s .cshrc file on startup
  • #!/usr/bin/perl -T — Execute using Perl with the option for taint checks
  • #!/usr/bin/env python2 — Execute using Python by looking up the path to the Python interpreter automatically via env


Invoking a script with three versions

Considering that you have a script ready with you, you can invoke it by
sh scriptname
./scriptname ( This needs to make the script itself directly executable with a chmod.)
Either:
chmod 555 scriptname (gives everyone read/execute permission)
or
chmod +rx scriptname (gives everyone read/execute permission)
or
chmod u+rx scriptname (gives only the script owner read/execute permission)

Extra tricks with Sha-Bang
Try setting other values for Sha-Bang interpreter
- /bin/rm
- Multiple Sha-Bang in single script

No comments:

Post a Comment