Passing arguments to a script
Passing arguments to a script is useful because a script can be used repeatedly without modification.
The arguments you pass on the command line are passed as values in the list
sys.argv
. You can use the len(sys.argv)
command to obtain the
number of values passed. 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 you can use the existing methods for this class, such as argv
.
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