Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved script argument parsing (any order), plus couple tiny tweaks #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ scp -o sftp_server="singularity -s exec --overlay=/data/HCPsquash/hcp1200-00-100
or more simply using the same type of wrapper described for sshfs:

```bash
sshfs -o sftp_server="/data/HCPsquash/sing_sftpd_here" user@computer2:/HCP_1200_data/remote_file.txt localfile.txt
scp -o sftp_server="/data/HCPsquash/sing_sftpd_here" user@computer2:/HCP_1200_data/remote_file.txt localfile.txt
```

### f) Extracting data using rsync
Expand Down
72 changes: 36 additions & 36 deletions bin/sing_command
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# sing_sftpd_here # default sqfs and image: in install dir
# sing_rsync_here # default sqfs and image: in install dir

VERSION="3.0"
VERSION="3.1"
BASENAME=$(basename $0)
PREFIX="" # -D option

Expand Down Expand Up @@ -51,6 +51,7 @@ RSYNC_COMMAND="rsync" # hopefully it's in the search path in the container

# Documentation and usage statement
function usage() {
local rc=${1:-2} # ability to exit with a different code if provided
cat <<USAGE 1>&2
This is $BASENAME $VERSION by Pierre Rioux

Expand All @@ -60,8 +61,6 @@ program, it will run the OpenSSH 'sftp-server', or the 'rsync' command.

Usage: $BASENAME [-D directory] [-I singularity_image_file] [-O overlay_file_pattern] [command...]

Note: options must be provided in the order shown!

where:

-D directory
Expand Down Expand Up @@ -130,7 +129,7 @@ Examples:
sing_sftpd -D /my/path/for/everything

USAGE
exit 2
exit $rc
}

##########################################
Expand All @@ -143,36 +142,42 @@ if test "X${BASENAME/*_here/Y}" = "XY" ; then
PREFIX=$(dirname $(readlink -e "$0")) || exit
fi

####################################
# Validation of arguments
####################################
#####################################
# Parsing and validation of arguments
#####################################

if test -z "$PREFIX" -a $# -lt 2 ; then
usage
fi

# Optional PREFIX in -D
if test "X$1" = "X-D" ; then
PREFIX="$2"
shift ; shift
if ! test -d "$PREFIX" ; then
echo "Error: '$PREFIX' for option -D is not a directory." 1>&2
exit 2
fi
fi

# Option -I: the path of the singularity image
if test "X$1" = "X-I" ; then
test $# -lt 2 && usage
SING_IMAGE="$2" # can be empty string if -D provided
shift ; shift
fi

# Option -O: the path (or pattern) for one or several overlay images
if test "X$1" = "X-O" ; then
test $# -lt 2 && usage
OVERLAYS="$2"; # can also be empty if -D provided
shift ; shift
SING_IMAGE="" # can be empty string if -D provided
OVERLAYS="" # can also be empty if -D provided
while test -n "$1" ; do
case "$1" in
-D)
test $# -lt 2 && usage
PREFIX="$2"
shift ; shift ;;
-I)
test $# -lt 2 && usage
SING_IMAGE="$2"
shift ; shift ;;
-O)
test $# -lt 2 && usage
OVERLAYS="$2"
shift ; shift ;;
-h | -help | --help)
usage 0
*)
# Must be command and/or arguments, leave as is
break ;;
esac
done

# Sanity check for optional PREFIX in -D
if ! test -d "$PREFIX" ; then
echo "Error: '$PREFIX' for option -D is not a directory." 1>&2
exit 2
fi

# Find image and overlays if necessary
Expand Down Expand Up @@ -209,7 +214,7 @@ fi
# Validate overlays patterns
if test -z "$OVERLAYS" ; then
echo "Error: no patterns for the overlays files provided." 1>&2
echo "Maybe you need to provide the path with -O ?" 1>&2
echo "Maybe you need to provide the pattern with -O ?" 1>&2
exit 2
fi

Expand Down Expand Up @@ -250,11 +255,6 @@ if test "$MODE" = "shell" ; then
fi
fi

if test "X$*" = "X-h" -o "X$*" = "X-help" -o "X$*" = "X--help" ; then
usage
exit 2
fi

if test $MODE = "exec" ; then
# OK do the main work now.
#
Expand All @@ -264,7 +264,7 @@ if test $MODE = "exec" ; then
exec singularity -s exec \
$SING_OVERLAYS \
$SING_IMAGE \
bash -c "exec $COMMAND $*"
bash -c "exec $COMMAND $@"
else # interactive shell
exec singularity -s shell \
$SING_OVERLAYS \
Expand Down