【发布时间】:2022-03-24 22:22:15
【问题描述】:
【问题讨论】:
标签: python shell google-colaboratory
【问题讨论】:
标签: python shell google-colaboratory
%%shell 或 ! 应该可以工作。我怀疑你的 shell 脚本不在你当前的工作目录中。
您可以通过运行%ls检查当前目录的内容
以下是运行 shell 脚本的完整示例: https://colab.research.google.com/drive/1i5lHPcsmcgeoFEGg0Dfwjhblsm2iMExP
【讨论】:
如果你在 shell 中,你不只是调用 .sh 文件——你应该在你自己的终端中得到同样的错误。您的 shell 没有在当前目录中查找 shell 命令,因此您需要在脚本中添加一些路径上下文以让 shell 知道它是一个实际的可运行程序,通常通过在脚本前添加一个点,例如,使用
$ . testAllLatin.sh
而不是
$ testAllLatin.sh
在 Unix Stack Exchange 网站上查看 What's the meaning of a dot before a command in shell?。最佳答案总结:
该上下文中的点表示将该文件的内容“来源”到当前 shell 中。
source本身就是一个 shell 内置命令。source和点运算符是同义词。
就 Colab 和 Notebooks 而言,%%shell 魔法将整个单元格作为 shell 中的命令运行。因此,您应该能够在单元格中简单地使用以下内容:
%%shell
. path/to/testAllLatin.sh
bang 只是在 shell 中运行那一行,所以如果你愿意,你可以穿插 Python。因此,您可以在单元格中执行以下操作:
print('this is Python stuff', 5+10)
!. path/to/testAllLatin.sh
print('is it all latin?')
无论如何,最好将外壳单元分开。
【讨论】:
在 Colab 中
!chmod u+x test.sh
!./test.sh
以上是你需要做的。
!test.sh 查找命令,因此它永远不会工作,因为您的是本地脚本。 如果你需要它作为命令将它移动到 $PATH 目录
【讨论】:
! 并且正在运行一个 shell 脚本,而不是像 ls 这样的 shell 命令:“我想在 colab 中运行一个脚本 shell :!testAllLatin.sh.