python - 用 sarge 运行 shell 脚本会吃掉我的引号

标签 python linux shell subprocess sh

我正在尝试使用 sarge 执行 shell 脚本图书馆。 我的文件 script.sh 包含以下内容:

#!/usr/bin/env sh
export LOCAL_PROJECT_ROOT=/path/to/the/dir
inotifywait -mr --exclude '(.git|.idea|node_modules)' \
-e modify,create,delete ${LOCAL_PROJECT_ROOT} --format '%w%f'

然后我像使用 sarge 一样运行它:

sarge.run('sh script.sh')

我看到 htop 没有 inotifywait 进程在运行。 但是,当我使用 sh script.sh 直接在 shell 中运行此脚本时,一切都按预期进行。

如果我删除 --exclude--format 都包含引号参数的部分,sarge 也能正常运行。

如果我将脚本重写成这样,它也可以在 sarge 上运行良好:

echo "inotifywait -mr --exclude '(.git|.idea|node_modules)' -e modify,create,delete ${LOCAL_PROJECT_ROOT} --format '%w%f'" | /bin/sh

最佳答案

听起来像是模块问题。

因为:

Python 2.7.6 (default, Nov 23 2017, 15:49:48) 
[GCC 4.8.4] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> 'a' == "a"
True
>>> '%s' %"'a'"
"'a'"
>>> "%s" %'"a"'
'"a"'
>>> 

也许:

#!/usr/bin/env sh
export LOCAL_PROJECT_ROOT=/path/to/the/dir
inotifywait -mr --exclude "(.git|.idea|node_modules)" -e modify,create,delete ${LOCAL_PROJECT_ROOT} --format "%w%f"
  1. 转义字符未定义 '\' check this
  2. 下一行字符是\n,但你得到的是“\\n”
  3. 你只运行 inotifywait -mr --exclude '(.git|.idea|node_modules)' 这对 shell 牌来说还不够。

关于python - 用 sarge 运行 shell 脚本会吃掉我的引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47967211/

相关文章:

python - 如何将 DRF 序列化器与 Graphene 一起使用

c - 为什么 printk() 只在内核模块的 init/exit 方法中起作用? (优先级应该没问题)

shell - 如何执行存储在变量中的命令?

python - 使用Twython发推文,twitter api报错

python - 在 Python 的 Selenium webdriver 中使用带有 headless (headless) firefox 的 http 代理

Python Change Master/Application Volume

java - Websphere jdk升级

linux - 压缩和删除具有特定年龄的文件

bash - shell 脚本 : with awk, 如果找到 string0 则写入 string1,否则写入 string2

git - 如何将 Bitbucket post-commit Hook shell 脚本正确转换为 Powershell 脚本?