Passing Arguments to a Script

Passing arguments to a script is useful as it means a script can be used repeatedly without modification. The arguments that are passed on the command line are passed as values in the list sys.argv. The number of values passed can be obtained by using the command len(sys.argv). For example:

import sys
print "test1"
print sys.argv[0]
print sys.argv[1]
print len(sys.argv)

In this example, the import command imports the entire sys class so that the methods that exist for this class, such as argv, can be used.

The script in this example can be invoked using the following line:

/u/mjloos/test1 mike don

The result is the following output:

/u/mjloos/test1 mike don
test1
mike
don
3