Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 从另一个文件(最好是python)替换shell脚本中的变量_Bash_Python 2.7_Shell - Fatal编程技术网

Bash 从另一个文件(最好是python)替换shell脚本中的变量

Bash 从另一个文件(最好是python)替换shell脚本中的变量,bash,python-2.7,shell,Bash,Python 2.7,Shell,我的shell脚本: #!/bin/sh ..... a='30' b='2' c='0.9' ..... 有没有办法用python脚本/任何其他语言替换变量的值?我应该能够用多个值替换这些值。例如,我应该能够设置b='2 1 3'和a='0.9 0.8'..很简单:在Bash脚本的顶部添加一行: source thevars.sh 然后,您可以使用一个简单的Python脚本编写所需的所有变量赋值,以创建thevars.sh 如果thevars.sh不存在,可以稍微修改Bash脚本

我的shell脚本:

#!/bin/sh
.....
a='30'  
b='2'  
c='0.9'
..... 

有没有办法用python脚本/任何其他语言替换变量的值?我应该能够用多个值替换这些值。例如,我应该能够设置b='2 1 3'和a='0.9 0.8'..

很简单:在Bash脚本的顶部添加一行:

source thevars.sh
然后,您可以使用一个简单的Python脚本编写所需的所有变量赋值,以创建
thevars.sh

如果
thevars.sh
不存在,可以稍微修改Bash脚本,使其具有默认参数:

test -f thevars.sh && source thevars.sh

a=${a-30}
b=${b-2  
c=${c-0.9}
然后,如果
thevars.sh
分配了任何变量,则将使用这些变量而不是默认值——例如:

a="foo bar"
b=9001

“替换”是什么意思?比如让Python脚本编辑shell脚本,或者让shell脚本从Python脚本中提取变量值?Python脚本编辑我的shell脚本值您还可以使用预命令修饰符调用脚本,如
a=50./script.sh
;请注意,
thevars.sh
中的定义将覆盖这样的赋值。