Unix Shell Script question

I have the following script

#!/bin/sh

MUTEXPREFIX="/tmp/"
READMUTEX=(test globallock)

# If mutexes found - exit out
for m in "${READMUTEX[@]}"; do
        [ ! \( -e "$MUTEXPREFIX.${m}" \) ] || (echo "$0 Mutex file found - Exiting\n" ; exit 1)
done;

echo "After for loop\n";
exit;

=============

What i want is - if there is a mutex file like /tmp/.test or /tmp/.globallock, the script should exit.

However when i run without the mutex file - it runs fine. But when i create a mutex file - like /tmp/.test - it does not exit out and executes the "after for loop" echo statement before exiting.

What am i doing wrong?

As I recall, not (!) and or (||) in [] does not work as expected, so try an if nested in an if, and echo when each if's then or else fires.

Does exit in a sub-shell in a test do more than register true/false to any inquiring parent?

$ sh -c 'if ( sleep 1;exit 1 )
> then
> echo true
> else 
> echo false
> fi;echo exit'
false
exit
$

According to man test, ! does indeed work inside . You're correct about || && though.

His "[...] || (...)" is bareback in the script.

The operators ! and -o work in [], but as I recall the ! is not evaluated to just the following, first predicate, but to all unless in \(\), which is getting busy. I just avoid getting too fancy in [].

I like 'if' or 'case' when a condition is being tested, not "logic as procedure". You can, but should you? For one thing, if/case allows you to drop in debug statements easily, if you get lost.

The script can be much simplified and many scripting errors removed.
One bug is on this comparison:

Assuming that the array assigment works (it actually gives a syntax error in my Posix Shell) you would be generating filenames /tmp/.test and /tmp/.globallock (note the embedded full stop).
There is never a reason to end a unix Shell command line with a semi-colon. This is unix not Oracle.
You "if" test is overcomplicated but it might work. Better to keep it simple so everybody can follow your code.

#!/bin/sh

MUTEXPREFIX="/tmp/"

# If mutexes found - exit out
for m in "test" "globallock"
do
      if [ -e "${MUTEXPREFIX}/${m}" ]
      then
            echo "${m} Mutex file found - Exiting"
            exit 1
      fi
done

echo "After for loop"
exit

Hi Methyl,

Sorry my bad - i updated the original post where i left the "." in the filename for Mutex.
The "." is correct - since we will be having /tmp/.test or /tmp/.globallock file - hidden files.

The code works fine until i put in a "echo "$0 Mutex file found - exiting" to get into an error log.
This below code works fine ...

======

#!/bin/sh

MUTEXPREFIX="/tmp/.topsight"
READMUTEX=(test globallock)

# If mutexes found - exit out
for m in "${READMUTEX[@]}"
do
   [ ! \( -e "$MUTEXPREFIX.${m}" \) ] || exit 1
done
echo "After for loop";
exit;

======
for simplicity sake - in our example here - i m doing an "echo" instead of writing to a file.

---------- Post updated at 03:31 PM ---------- Previous update was at 02:55 PM ----------

I did a "sh -vx" to deebug this script. For some reason it does not execute the exit 1 - which is in the code.

[scripts]$ touch /tmp/.test
[scripts]$ sh -vx ./x.sh
#!/bin/sh

MUTEXPREFIX="/tmp/"
+ MUTEXPREFIX=/tmp/
READMUTEX=(test globallock)
+ READMUTEX=(test globallock)

# If mutexes found - exit out
for m in "${READMUTEX[@]}"
do
   [ ! \( -e "$MUTEXPREFIX.${m}" \) ] || (echo "$0 Mutex file found - Exiting" ; exit 1)
done
+ for m in '"${READMUTEX[@]}"'
+ '[' '!' '(' -e /tmp/.test ')' ']'
+ echo './x.sh Mutex file found - Exiting'
./x.sh Mutex file found - Exiting
+ exit 1
+ for m in '"${READMUTEX[@]}"'
+ '[' '!' '(' -e /tmp/.globallock ')' ']'

echo "After for loop";
+ echo 'After for loop'
After for loop

exit;
+ exit

=======

---------- Post updated 02-16-12 at 10:30 AM ---------- Previous update was 02-15-12 at 03:31 PM ----------

Any idea why it would not execute the "exit 1" statement ?

Any Unix shell scripting gurus? :confused:

It is because the "exit 1" is in a child script (the piece of code surrounded by brackets) which means the the exit just went up one level.

The rest of the script is a mystery to me because it is overcomplicated.

I searched thru the web but did not get any examples.

We have the following documentation for using && and || for if-then-else condition

# cmd1 && cmd2   Run cmd1, then if cmd1 successful run cmd2, otherwise skip.
# cmd1 || cmd2   Run cmd1, then if cmd1 not successful run cmd2, otherwise skip.

However what do you do if you want to have multiple commands for "cmd2" to be executed with one of them being "exit" command.

Can't see the point of the array. My interpretation of what you mean is:

#!/bin/sh
for filename in "/tmp/.test" "/tmp/.globallock"
do
if [ -e "${filename}" ]
then
          echo "Mutex file found: ${filename}"
          exit 1
fi
echo "After for loop"

Hmm. This is only subtly different from my post a fortnight ago ... but simplified a bit more.