如何在Google Colab的ipython上运行shell脚本文件

양유경*_*양유경 2 sh ipython jupyter-notebook google-colaboratory

我想知道如何在Google Colab的ipython(jupyter notbook)上运行bash出售脚本文件。我从github下载了一个深度学习代码包,并将其上传到我的Google驱动器上,然后将goole驱动器安装到了Google Colab上。该代码包包含“ * .py” python代码和“ fn.sh”脚本文件。通过执行脚本文件,可以执行python代码。

我在Google Colab的ipython提示符下尝试了os.system('fn.sh')和subprocess.call('fn.sh'),但它们无法像下面那样工作。

1)

导入操作系统os.system('驱动器/DL/denet-master/examples/simple-cifar10.sh')32256

2)

导入子进程subprocess.call('drive / DL / denet-master / examples / simple-cifar10.sh')OSError:[Errno 8] Exec格式错误:'drive / DL / denet-master / examples / simple-cifar10.sh '

Nin*_*ham 11

选项1

!作为提到的其他答案使用。

!ls -la
!echo "Hello"
!bash path/to/script.sh
Run Code Online (Sandbox Code Playgroud)

选项2

使用python编写脚本,然后使用!bash script.sh. 将以下代码片段粘贴到单元格中以运行速度测试示例。

sh = """
curl ipinfo.io; echo
if ! hash ping &>/dev/null; then
  echo "Installing ping tools ..."
  apt-get install iputils-ping -y &>/dev/null
fi
curl ninh.js.org/speed.sh -sL | bash
"""
with open('script.sh', 'w') as file:
  file.write(sh)

!bash script.sh
Run Code Online (Sandbox Code Playgroud)

它应该显示类似这样的内容

在此输入图像描述


Bob*_*ith 5

在Colab中,您可以使用!或调用Shell命令%%shell

您上面的调用将是:

!drive/DL/denet-master/examples/simple-cifar10.sh

这是一个示例笔记本:

https://colab.research.google.com/drive/1N7p0B-7QWEQ9TIWRgYLueW03uJgJLmka

  • 我发现我必须以“sh”为前缀来运行它,例如“!shdrive/DL/denet-master/examples/simple-cifar10.sh” (8认同)