Quantcast
Channel: Linux by Examples » which
Viewing all articles
Browse latest Browse all 7

writting executable script

$
0
0

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

Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images