【问题标题】:How to run a script shell in google Colab?如何在谷歌 Colab 中运行脚本 shell?
【发布时间】:2022-03-24 22:22:15
【问题描述】:

我想在 colab 中运行脚本 shell,我使用了“!”我也试过“%%shell”

【问题讨论】:

    标签: python shell google-colaboratory


    【解决方案1】:

    %%shell! 应该可以工作。我怀疑你的 shell 脚本不在你当前的工作目录中。

    您可以通过运行%ls检查当前目录的内容

    以下是运行 shell 脚本的完整示例: https://colab.research.google.com/drive/1i5lHPcsmcgeoFEGg0Dfwjhblsm2iMExP

    【讨论】:

      【解决方案2】:

      如果你在 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?')
      

      无论如何,最好将外壳单元分开。

      【讨论】:

        【解决方案3】:

        在 Colab 中

        !chmod u+x test.sh
        !./test.sh
        

        以上是你需要做的。

        !test.sh 查找命令,因此它永远不会工作,因为您的是本地脚本。 如果你需要它作为命令将它移动到 $PATH 目录

        【讨论】:

        • 在原始问题中,OP 已经尝试使用 ! 并且正在运行一个 shell 脚本,而不是像 ls 这样的 shell 命令:“我想在 colab 中运行一个脚本 shell !testAllLatin.sh.
        猜你喜欢
        • 2015-04-19
        • 2021-08-12
        • 1970-01-01
        • 2019-02-19
        • 2018-05-06
        • 2022-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多