How to pass Bash variables as Python arguments -
i'm trying figure out if possible pass values stored in variable in bash in argument values in python script.
we have python script use create dns records in bind , i've been tasked cleaning our outdated dns database. far have in bash:
hostnames=$(</users/test/test.txt) zone="zone name here" ip=$(</users/test/iptest.txt) host in $hostnames python pytest.py --host $hostnames --zone $zone --ip $ip done
unfortunately, don't have test environment can test on before run on prod , don't have experience python or bash scripting. i've done powershell scripting, wondering if have above work.
i've looked around on forums here have not found make sense of. post seems answer question somewhat, i'm still not sure how works. how pass bash variable python?
yes, seems work fine me.
to test, threw quick python script called test.py:
#!/usr/bin/python import sys print 'number of arguments: ', len(sys.argv) #we know sys.argv[0] script name, let's @ sys.argv[1] print sys.argv[1]
then, in terminal, set variable:
>testvar="testing"
and try script, passing variable script:
>python test.py $testvar >number of arguments: 2 >testing
admittedly i'm not well-versed in python, did seem accomplish wanted. said, assumes python script referenced set parse names of parameters being passed - you'll notice test script not that, spits out whatever pass it.