Definition: A script is a collection of instructions written in a given interpreted programming language such as Bash, Python, or R) and gathered in a text file. If given a script, the corresponding interpreter (such as the Bash interpreter, the Python interpreter, or the R interpreter) will execute all the instructions contained in the script in order from the first line to the last line. In other words, a script is a program that is written in an interpreted programming language (as opposed to compiled programming language such as C, C++, and Java).

Usually, one choose a text editor (such as vi, emacs, gedit, notepad, etc. ) to write a script. Then one invoke the interpreter on the script by typing the interpreter name on the command line, followed by the path indicating where the script file is located. For instance:

Rscript MyScript.R

In this notebook, we will also learn how to use the ipython notebook as an editor to write your script from it, and how to run them from within the notebook.

Remark: The file extension .sh, .py, .R etc. in the script file names above are just conventions; it is not needed by the interpreter in order to execute the script. It's just nice, since you know the programming language just by looking at the file name.

The notebook as an editor

The notebook editor

The notebook editor is called by using the magic command

%%file filename

at the beginning of a code cell.

In [1]:

print 'Hellow world'


Running the script from the notebook

Two solutions:

Solution 1 involve using a bash cell:

In [2]:


Hellow world

Solution 2: Use the magic command


In [3]:

Hellow world

Script input and output

A script can be seen as one of the programming embodiments of the mathematical idea of a mathematical function; that is, a recipe $f$, denoted in math using the expressive arrow symbologism $f:A\rightarrow B$, that knows how to transform any element $a\in A$ from an input set $A$ into an output element $f(b)\in B$ belonging to an output set $B$. (Later on, we will see that there is a also a notion of function in Python, which can be used withing scripts, and which can be regarded as mini-scripts withing scripts).

The main difference between mathematical functions and their programmatic embodiements is that in programming the input set can be split into different "physical" components. For instance, a script can take its arguements from a default place called the standard input, as well as from files stored on the file system (or the internet) and command parameters. Here is a drawing summarizing the various input and output source for a script:

Input from command parameters

Import the argv predifined list from the module sys:

from sys import argv

Then unpack the command arguments as in the example above.

In [21]:

from sys import argv

arg0, arg1, arg2, arg3, arg4, arg6 = argv

print arg3


Input and output from files

In [26]:

echo 'Hello I am the data in the file!' > inputData.txt

In [36]:

inputFile = open('inputData.txt')

data =

outputFile = open('output.txt', 'w')




In [37]:

Standard input, output, and error streams: stdin, stdout, stderror

In [52]:

from sys import stdin, stdout, stderr


stderr.write('Aie! Aie! Aie! Big catastroph! Deadly mistake')



In [53]:

Aie! Aie! Aie! Big catastroph! Deadly mistake

In [ ]: