-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes-passing_args.txt
36 lines (28 loc) · 975 Bytes
/
notes-passing_args.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
.-----------------------------------.
| November 16th, 2016 |
| Passing Arguments into a script |
'-----------------------------------'
# Making a script executable without calling python first, place
# the below 'shebang' at the top of the file: (include the #)
#!/usr/bin/env python
# Use 'input' to call a library.
# A library for accepting arguments such as input is 'sys'
import sys
# Simple statement to print arguments passed to the script. This returns a list.
print sys.argv
$ ./script.py whatever another things
['./temp.py', 'whatever', 'another', 'things']
# Accepting input with some decisionmaking:
#!/usr/bin/env python
import sys
if len(sys.argv) == 2:
ip_addr = sys.argv.pop()
print("The IP address is {}".format(ip_addr))
else:
print("You made an error.")
$ ./temp.py 10.1.1.1
The IP address is 10.1.1.1
$ ./temp.py 10.1.1.1 120.1.1.1
You made an error.
$ ./temp.py 10.1.1.1 120.1.1.1 1.1.1.1
You made an error.