27

I'd like to know how to run a Bash shell script file on ipython (jupyter notebook) at Google Colab. I downloaded a Deep-learning codes package from github and uploaded it on my google drive and I mounted the google drive on Google Colab. The code package includes '*.py' python codes and 'fn.sh' script file. By executing the script file the python codes can be executed.

I tried os.system('fn.sh') and subprocess.call('fn.sh') on the ipython prompt at Google Colab but they doesn't work like below.

1)

import os
os.system('drive/DL/denet-master/examples/simple-cifar10.sh')
32256
import subprocess
subprocess.call('drive/DL/denet-master/examples/simple-cifar10.sh')
OSError: [Errno 8] Exec format error: 'drive/DL/denet-master/examples/simple-cifar10.sh'

5 Answers 5

40

In Colab, you can invoke shell commands using either ! or %%shell.

Your invocation above would be:

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

Here's an example notebook:

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

Sign up to request clarification or add additional context in comments.

4 Comments

From your comments I found out that the absolute file path was wrong. By fixing it, your answer works well on my problem. Thanks.
I found that I had to run it prefixed with sh, e.g. !sh drive/DL/denet-master/examples/simple-cifar10.sh
Note that %%shell is a cell-specific magic word, so it applies to the whole cell but only the current cell. More info can be found by running the %magic command.
Note that a magic command, %%sh, is also available to provide an alternative to the comment above from @mheavers
12

Option 1

Use ! as other answers mentioned.

!ls -la
!echo "Hello"
!bash path/to/script.sh

Options 2

Use python to write a script, then execute it with !bash script.sh. Paste the following snippet to a cell to run a speed test example.

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

It should show something like this

enter image description here

Comments

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

Comments

1

For google colab use

!/content/folder/file.sh

you can copy path of the .sh file. Make sure you use a '/' before the content. This worked for me. I didn't use !bash or !sh

Comments

-1

here is way

first run and save the output to a text file like this

import os
os.system("pip list > file.txt")

then read the output from the file

import os
with open("file.txt","r") as file:
    print(file.read())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.