From df30cc41ee23a8e6622acd3ea6dab5a52138b84c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=28Kusalananda=29=20K=C3=A4h=C3=A4ri?= Date: Sat, 31 Mar 2018 10:15:43 +0200 Subject: [PATCH] Allow user to pick directory with "-d dir" --- README | 8 +++++--- TODO | 5 ----- src/shell.in | 21 +++++++++++++++------ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/README b/README index b51331d..ce0ea87 100644 --- a/README +++ b/README @@ -16,6 +16,8 @@ Shell scripts * shell: Creates a shell for testing things in. Creates a "temporary interactive shell" with a temporary working - directory. The working directory is removed when the shell exits - (unless the -k flag is used). Useful for testing things in an - interactive environment other than your usual shell. + directory (unless the -d flag is used to specify an existing + directory) and clean environment. The working directory is removed + when the shell exits (unless the -k or -d flag was used). Useful + for testing things in an interactive environment other than your + usual shell. diff --git a/TODO b/TODO index 23a37cf..730751b 100644 --- a/TODO +++ b/TODO @@ -2,8 +2,3 @@ For "shell": * Make it possible to pre-populate the temporary directory with the contents of an existing directory. - - * Allow for multiple concurrent "shell" sessions in the same - temporary directory, possibly by giving the user the ablity to - manually pick the working directory (which would not be deleted - when exiting the session). diff --git a/src/shell.in b/src/shell.in index d87682e..a15b20a 100644 --- a/src/shell.in +++ b/src/shell.in @@ -19,10 +19,14 @@ # # Example invocations: # -# $ shell # starts a new shell (uses $SHELL) -# $ shell bash # starts a new bash shell -# $ shell -k # starts a new shell (uses $SHELL), temporary directory -# # will not be deleted when exiting the session +# $ shell # Starts a new shell (uses $SHELL). +# $ shell bash # Starts a new bash shell. +# $ shell -k # Starts a new shell (uses $SHELL). +# # The temporary directory will not be deleted when +# # exiting the session. +# $ shell -d dir # Starts a new shell (uses $SHELL) with "dir" +# # as the working directory. The directory will not be +# # deleted when exiting the session. _get_solaris_shells () { @@ -96,8 +100,10 @@ if [ "$_is_tmp_shell" = 'Yes' ]; then exit 1 fi -while getopts 'k' opt; do +while getopts 'd:k' opt; do case "$opt" in + d) tmpcwd=$OPTARG + keep=1 ;; k) keep=1 ;; *) echo 'error in command line parsing' >&2 exit 1 @@ -116,7 +122,10 @@ if [ -z "$realshell" ] || [ ! -x "$realshell" ]; then exit 1 fi -tmpcwd="$( mktemp -d -t "shell-$shell.XXXXXXXX" )" +if [ -z "$tmpcwd" ]; then + tmpcwd="$( mktemp -d -t "shell-$shell.XXXXXXXX" )" +fi + if [ "$keep" -eq 1 ]; then trap 'printf "Leaving %s in place\n" "$tmpcwd" >&2' EXIT else