0

I have a script called myscript.sh with multiple commands within it

#!/bin/sh

singularity shell -B /home/user/Desktop/ /home/user/image/some_image.simg
/home/user/miniconda/activate my_env
cd /app/app_folder/scripts
ls -ash

The first command (line 3) invokes a shell and I want the subsequent commands to run within the invoked shell.

How do I get this to work properly.

se7en
  • 101
  • 4
  • I'm not familiar with `singularity` shell, but Instead of an `sh` script, I'd write a `singularity` script. I imagine you'd simply replace line 1 (shebang) with `#!/usr/bin/singularity shell -B ...` and remove line 3. – Stewart Apr 27 '21 at 19:08
  • If `singularity` isn't a shell, and you actually trying to launch a few commands in an isolated shell, you can use a ["subshell"](https://unix.stackexchange.com/a/93836/272848) using `( ... )` notation. – Stewart Apr 27 '21 at 19:12
  • You'll probably get an error with that shebang line: remove the trailing slash -- `/bin/sh/` isn't a directory – glenn jackman Apr 27 '21 at 22:00
  • thanks @glennjackman I have edited the question now. the trailing slash wasn't supposed to be there. typo! – se7en Apr 29 '21 at 18:52

2 Answers2

0

With a glance at https://singularity.lbl.gov/archive/docs/v2-3/docs-shell looks like you want (untested):

singularity shell -B /home/user/Desktop/ /home/user/image/some_image.simg -c '
  /home/user/miniconda/activate my_env
  cd /app/app_folder/scripts
  ls -ash
'

You may not be able to use newlines in the -c command, in which case try semicolons instead.

glenn jackman
  • 84,176
  • 15
  • 116
  • 168
0

So basically I had to replace shell with exec and save the subsequent commands in a different executable bash script newscript.sh which contains

#!/bin/sh

/home/user/miniconda/activate my_env
cd /app/app_folder/scripts
ls -ash

and then run myscript.sh

#!/bin/sh

singularity exec -B /home/user/Desktop/ /home/user/image/some_image.simg bash newscript.sh

This method will run newscript.sh in the singularity shell after the singularity shell is invoked in myscript.sh

se7en
  • 101
  • 4