Live Chat!

writting executable script

2 votos Vota!!

October 8th, 2006 mysurface Posted in Bash, Developer, chmod, file, python, sh, which | Hits: 23800 |

Shell script (sh), Bash script (bash), Python and perl are all scripting language. By default, to run a scripting file, for example Python script, you need to do this:

python myscript.py

Another example for shell script:

sh myscript.sh

But we make is executable and can execute by running directly like this

./myscript.py

If you place your script into directory that exported into your PATH, such as /usr/bin; you can run without specified ./

How to make your script executable that?

  1. Add the path of script interpreter at the first line of your script.
  2. Change mode to executable

Let say I want to make my python script executable, I add this entry at the first line of myscript.py:

#!/usr/bin/python

Then i change mode to executable:

chmod +x myscript.py

For shell script, add this entry:

#!/bin/sh

You can use “which” to check the real path of the interpreter, like this

which sh

Another advantage of adding “sha bang” (#!….) is the script will be recognize by file.

file myscript.sh

If you do not add “sha bang”, file will display

myscript.sh: ASCII text

But after you add that,

myscript.sh: Bourne shell script text executable

3 Responses to “writting executable script”

  1. We may use the env instead if we don’t know the exact location of python:

    #! /usr/bin/env python

    More at:
    python FAQ, python’s mailling list discussion

  2. how can I write an executable script using the linux that computes summary statistics for the variables x1,x2,and x3. Include: n, no. of missing observations, mean, variance, standard deviation, and minimum and maximum values.

  3. how to compute correlattions among three variables?

Leave a Reply

Security Code: