将参数传递给 shell 命令

时间:2021-07-25 15:09:22

标签: python bash shell

我有一个在 python 脚本中执行的 shell 脚本,我想将两个变量作为参数传递给脚本。我使用了 $ 符号,但它不起作用。 这是shell脚本:

path='/home/ranim/Documents/mmdet/media/Page_00287_jpg.rf.6a823c5ce3894f223a0e8f4ec20b11b2/article1'
save='/home/ranim/Documents/mmdet/media/Page_00287_jpg.rf.6a823c5ce3894f223a0e8f4ec20b11b2/article1/annotations.json'
call('python "/home/ranim/Documents/mmdet/mmdetection/tools/test.py" \
"/home/ranim/Documents/mmdet/models/second_model_conf.py" \
"/home/ranim/Documents/mmdet/models/second_model_checkpoint.pth" \
--cfg-options data.test.ann_file=$save \
data.test.img_prefix=$path \
--format-only \
--options "jsonfile_prefix=path"', shell=True)

1 个答案:

答案 0 :(得分:2)

我猜字符串插值可用于将您的 Python 变量传递给 args 命令的 call。例如,如果您使用 fstring,代码将如下所示:

path='/home/ranim/Documents/mmdet/media/Page_00287_jpg.rf.6a823c5ce3894f223a0e8f4ec20b11b2/article1'
save='/home/ranim/Documents/mmdet/media/Page_00287_jpg.rf.6a823c5ce3894f223a0e8f4ec20b11b2/article1/annotations.json'
call(f'python "/home/ranim/Documents/mmdet/mmdetection/tools/test.py" \
"/home/ranim/Documents/mmdet/models/second_model_conf.py" \
"/home/ranim/Documents/mmdet/models/second_model_checkpoint.pth" \
--cfg-options data.test.ann_file={save} \
data.test.img_prefix={path} \
--format-only \
--options "jsonfile_prefix=path"', shell=True)