bash脚本在打开的奇异容器中执行操作

编程入门 行业动态 更新时间:2024-10-28 02:34:35
本文介绍了bash脚本在打开的奇异容器中执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在终端的RedHat上使用以下工作流程:

I am using the following workflow on RedHat in terminal:

  • 打开一个奇异图片:

singularity run /mn/sarpanitu/singularity/test/fenics-and-more.img

singularity run /mn/sarpanitu/singularity/test/fenics-and-more.img

  • 导出某些显示在奇点内:

export DISPLAY=:0.0

export DISPLAY=:0.0

  • 以单数形式导出到 gmsh 的路径:
  • export a path to gmsh inside the singularity:

export PATH="$HOME/Downloads/gmsh-git-Linux64/bin:$PATH"

export PATH="$HOME/Downloads/gmsh-git-Linux64/bin:$PATH"

我想把所有东西都当作bash脚本.我的第一种(无效的)方法是以下方法(全部在 singularity_script.sh 文件中):

I want to put everything as a bash script. My first (not working) approach is the following (all in a singularity_script.sh file):

#!/bin/bash function singularity_script(){ singularity run /mn/sarpanitu/singularity/test/fenics-and-more.img export DISPLAY=:0.0 export PATH="$HOME/Downloads/gmsh-git-Linux64/bin:$PATH" }

我通过采购然后调用函数来执行它:

I execute it by sourcing and then calling the function:

chmod +x singularity_script.sh . singularity_script.sh singularity_script

但是,这当然是行不通的,因为导出是在父终端中完成的(我认为吗?),而不是在子节点上的奇异之处.因此,我无法以奇异的方式正确导出显示和路径.

But of course, this does not work as the exports are done (I think?) in the parent terminal and not the son singularity. So I do not get the display and path exported correctly in singularity.

有什么办法解决这个问题?我猜解决方案是在容器启动时自动在容器内运行脚本,但是如何简单地做到这一点?

Any way to fix this? I guess the solution would be to automatically run the script inside the container at startup of the container, but how to do this simply?

推荐答案

知道了! @tormodlandet的解决方案存在一个问题,即,一旦使用-c选项调用的命令运行,奇异容器就会死亡.

Got it! The solution by @tormodlandet had one problem, i.e. the singularity container died once the command called with the -c option were run.

我可以通过执行以下命令使其工作:

I can get it to work by executing the following instead:

singularity shell /mn/sarpanitu/singularity/test/fenics-and-more.img -c "export DISPLAY=:0.0 && export PATH="$HOME/Downloads/gmsh-git-Linux64/bin:$PATH" && /bin/bash -norc"

这会执行我想要的奇异命令,然后从奇异容器中生成一个gnome-terminal,直到退出后它才会死亡.

This does the command I want in the singularity, and then spawns a gnome-terminal from within the singularity container that does not die until it is exited.

在末尾缺少/bin/bash -norc表示奇异容器在最后一个命令后死亡.

Missing the /bin/bash -norc at the end means that the singularity container dies after the last command.

因此,要从脚本而不是普通命令中调用有用的命令,只需使用:

So in order to call the useful commands from a script rather than the plain command, simply use:

singularity shell /mn/sarpanitu/singularity/test/fenics-and-more.img -c "bash script_singularity.sh && /bin/bash -norc"

当前工作目录中的一个script_singularity.sh文件,其中包含要运行的命令:

where there is in the current working directory a script_singularity.sh file containing the commands to run:

echo "Hi there..." export DISPLAY=:0.0 export PATH="$HOME/Download/gmsh-git-Linux64/bin:$PATH" echo "...done exports!"

修改

如果您仍然想要一个美观的终端,可以提供一个bashrc配置文件.例如:

If you want to still have a good looking terminal, you can provide a bashrc config file. For example:

singularity shell /mn/sarpanitu/singularity/test/fenics-and-more.img -c "bash script_singularity.sh && /bin/bash -rcfile singularity_bashrc"

其中singularity_bashrc是您要使用的bashrc.例如,它非常适合(在此处添加冗长,并且在很多地方都有更详细的bashrc解释,但这在某些注释中是必需的):

where singularity_bashrcis your bashrc to use. For example, it works great with (this is verbose to add here and there are many places with much more detailed bashrc explanations, but this was requested in some comments):

# check the window size after each command and, if necessary, # update the values of LINES and COLUMNS. shopt -s checkwinsize # set variable identifying the chroot you work in (used in the prompt below) if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then debian_chroot=$(cat /etc/debian_chroot) fi # set a fancy prompt (non-color, unless we know we "want" color) case "$TERM" in xterm-color) color_prompt=yes;; esac # uncomment for a colored prompt, if the terminal has the capability; turned # off by default to not distract the user: the focus in a terminal window # should be on the output of commands, not on the prompt force_color_prompt=yes if [ -n "$force_color_prompt" ]; then if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then # We have color support; assume it's compliant with Ecma-48 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such # a case would tend to support setf rather than setaf.) color_prompt=yes else color_prompt= fi fi if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi unset color_prompt force_color_prompt # If this is an xterm set the title to user@host:dir case "$TERM" in xterm*|rxvt*) PS1="sing-\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1" ;; *) ;; esac # enable color support of ls and also add handy aliases if [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" alias ls='ls --color=auto' #alias dir='dir --color=auto' #alias vdir='vdir --color=auto' alias grep='grep --color=auto' alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto' fi

更多推荐

bash脚本在打开的奇异容器中执行操作

本文发布于:2023-10-24 09:57:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523641.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇异   容器   脚本   操作   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!