For passing only one argument, you can have a python script like this:
import subprocess
correctBc4jfiles = ["1","2","3"]
subprocess.call(["/bin/bash", "/path/to/mail.sh"] + correctBc4jfiles)
And have a bash script like this:
#!/bin/bash
# I want to know the number of files the correctBc4jfiles variable holds
echo "$#"
# I want to iterate this variable correctBc4jfiles. how will i iterate it in shell script.
for A in "$@"; do
echo "$A"
done
# I want to assign the value of the correctBc4jfiles variable into another array variable which belongs to the shell script itself.
ANOTHER=("${@}")
# for A in "${ANOTHER[@]}"; do ...
Can we pass more than one variable from python to shell script? Like in this case we are passing only one variable correctBc4jfiles? How can I do this?
You have to make your python script pass the number of values for every variable as well like:
import subprocess
var0 = ["|1|", "|2|", "|3|"]
var1 = ["|4|", "|5|", "|6|"]
subprocess.call(["/bin/bash", "script.sh"] + [str(len(var0))] + var0 + [str(len(var1))] + var1)
And let bash interpret it:
#!/bin/bash
varprefix='var'
varindex=0
declare -i count
while [[ $# -gt 0 ]]; do
count=$1
if [[ count -gt 0 ]]; then
eval "${varprefix}$(( varindex++ ))=(\"\${@:2:count}\")"
shift "$count"
fi
shift
done
set | grep ^var ## Just show what variables were made.
It gives an output like this:
var0=([0]="|1|" [1]="|2|" [2]="|3|")
var1=([0]="|4|" [1]="|5|" [2]="|6|")
varindex=2
varprefix=var
So with that you could already use varX.
If you don't like the varX format, you could just copy the values to desired array variable:
myarray1=("${var0[@]}")
myarray2=("${var1[@]}")
subprocess.call(['./mail.sh'] + correctBc4jfiles)
then bash away at the command line args – Ryan Haining Aug 29 '13 at 5:02subprocess.call(['./mail.sh'] + correctBc4jfiles, shell=True)
– Ryan Haining Aug 29 '13 at 14:30