Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variable arguments from bash script to python script

I've been trying to solve this issue for sometime now with no luck. The crust of the situation is that I'm using a bash script to send parameters to a a python script:

Example:

foo.sh calls bar.py....the call looks like: bar.py $var1 $var2 ... $varn

The python script then prints all the arguments using the sys.argv array. The python script works correctly from the command line, but when called from with the bash script (i.e foo.sh), I get no output from bar.py.

Also, I started foo.sh with the "#!/bin/bash -x" option and watched the output as well.

TO summarize:

  • Two scripts, foo.sh and bar.py
  • foo.sh calls bar.py, passing variables of foo.sh as arguments to bar.py
  • bar.py prints the arguments it sees using sys.argv
  • bar.py works when run from its own terminal, doesn't work when called from foo.sh

Any help would be awesome!!!!

Thanks!

Edit: Hi all, thanks for the replies, the complete code is pretty long...but... the contents of the two scripts could be summed

foo.sh ____

#!/bin/bash
declare -a list1;  
declare -a list2;  

list1=("foo" "bar" "please");  
list2=("foo" "bar" "please" "help");  

declare -a joined;  

joined=( $(bar.py "${list1[@]}" "${list2[@]}" ) );

bar.py ____

#!/bin/python
import sys  

for arg in sys.argv:  
    print arg  

As I assume all the indents in the python are correct (not sure how StackOverflow does this yet :) ). These two represent the core of the issue i'm having. As stated, bar.py prints arguments correctly, when it it not called from foo.sh.

PS: I did mean to say "crust"

like image 486
certifiedNoob Avatar asked Oct 17 '10 22:10

certifiedNoob


People also ask

How do you pass variables to a Python script?

How to pass information to a Python script using the sys. argv command by hard-coding the input variables in Jupyter Notebook or through the interactive use of the input() function.

How do you pass arguments to function while executing a Python script from terminal?

The best choice is using the argparse module, which has many features you can use. So you have to parse arguments in your code and try to catch and fetch the arguments inside your code. You can't just pass arguments through terminal without parsing them from your code.

How do you pass a variable as an argument in shell script?

Using arguments Inside the script, we can use the $ symbol followed by the integer to access the arguments passed. For example, $1 , $2 , and so on. The $0 will contain the script name.


Video Answer


2 Answers

Edit, since code has been posted

Your code is doing the correct thing - except that the output from your bar.py script is being captured into the array joined. Since it looks like you're not printing out the contents of joined, you never see any output.

Here's a demonstration:

File pybash.sh

#!/bin/bash

declare -a list1
declare -a list2

list1=("Hello" "there" "honey")
list2=("More" "strings" "here")

declare -a joined

joined=($(./pytest.py ${list1[@]} ${list2[@]}))
echo ${joined[@]}

File pytest.py

#!/usr/bin/python

import sys

for i in sys.argv:
    print "hi"

This will print out a bunch of 'hi' strings if you run the bash script.

like image 70
逆さま Avatar answered Sep 20 '22 19:09

逆さま


EDIT:

I figured it out, I had some weired combo of characters, the line was not properly escaped. I changed it from

var=( $( some commands) )

to

var=( some commands ) // using backticks (still learning the SO editor...)

Bash escaping is some ride lol! To those who answered, thanks for all your help

like image 39
certifiedNoob Avatar answered Sep 22 '22 19:09

certifiedNoob