Py学习  »  Python

在bash中激活虚拟环境并在一个命令中执行python脚本?

bart cubrich • 3 年前 • 1533 次点击  

说我有文件 /templates/apple 我想

  1. 把它放在两个不同的地方然后
  2. 去掉原稿。

所以 /模板/苹果 将被复制到 /templates/used /templates/inuse 然后我想把原稿去掉。

cp 最好的方法是 rm ? 还是有更好的方法?

我想在一行中完成这一切,所以我认为它看起来像:

cp /templates/apple /templates/used | cp /templates/apple /templates/inuse | rm /templates/apple

这是正确的语法吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129967
 
1533 次点击  
文章 [ 6 ]  |  最新文章 3 年前
Peter Mortensen Renaud
Reply   •   1 楼
Peter Mortensen Renaud    7 年前

在我看来,使用管道似乎很奇怪。无论如何,你应该使用逻辑推理 and Bash操作员:

$ cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apples

如果 cp 命令失败时 rm 不会被处决。

或者,可以使用 for 循环和 cmp .

Peter
Reply   •   2 楼
Peter    14 年前

试试这个。。

cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple

Eyal Levin
Reply   •   3 楼
Eyal Levin    7 年前

另一个选择是打字 Ctrl+V Ctrl+J 在每个命令的末尾。

示例(替换) # 具有 Ctrl+V Ctrl+J ):

$ echo 1#
echo 2#
echo 3

输出:

1
2
3

无论之前的命令是否失败,这都将执行这些命令。

等同于: echo 1; echo 2; echo 3

如果要停止执行失败的命令,请添加 && 在每行的末尾,最后一行除外。

示例(替换) # 具有 Ctrl+V Ctrl+J ):

$ echo 1 &&#
failed-command &&#
echo 2

输出:

1
failed-command: command not found

在里面 zsh 你也可以使用 Alt+Enter Esc+Enter 而不是 Ctrl+V Ctrl+J

glenn jackman
Reply   •   4 楼
glenn jackman    14 年前

注意 cp A B; rm A 正是 mv A B 。它也会更快,因为您不必实际复制字节(假设目标位于同一文件系统上),只需重命名文件即可。所以你想要 cp A B; mv A C

sactiw Marc B
Reply   •   5 楼
sactiw Marc B    7 年前

为什么不呢? cp 到位置1,然后 mv 到位置2。这将负责“删除”原件。

不,这不是正确的语法。 | 用于“管道”一个程序的输出,并将其转换为下一个程序的输入。你想要的是 ; ,它分隔多个命令。

cp file1 file2 ; cp file1 file3 ; rm file1

如果您要求在启动下一个命令之前单个命令必须成功,那么您可以使用 && 相反:

cp file1 file2 && cp file1 file3 && rm file1

那样的话,如果 内容提供商 命令失败时 rm 不会跑。

Maxim Egorushkin
Reply   •   6 楼
Maxim Egorushkin    11 年前

你正在使用 | (管道)将一个命令的输出引导到另一个命令。你要找的是 && 操作员仅在前一个命令成功时执行下一个命令:

cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple

cp /templates/apple /templates/used && mv /templates/apple /templates/inuse

总结(非详尽地)bash的命令运算符/分隔符:

  • | 管道标准输出( stdout )将一个命令转换为另一个命令的标准输入。注意 stderr 不管发生什么,它仍然会进入默认的目的地。
  • |& 两条管道 斯特杜特 标准错误 将一个命令转换为另一个命令的标准输入。非常有用,在bash版本4及更高版本中提供。
  • && 执行 && 除非上一次成功。
  • || 执行 || 只是前一次失败了。
  • ; 执行 ; 始终不考虑上一个命令是否成功。除非 set -e 之前被调用,这导致 bash 因错误而失败。