在python脚本中调用shell脚本时,如何将变量从python传递到shell脚本
情景: 我有一个名为在python脚本中调用shell脚本时,如何将变量从python传递到shell脚本,python,bash,shell,Python,Bash,Shell,情景: 我有一个名为bc4j.py的python脚本,它有一个全局变量 correctBc4jfiles = [] 此变量包含所有文件的列表 现在我想在这个python脚本和那个shell脚本中调用另一个shell脚本,比如mail.sh 我想知道correctBc4jfiles变量包含的文件数 我想迭代这个变量correctBc4jfiles。我将如何在shell脚本中迭代它 我想将correctBc4jfiles变量的值赋给另一个数组变量,该变量属于shell脚本本身 我们可以从pyth
bc4j.py
的python脚本,它有一个全局变量
correctBc4jfiles = []
此变量包含所有文件的列表
现在我想在这个python脚本和那个shell脚本中调用另一个shell脚本,比如mail.sh
correctBc4jfiles
变量包含的文件数correctBc4jfiles
变量的值赋给另一个数组变量,该变量属于shell脚本本身correctBc4jfiles
?我该怎么做- 单步浏览列表,然后为每个文件调用一次
。这是最可靠的方法mail.sh
- 将列表中的项目转换为空格分隔的字符串,然后将其传递到
(假设mail.sh
设置正确,它应该接受多个参数)mail.sh
['a.txt'、'b.txt'、'c.txt']
批量传递到脚本。您可以:
- 单步浏览列表,然后为每个文件调用一次
。这是最可靠的方法mail.sh
- 将列表中的项目转换为空格分隔的字符串,然后将其传递到
(假设mail.sh
设置正确,它应该接受多个参数)mail.sh
您不能做的是将
['a.txt'、'b.txt'、'c.txt']
整体传递到脚本。对于只传递一个参数,您可以使用如下python脚本:
import subprocess
correctBc4jfiles = ["1","2","3"]
subprocess.call(["/bin/bash", "/path/to/mail.sh"] + correctBc4jfiles)
#!/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 ...
var0=([0]="|1|" [1]="|2|" [2]="|3|")
var1=([0]="|4|" [1]="|5|" [2]="|6|")
varindex=2
varprefix=var
还有一个bash脚本,如下所示:
import subprocess
correctBc4jfiles = ["1","2","3"]
subprocess.call(["/bin/bash", "/path/to/mail.sh"] + correctBc4jfiles)
#!/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 ...
var0=([0]="|1|" [1]="|2|" [2]="|3|")
var1=([0]="|4|" [1]="|5|" [2]="|6|")
varindex=2
varprefix=var
我们可以从python向shell脚本传递多个变量吗?就像在本例中,我们只传递一个变量correctBc4jfiles?我该怎么做
您必须让python脚本为每个变量传递值的数量,如:
import subprocess
var0 = ["|1|", "|2|", "|3|"]
var1 = ["|4|", "|5|", "|6|"]
subprocess.call(["/bin/bash", "script.sh"] + [str(len(var0))] + var0 + [str(len(var1))] + var1)
让bash来解释它:
#!/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.
它给出如下输出:
import subprocess
correctBc4jfiles = ["1","2","3"]
subprocess.call(["/bin/bash", "/path/to/mail.sh"] + correctBc4jfiles)
#!/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 ...
var0=([0]="|1|" [1]="|2|" [2]="|3|")
var1=([0]="|4|" [1]="|5|" [2]="|6|")
varindex=2
varprefix=var
这样你就可以使用varX了
如果不喜欢varX格式,可以将值复制到所需的数组变量:
myarray1=("${var0[@]}")
myarray2=("${var1[@]}")
为了只传递一个参数,可以使用如下python脚本:
import subprocess
correctBc4jfiles = ["1","2","3"]
subprocess.call(["/bin/bash", "/path/to/mail.sh"] + correctBc4jfiles)
#!/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 ...
var0=([0]="|1|" [1]="|2|" [2]="|3|")
var1=([0]="|4|" [1]="|5|" [2]="|6|")
varindex=2
varprefix=var
还有一个bash脚本,如下所示:
import subprocess
correctBc4jfiles = ["1","2","3"]
subprocess.call(["/bin/bash", "/path/to/mail.sh"] + correctBc4jfiles)
#!/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 ...
var0=([0]="|1|" [1]="|2|" [2]="|3|")
var1=([0]="|4|" [1]="|5|" [2]="|6|")
varindex=2
varprefix=var
我们可以从python向shell脚本传递多个变量吗?就像在本例中,我们只传递一个变量correctBc4jfiles?我该怎么做
您必须让python脚本为每个变量传递值的数量,如:
import subprocess
var0 = ["|1|", "|2|", "|3|"]
var1 = ["|4|", "|5|", "|6|"]
subprocess.call(["/bin/bash", "script.sh"] + [str(len(var0))] + var0 + [str(len(var1))] + var1)
让bash来解释它:
#!/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.
它给出如下输出:
import subprocess
correctBc4jfiles = ["1","2","3"]
subprocess.call(["/bin/bash", "/path/to/mail.sh"] + correctBc4jfiles)
#!/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 ...
var0=([0]="|1|" [1]="|2|" [2]="|3|")
var1=([0]="|4|" [1]="|5|" [2]="|6|")
varindex=2
varprefix=var
这样你就可以使用varX了
如果不喜欢varX格式,可以将值复制到所需的数组变量:
myarray1=("${var0[@]}")
myarray2=("${var1[@]}")
subprocess.call(['./mail.sh']+correctBc4jfiles)
然后猛击命令行arg,即使它没有调用文件也不起作用。如果执行subprocess.call(['./mail.sh']+correctBc4jfiles,shell=True)
subprocess.call(['./mail.sh']+correctBc4jfiles),它会找到它吗
然后猛击命令行arg,它不工作,即使它也不调用文件。如果执行子流程,它是否找到它。调用(['./mail.sh']+correctBc4jfiles,shell=True)
我已经尝试过了,但不工作。。。。你能告诉我怎么做吗?因为我想执行上面提到的4个动作,我已经试过了,但没有工作。。。。你能不能给我一个清晰的想法,如何做到这一点,因为我想执行上述4个动作