OmG
OmG

Reputation: 18838

Read shell script result in Google Colab Research Notebook

I know how to run a script in the notebook (for example this post). Now the question is how can I read the result of the shell script in python? For example, we have an echo "hi" command, or we call a shell script, and we want to read the printed message on the console in python.

Upvotes: 4

Views: 3235

Answers (1)

OmG
OmG

Reputation: 18838

It can be done easily! You can put it equal to the name of a variable in your code. For example:

result = !echo "hello"

result is a list of strings, that shows the lines of printed output of the command in the console as of its item.

Moreover, you can put it in a loop like all other python expressions:

result = []
for i in range(10):
    echo_result = !echo $i
    result.append(echo_result)

result
>> [['0'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]

Upvotes: 6

Related Questions