我有以下查找命令。我将使用python调用这些命令并运行它们。这是我一次运行这些命令的一个要求。
查找命令
find /path/to/files/ -type f -mtime +3 -exec rm -f {} \;
find /path/to/files2 -type f -mtime +6 -exec rm -f {} \;当我像这样在bash中运行它们时,我会得到一个错误查找:路径必须在表达式之前:‘find’
find /path/to/files/ -type f -mtime +3 -exec rm -f {} \; find /path/to/files2 -type f -mtime +6 -exec rm -f {} \;但是,当我像这样运行它时,我不会出错。
find /path/to/files/ -type f -mtime +3 -exec rm -f {} \; && find /path/to/files2 -type f -mtime +6 -exec rm -f {} \;我想理解为什么我在&&上没有错误。以及运行连续find命令的最佳方法是什么。
这是我的一个要求,我应该运行如下。我更希望知道一个可以完成以下任务的解决方案,但我也愿意听取一些建议。
cmd1 = 'find ... \; '
cmd2 = 'find ... \; '
cmd3 = 'find ... \; '
# This utilises string concatenation
full_cmd = cmd1 + cmd2 + cmd3
# I need to run them in one call.
# It's hard (not impossible) to change this since it is a part of a bigger code base
python_func_which_runs_bash(full_cmd)发布于 2020-06-10 15:02:19
使用&&的命令确实会连续运行这些命令,但是只有当第一个命令成功退出时,第二个命令才会运行。如果要无条件地运行第二个命令,可以使用;代替&&。
请注意,转义的\;不算--它是精确地转义的,这样shell就不会将它看作一个连接,而是将它作为一个普通参数传递给find。
另请参阅:
https://unix.stackexchange.com/questions/592078
复制相似问题