0

I am having problem in sending bash array problem_list to python script update_contest.py. Here is my code :

bash file :

echo $problems
declare -a problem_list
for i in $problems;
do
    problem_list+=($i)
done
echo ${problem_list[@]}
python3 update_contest.py $id ${problem_list[@]}

python file:

id = (sys.argv)[1]
problem_list = (sys.argv)[2]
print(problem_list)

output in terminal :

A B C D E
A B C D E
A

As can be seen, only A is passed as argument in problem_list.

1 Answer 1

2

You are indexing the list of arguments with 2, so only returning its second element, which is the first argument.

Replace this line problem_list = (sys.argv)[2] with problem_list = sys.argv[2:-1].

https://docs.python.org/3/library/sys.html

2
  • Thanks a lot ! it worked. Actually I thought I need to send array as single argument. But, this approach also worked. May 18, 2019 at 5:38
  • @PratikKale You can't pass any real data structure as an argument to a unix program; the arguments are just strings, so to "pass an array" you turn it into a list of strings and pass each of those strings as a separate argument. BTW, the safe way to do this is "${problem_list[@]}" -- the double-quotes prevent the shell from trying to parse the array elements, and mangling them in the process. Putting double-quotes around variable references in the shell is almost always a good idea. May 18, 2019 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.