简体   繁体   English

从bash脚本执行python脚本时转义Bash通配符?

[英]Escaping Bash wildcards when executing python script from bash script?

I've got a bash script that defines an array of file globs containing * wildcards. 我有一个bash脚本,它定义了一个包含*通配符的文件globs数组。

The script executes a python script passing each of these globs as a parameter. 该脚本执行python脚本,将每个globs作为参数传递。

On the command line I can quote the entire glob, and python is happy with it. 在命令行上,我可以引用整个glob,而python对它很满意。

In the bash script, if I quote the globs, python's os.path.realpath seems to get confused with the quotes - which are passed in to the script itself. 在bash脚本中,如果我引用globs,python的os.path.realpath似乎与引号混淆 - 它们被传递给脚本本身。

eg bash script: 例如bash脚本:

BACKUP_PATHS=("/root/backups/db-dump-*" "/root/backups/*_backup.tar.bz2")
for path in "${BACKUP_PATHS[@]}"
do
  /usr/local/bin/python2.7 /usr/local/bin/clean.py \'$path\' $MAX_FILES
done

python: 蟒蛇:

os.path.realpath(path)    # results in something like: /usr/local/bin'/root/backups/db-dump-*'

How can I make them play together? 我怎样才能让他们一起玩?

Thanks 谢谢

Instead of \\'$path\\' , use "$path" . 而不是\\'$path\\' ,使用"$path" The double-quotes allow the contents of the variable $path to be substituted, but prevent wildcard expansion and ensure the path is passed as a single argument. 双引号允许替换变量$path的内容,但是防止通配符扩展并确保路径作为单个参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM