Skip to content

Commit d222c54

Browse files
committed
Revise the runscript example to allow passing through options to cowsay
1 parent 82a766b commit d222c54

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

README.md

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,38 @@ MirrorURL: http://us.archive.ubuntu.com/ubuntu/
540540
541541
542542
%runscript
543-
#!/bin/bash
544-
if [ $# -ne 2 ]; then
545-
echo "Please provide an input and an output file."
543+
infile=
544+
outfile=
545+
546+
usage() {
547+
>&2 echo "Usage:"
548+
>&2 echo "$SINGULARITY_NAME -i <infile> -o <outfile> [ -- <cowsay options> ]"
546549
exit 1
550+
}
551+
552+
while getopts i:o: argument
553+
do
554+
case $argument in
555+
i)
556+
infile="$OPTARG"
557+
;;
558+
o)
559+
outfile="$OPTARG"
560+
;;
561+
?)
562+
usage
563+
;;
564+
esac
565+
done
566+
567+
shift "$((OPTIND - 1))"
568+
569+
if [ -z "$infile" ] || [ -z "$outfile" ]
570+
then
571+
usage
547572
fi
548-
cat $1 | cowsay > $2
573+
574+
cat "$infile" | cowsay "$@" > "$outfile"
549575
550576
%post
551577
echo "Hello from inside the container"
@@ -567,13 +593,14 @@ $ sudo singularity build --force lolcow.simg Singularity
567593

568594
Note the `--force` option which ensures our previous container is completely overwritten.
569595

570-
After rebuilding our container, we can call the lolcow.simg as though it were an executable, and simply give it two arguments. One for input and one for output.
596+
After rebuilding our container, we can call the lolcow.simg as though it were an executable, give it input and output file names, and optionally give additional arguments to go directly to the `cowsay` program.
571597

572598
```
573599
$ ./lolcow.simg
574-
Please provide an input and an output file.
600+
Usage:
601+
lolcow.simg -i <infile> -o <outfile> [ -- <cowsay options> ]
575602
576-
$ ./lolcow.simg input output2
603+
$ ./lolcow.simg -i input -o output2
577604
578605
$ cat output2
579606
______________________________________
@@ -652,7 +679,7 @@ Now the `/mnt` directory in the container is bind mounted to the `/data` directo
652679
Now what about our earlier example in which we used a runscript to run a our container as though it were an executable? The `singularity run` command accepts the `--bind` option and can execute our runscript like so.
653680

654681
```
655-
$ singularity run --bind /data:/mnt lolcow.simg /mnt/vader.txt /mnt/output3
682+
$ singularity run --bind /data:/mnt lolcow.simg -i /mnt/vader.txt -o /mnt/output3
656683
657684
$ cat /data/output3
658685
__________________
@@ -667,10 +694,11 @@ $ cat /data/output3
667694

668695
But that's a cumbersome command. Instead, we could set the variable `$SINGULARITY_BINDPATH` and then use our container as before.
669696

697+
670698
```
671699
$ export SINGULARITY_BINDPATH=/data:/mnt
672700
673-
$ ./lolcow.simg /mnt/output3 /mnt/metacow2
701+
$ ./lolcow.simg -i /mnt/output3 -o /mnt/metacow2 -- -n
674702
675703
$ ls -l /data/
676704
total 12
@@ -679,22 +707,16 @@ total 12
679707
-rw-rw-r-- 1 ubuntu ubuntu 17 Jun 7 20:57 vader.txt
680708
681709
$ cat /data/metacow2
682-
________________________________________
683-
/ __________________ < I am your father \
684-
| > |
685-
| |
686-
| ------------------ |
687-
| |
688-
| \ ^__^ |
689-
| |
690-
| \ (oo)\_______ |
691-
| |
692-
| (__)\ )\/\ |
693-
| |
694-
| ||----w | |
695-
| |
696-
\ || || /
697-
----------------------------------------
710+
______________________________
711+
/ __________________ \
712+
| < I am your father > |
713+
| ------------------ |
714+
| \ ^__^ |
715+
| \ (oo)\_______ |
716+
| (__)\ )\/\ |
717+
| ||----w | |
718+
\ || || /
719+
------------------------------
698720
\ ^__^
699721
\ (oo)\_______
700722
(__)\ )\/\

0 commit comments

Comments
 (0)