3

I have a bash script which moves the user calling the script to new working directory.

If I call the script with . ./script.sh cd works, and user gets moved to new directory, but the problem is that script is also using exit command if no argument is passed to it, which results in SSH session of the user getting closed.

How can I change the directory without calling the script with . ./script.sh, or how can I exit the script without killing user session when calling . ./script.sh.

The part of the script that I am having problem is this:

 #!/bin/bash

    VAR=$1

    if [[ "$VAR" == "" ]]; then printf "No argument provided, please provide what you want to check.\n"; exit 1; fi

    change_cwd () {
    cd $NEWDIR
    }

change_cwd

If I call ./script.sh exit works as intended, but directory is changed for script subshell not user, but if I use . ./script.sh then exit kills whole SSH session of the user calling the script.

What would be the ways to resolve this, for both exit and cd to work from bash script, either to change directory on some other way or exit script some other way?

The point is that I need to have the script changing the working directory of the user, and, in case of no argument, or wrong argument, as I intend to add some other checks for $VAR, stop executing the rest of the script and return to user shell, so that user runs the script again.

0

Test with $# the total number of arguments provided. If $# is 0 then don't issue the exit command.

I'm not sure what the change_cwd function looks like because $NEWDIR is nowhere defined, but here's and example of what could work:

    VAR=$1
    ARGC=$#

    if test $ARGC -eq 0 
    then 
      printf "No argument provided, please provide what you want to check.\n"
    else
      change_cwd ()
      {
        cd $NEWDIR
      }
    fi

(untested, but you get the gist..)

Also, it's probably neater to test with test -z to test for en empty string.

  • how will that exit the bash script and return to user shell. I need the user to rerun the script again with some argument, if no argument is provided I don't want to run the rest of the script, and I want user to run the script again. – rAlen Mar 8 '15 at 23:38
  • yeah i went with that didn't see your reply edit when i pasted my anwser – rAlen Mar 8 '15 at 23:53
  • As long as it works for you! :-) My code example is of course horribly broken because I don't use VAR or NEWDIR anywhere but feel free to come back for further help if required. – captcha Mar 8 '15 at 23:56
  • no its fine i didnt think of including it in if else, i just paste all other fucntion calls in else, as script is basically just calling various functions i defined. else;change_cwd;rest of functions;fi – rAlen Mar 8 '15 at 23:57
1

If the script is invoked as ./script, it cannot change the current directory of its parent process. So the script will have to be invoked as . ./script.

You can use the return builtin to exit the script and go back to its caller. You can only do that outside a function: in a function, return would return from the function, not from the script.

Keep in mind that if your script is sourced, it will be executed by the user's shell, not by bash, so users of other shells (zsh, fish, tcsh, …) won't be able to use your script. It's relatively easy to write scripts that work in bash and zsh (start your script with emulate -L ksh 2>/dev/null and most things will work), markedly less so to write scripts that also work in fish and tcsh.

An alternate approach to work around shell incompatibilities is to write the logic in the program of your choice to parse arguments, determine which directory to change into, etc. Then have your program print out shell code for the caller's shell, and have the caller evaluate the output of your script. Thus your script might output something like

cd /some/where

or

echo "Error: blah blah"; false

and it would be invoked with

eval "`./script`"

or a tcsh/fish/… analog.

0

the script is closing the ssh session because . ./script does not create a subshell, instead it runs the script in the current shell, so exit here is treated for the current interactive shell so is normal that this exits the session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.