Sponsored Content
Full Discussion: Unix Shell Script question
Top Forums Shell Programming and Scripting Unix Shell Script question Post 302598926 by GosarJunk on Thursday 16th of February 2012 01:30:22 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
FLOCK(1)							   User Commands							  FLOCK(1)

NAME
flock - manage locks from shell scripts SYNOPSIS
flock [options] file|directory command [arguments] flock [options] file|directory -c command flock [options] number DESCRIPTION
This utility manages flock(2) locks from within shell scripts or from the command line. The first and second of the above forms wrap the lock around the execution of a command, in a manner similar to su(1) or newgrp(1). They lock a specified file or directory, which is created (assuming appropriate permissions) if it does not already exist. By default, if the lock cannot be immediately acquired, flock waits until the lock is available. The third form uses an open file by its file descriptor number. See the examples below for how that can be used. OPTIONS
-c, --command command Pass a single command, without arguments, to the shell with -c. -E, --conflict-exit-code number The exit code used when the -n option is in use, and the conflicting lock exists, or the -w option is in use, and the timeout is reached. The default value is 1. -F, --no-fork Do not fork before executing command. Upon execution the flock process is replaced by command which continues to hold the lock. This option is incompatible with --close as there would otherwise be nothing left to hold the lock. -e, -x, --exclusive Obtain an exclusive lock, sometimes called a write lock. This is the default. -n, --nb, --nonblock Fail rather than wait if the lock cannot be immediately acquired. See the -E option for the exit code used. -o, --close Close the file descriptor on which the lock is held before executing command. This is useful if command spawns a child process which should not be holding the lock. -s, --shared Obtain a shared lock, sometimes called a read lock. -u, --unlock Drop a lock. This is usually not required, since a lock is automatically dropped when the file is closed. However, it may be required in special cases, for example if the enclosed command group may have forked a background process which should not be hold- ing the lock. -w, --wait, --timeout seconds Fail if the lock cannot be acquired within seconds. Decimal fractional values are allowed. See the -E option for the exit code used. The zero number of seconds is interpreted as --nonblock. --verbose Report how long it took to acquire the lock, or why the lock could not be obtained. -V, --version Display version information and exit. -h, --help Display help text and exit. EXAMPLES
shell1> flock /tmp -c cat shell2> flock -w .007 /tmp -c echo; /bin/echo $? Set exclusive lock to directory /tmp and the second command will fail. shell1> flock -s /tmp -c cat shell2> flock -s -w .007 /tmp -c echo; /bin/echo $? Set shared lock to directory /tmp and the second command will not fail. Notice that attempting to get exclusive lock with second command would fail. shell> flock -x local-lock-file echo 'a b c' Grab the exclusive lock "local-lock-file" before running echo with 'a b c'. ( flock -n 9 || exit 1 # ... commands executed under lock ... ) 9>/var/lock/mylockfile The form is convenient inside shell scripts. The mode used to open the file doesn't matter to flock; using > or >> allows the lock- file to be created if it does not already exist, however, write permission is required. Using < requires that the file already exists but only read permission is required. [ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || : This is useful boilerplate code for shell scripts. Put it at the top of the shell script you want to lock and it'll automatically lock itself on the first run. If the env var $FLOCKER is not set to the shell script that is being run, then execute flock and grab an exclusive non-blocking lock (using the script itself as the lock file) before re-execing itself with the right arguments. It also sets the FLOCKER env var to the right value so it doesn't run again. EXIT STATUS
The command uses sysexits.h return values for everything, except when using either of the options -n or -w which report a failure to acquire the lock with a return value given by the -E option, or 1 by default. When using the command variant, and executing the child worked, then the exit status is that of the child command. AUTHOR
H. Peter Anvin <hpa@zytor.com> COPYRIGHT
Copyright (C) 2003-2006 H. Peter Anvin. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICU- LAR PURPOSE. SEE ALSO
flock(2) AVAILABILITY
The flock command is part of the util-linux package and is available from Linux Kernel Archive <https://www.kernel.org/pub/linux/utils /util-linux/>. util-linux July 2014 FLOCK(1)
All times are GMT -4. The time now is 12:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy