简体   繁体   English

如何从Jupyter Notebook中的Python字符串执行bash脚本?

[英]How to execute a bash script from a Python string in Jupyter Notebook?

I am using Jupyter Notebook and would like to execute a bash script from a python string. 我正在使用Jupyter Notebook,并希望从python字符串执行bash脚本。 I have a python cell creating the bash script which then I need to print, copy to another cell, and then run it. 我有一个python单元格创建bash脚本,然后我需要打印,复制到另一个单元格,然后运行它。 Is it possible to use something like exec('print('hello world!')') ? 是否可以使用像exec('print('hello world!')')这样的东西exec('print('hello world!')')

Here is an example of my bash script: 这是我的bash脚本的一个例子:

%%bash -s "$folder_dir" "$name_0" "$name_1" "$name_2" "$name_3" "$name_4" "$name_5" "$name_6" "$name_7" "$name_8" "$name_9" "$name_10" "$name_11"

cd $1

ds9 ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10} ${11} ${12} ${13}

If not possible, then how can I go to a different directory, and run 如果不可能,那么我该如何进入不同的目录,然后运行

ds9 dir1 dir2 dir3 ...

within my Jupyter Notebook since I only can initialize dir's using python. 在我的Jupyter笔记本中,因为我只能使用python初始化dir。 Note that the number of dir's is not fixed every time I run my code. 请注意,每次运行代码时,目录的数量都不固定。 ds9 is just a command to open multiple astronomical images at the same time. ds9只是一个同时打开多个天文图像的命令。

I know I can save my bash script to a .sh file and execute that, but I am looking for a classier solution. 我知道我可以将我的bash脚本保存到.sh文件并执行它,但我正在寻找一个更优雅的解决方案。

The subprocess module is the Right Thing for invoking external software -- in a shell or otherwise -- from Python. subprocess模块是从Python调用外部软件(在shell或其他方面)的正确方法。

import subprocess

folder_dir="/" # your directory
names=["name_one", "name_two"]   # this is your list of names you want to open

subprocess.check_call(
    ['cd "$1" || exit; shift; exec ds9 "$@"', "_", folder_dir] + names,
    shell=True)

How it works (Python) 它是如何工作的(Python)

When passing a Python list with shell=True , the first item in that list is the script to run; 传递带有shell=True的Python列表时,该列表中的第一项是要运行的脚本; the second is the value of $0 when that script is running, and subsequent items are values for $1 and onward. 第二个是该脚本运行时$0的值,后续项是$1及以后的值。

Note that this runs sh -c '...' with your command. 请注意,这sh -c '...'使用您的命令运行sh -c '...' sh is not bash, but (in modern systems) a POSIX sh interpreter. sh不是bash,而是(在现代系统中)一个POSIX sh解释器。 It's thus important not to use bash-only syntax in this context. 因此,重要的是不要在此上下文中使用仅使用bash的语法。

How it works (Shell) 它是如何工作的(壳牌)

Let's go over this line-by-line: 让我们逐行了解一下:

cd "$1" || exit # try to cd to the directory passed as $1; abort if that fails
shift           # remove $1 from our argument list; the old $2 is now $1, &c.
exec ds9 "$@"   # replace the shell in memory with the "ds9" program (as an efficiency
                # ...measure), with our argument list appended to it.

Note that "$1" and "$@" are quoted. 请注意,引用"$1""$@" Any unquoted expansion will be string-split and glob-expanded; 任何不带引号的扩展都将进行字符串拆分和全局扩展; without this change, you won't be able to open files with spaces in their names. 如果没有此更改,您将无法打开名称中包含空格的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM