Reputation: 8145
I use SSH Secure Shell client to connect to a server and run my scripts.
I want to stop a script on some conditions, so when I use exit, not only the script stops, but all the client disconnects from the server!, Here is the code:
if [[ `echo $#` -eq 0 ]]; then
echo "Missing argument- must to get a friend list";
exit
fi
for user in $*; do
if [[ !(-f `echo ${user}.user`) ]]; then
echo "The user name ${user} doesn't exist.";
exit
fi
done
A picture of the client:
Why is this happening?
Upvotes: 0
Views: 1258
Reputation: 58768
exit
returns from the current shell - If you've started a script by running it directly, this will exit the shell that the script is running in.
return
returns from a function or sourced file (TY Dennis Williamson) - Same thing, but it doesn't terminate your current shell.
break
returns from a loop - Similar to return
, but can be used anywhere within a loop to stop processing more items. This is probably what you want.
Upvotes: 2
Reputation: 75
if you are running from the current shell, exit will obviously exit from the shell and disconnect you. try running it in a new shell ( use a . before the script) or else use 'return' instead of exit
Upvotes: 0
Reputation: 69012
You use source
to run the script, this runs it in the current shell. That means that exit
terminates the current shell and with that the ssh session.
replace source
with bash
and it should work, or better put
#!/bin/bash
on to of the file and make it executable.
Upvotes: 2