views:

82

answers:

1

I have a script with a number of options in it one of the option sets is supposed to change the directory and then exit the script however running over ssh with the source to get it to change in the parent it exits SSH is there another way to do this so that it does not exit? my script is in the /usr/sbin directory.

A: 

You might try having the script run a subshell instead of whatever method it is using to “change [the directory] in the parent” (presumably you have the child print out a cd command and have the parent do something like eval "$(script --print-cd)"). So instead of (e.g.) a --print-cd option, add a --subshell option that starts a new instance of $SHELL.

d=/path/to/some/dir
#...
cd "$d"
#...
if test -n "$opt_print_cd"; then
    sq_d="$(printf %s "$d" | sed -e "s/'/'\\\\''/g")"
    printf "cd '%s'\n" "$sq_d"
elif test -n "$opt_subshell"; then
    exec "$SHELL"
fi

If you can not edit the script itself, you can make a wrapper (assuming you have permission to create new, persistent files on the ‘server’):

#!/bin/sh
script='/path/to/script'
print_cd=
for a; do test "$a" = --print-cd && print_cd=yes && break; done
if test -n "$print_cd"; then 
    eval "$("$script" ${1+"$@"})" # use cd instead of eval if the script prints a bare dir path
    exec "$SHELL"
else
    exec $script" ${1+"$@"}
fi
Chris Johnsen