Reputation: 51
I'm trying to send a parameter on jenkins and have the parameter recognized on the a python file.
Is there a command where I could do this on bash:
param = "Hello World"
param -> /usr/green/test.py
Also how does the py file know to grab the parameter?
Upvotes: 1
Views: 9598
Reputation: 171
One solution would be to print the parameter and process through a pipe with python.
Like:
read.py:
#!/usr/bin/python
import sys
pythonparams = sys.stdin.read()
# do something
then
echo "$param"|./read.py
Upvotes: 0
Reputation: 36
You have to put the script's arguments after the file path separated by spaces, like this:
/usr/green/test.py arg1 arg2 arg3
to pass the value of a bash variable to the script, you have to prefix the variable name with a $
(dollar sign):
param="Hello world"
/usr/green/test.py "$param"
The list of arguments in the python script can be accessed with sys.argv
. The first element in the list is the path to the script therefore the second element is the first argument.
Example /usr/green/test.py (Python 3):
import sys
print(sys.argv[1])
bash input:
param="Hello World"
python3 /usr/green/test.py "$param"
bash output:
Hello World
Note: You have to put the variable name in quotes or Hello and World will be passed as two separate arguments to test.py.
To execute the script without the python
command, you can add a #!
(shebang) line:
#!/usr/bin/python3
import sys
print(sys.argv[1])
Make the script executable by typing in bash:
chmod +x /usr/green/test.py
Upvotes: 0
Reputation: 1804
you can pass parameters from BASH to you python script via using sys egg in the python scripts. then pass on the environment variables in the command .
PyScript
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
command in BASH :
$python test.py $BUILD_ID $BUILD_URL
output :
Number of arguments: 2 arguments.
Argument List: ['test.py', '107', 'http://0.0.0.0:8080/artefact/builds/24']
Upvotes: 4
Reputation: 2094
You can access parameters passed to the script this way:
import sys
print sys.argv[1]
~ python test.py 'hello world'
Upvotes: 3