0

I have a python script that takes an argument and return a multiplication. I have this shell script pythoncall.sh: python /user/mult.py var1

I want to run this for different argument values in a loop: second.sh

for i in 1 2 3
  do 
  var1=$i
  ./pythoncall.sh
done

but i don't get the result because i don't pass the variable i think. my error is IndexError: list index out of range.

the python script:

import sys
def main(argv):

    num1=sys.argv[1]
    len1=int(num1)*3
    print(len1)

if __name__ == '__main__':
    main(sys.argv)
sebis
  • 1
  • 1
  • 5

1 Answers1

0

So, the direct answer is you need to export the variable for the second shell script to be able to see it:

export var1=$i

but why do you need the variable and second shell script at all? Why not just call the python script directly:

for i in 1 2 3
do
  python /user/mult.py $i
done
Phydeaux
  • 2,795
  • 3
  • 17
  • 35
  • I tried with export and still i can not run it, ValueError: invalid literal for int() with base 10: 'var1'. I have to do it in this form. – sebis Nov 19 '20 at 14:27
  • Ah, you are passing the literal string `"var1"` to Python. Change the second shell script to `python /user/mult.py $var1` (with the `$` sign) – Phydeaux Nov 19 '20 at 14:33
  • I add it and i have the problem : IndexError: list index out of range. – sebis Nov 19 '20 at 14:41