Unix Shell Script question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Shell Script question
# 1  
Old 02-15-2012
Unix Shell Script question

I have the following script
=========
Code:
#!/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?

Last edited by GosarJunk; 02-15-2012 at 07:26 PM.. Reason: please use code tags
# 2  
Old 02-15-2012
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?

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

# 3  
Old 02-15-2012
Quote:
Originally Posted by DGPickett
As I recall, not (!) and or (||) in [] does not work as expected
According to man test, ! does indeed work inside [ ]. You're correct about || && though.
# 4  
Old 02-15-2012
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.
# 5  
Old 02-15-2012
The script can be much simplified and many scripting errors removed.
One bug is on this comparison:
Quote:
"$MUTEXPREFIX.${m}"
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.

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


Last edited by methyl; 02-15-2012 at 04:32 PM.. Reason: correct echo. There's nothing useful in $0.
# 6  
Old 02-16-2012
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 ...

======
Code:
#!/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.
=======
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? Smilie

Last edited by Franklin52; 02-16-2012 at 03:35 AM.. Reason: Please use code tags for code and data samples, thank you
# 7  
Old 02-16-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX shell script question.

I need to check whether the directory is exist or not. only three letter will be passed as argument. from that it should pick the entire directory. Instead of banking and manfucuture the input will be passed as man or ban. $1 -> ban $2-> monday #!/bin/sh DIR='/sales/$1*/monday' if ;... (3 Replies)
Discussion started by: arun888
3 Replies

2. Shell Programming and Scripting

Shell script question

Hi all, can you plz check whether the below code is correct & some inputs. I need to read the below file and process it. input : /home/ibm/var.txt urgent not urgent not needed. #!/usr/bin/ksh VAR=/home/ibm/var.txt if ] then (7 Replies)
Discussion started by: ramkumar15
7 Replies

3. UNIX for Dummies Questions & Answers

Question about shell scripting in UNIX

Unix script coding help? i am trying to write a code that will display following menu to user: (A) Add (B) Subtract (C) Multiply (D) Divide (E) Modulus (F) Exponentiation (G) Exit Then ask user for choice (A-F). After taking users choice ask user for two numbers and perform... (0 Replies)
Discussion started by: renegade755
0 Replies

4. Shell Programming and Scripting

New Unix user with shell script question using grep

Hello, I am a new Unix user and new to shell programming. I am working on a script to go through a log file and find the text error: grep -i 'error' monplus.mplog if I find the text error in the log file I would like to echo a message to the operator staing there is an error I am currently... (2 Replies)
Discussion started by: dtracy01
2 Replies

5. Homework & Coursework Questions

question on shell script

hiiiiiiiiiiiii,,I found an error on my following script but couldnt find it!!! Can you please help me as soon as possible?! echo "enter a number " read n i=0 first=0 second=1 result=0 prime="true" echo –n " $first $second " while do result=`expr $first + $second` first=$second... (10 Replies)
Discussion started by: moonlips
10 Replies

6. Homework & Coursework Questions

Question on shell script

Hiiiiiiiiiiiii all, Please i want your help fast, the teacher gave us this assignment can u help me to write it? this is the question: Write a shell script to point all prime numbers from the fibonacci series of integer N? using Red hat Os Thanks all and waiting for ur answers... (1 Reply)
Discussion started by: moonlips
1 Replies

7. Programming

question on shell script

when i run a shell script i have to type ./my_prog and the first line of my_prog has to have #!/usr/bin/env bash how do i change it to i only have to type my_prog to run it? (4 Replies)
Discussion started by: omega666
4 Replies

8. UNIX for Dummies Questions & Answers

Linux Shell Question: how to print the shell script name ?

Suppose I have a script named "sc.sh" in the script how to print out its name "sc.sh"? (3 Replies)
Discussion started by: meili100
3 Replies

9. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies

10. Shell Programming and Scripting

A shell script question

Hi, I have a file say xmldir.conf. This is a flat file which contains the data in specific format not other then this. The format is /backup/surjya/mvfile,noeof /backup/surjya/mdbase,eof /backup/surjya/mdbaseso /backup/surjya/trial,hoeof /backup/surjya/test,eof The field before "," is... (2 Replies)
Discussion started by: surjyap
2 Replies
Login or Register to Ask a Question