Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using CentOS 5.4. I created a bash script that does some checking before running any commands. If the check fails, it will simply exit 0. The problem I am having is that on our server, the script will exit the su or ssh session when the exit 0 is called.

#!/bin/bash
# check if directory has contents and exit if none
if [ -z "`ls /ebs_raid/import/*.txt 2>/dev/null`" ]; then
  echo "ok"
  exit 0
fi

here is the output:

[root@ip-10-251-86-31 ebs_raid]# . test.sh 
ok
[russ@ip-10-251-86-31 ebs_raid]$

as you can see, I was removed from my sudo session, if I wasn't in the sudo session, it would have logged me out of my ssh session. I am not sure what I am doing wrong here or where to start.

share|improve this question
2  
You can do this, by the way: if ! ls /ebs_raid/import/*.txt 2>/dev/null; then –  Dennis Williamson Apr 23 '10 at 23:14
add comment

1 Answer

up vote 3 down vote accepted

When you use . to run a shell script it executes in the current shell rather than starting a new shell. Type bash test.sh to get the behaviour you want.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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