String comparison problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String comparison problems
# 1  
Old 02-24-2012
String comparison problems

Req:
===========
1)I have one shell script a.sh
2)I opened Putty session , with user name = SIR100 , and i ran a.sh script in back ground and
it took 5 minutes to complete.Meanwhile,i ran same script second time i.e a.sh with same user it sholud not allow to run the script , but it's allowing using my below code.But, if i ran the same script , with different user , i.e SIR200 , it should allow to run the script.
3)Final requirement is , One user can't run the script , untill first one has completed.

4)According to below code , when user runs first time , cursor shoud goes to last else block , and script needs to be started.Where in , if same user run second time before completion of first scrit , it should goes to last if condition , and script should get exit.

5)Here , main problem is , how to compare 2nd time if the same user is running the script.

below is the code snippet:
==============================
Code:
#!/bin/sh

#Below command will print the login user name
oper=`whoami`



#Below command will print , on which user the script a.sh is running.
User=`ps -ef|grep a.sh|grep -v grep|awk '{print $1}'`

echo "Login User " $oper
echo "User Name  " $User

flag=0

listOfUsers=${echo $User | tr " " "\n"}
echo "List of Users " $listOfUsers

for userName in $listOfUsers
do
    echo $userName  
    if [ "${userName}" = "${oper}" ]
    then
    echo "Inside if aaaaaaa"
        flag=1
        exit 1
    fi
done


echo "flag value  " $flag 

if [ $flag -eq 1 ]
 then
 echo "Inside if bbbbbbbbb"
        echo "script is in progress..."
	
        exit 1
 else
        echo "Script is not running it initiated now."
	sleep 40
 fi


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 02-24-2012 at 10:31 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-24-2012
Try this:

Code:
listOfUsers=$(echo $User | tr " " "\n")

Guru.
# 3  
Old 02-24-2012
I tried the same, still im facing the same problem
# 4  
Old 02-25-2012
Then go with the lock file concept ..

Add the below lines as the first and last line of your a.sh script.
Code:
touch $(whoami).lockfile ## to create a lock file for that particular user
rm $(whoami).lockfile ## add this in last line of a.sh

Create a file b.sh then have the below lines in it.
Code:
if [ -f $(whoami).lockfile ]
then
   echo "Lock file exists for $(whoami). Script is running ..." ; exit
else
   echo "Lock file not exists for $(whoami). Initiating new script .."
   sh a.sh
fi

Hope this helps Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File comparison problems

We are running parallel reports with Legacy vs a new system and want to compare certain lines with the diff or diff -b command. When a six digit number is read at the beginning of the line (no decimal point) we read and ignore a header line (next), move any minus sign to the end of the number and,... (7 Replies)
Discussion started by: wbport
7 Replies

2. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

3. Shell Programming and Scripting

String comparison

hi team, i want to compare the below string from logs, but its is not working. if ]; then echo "restart some process" fi (4 Replies)
Discussion started by: mfaizan40
4 Replies

4. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

5. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Help with string comparison

#!/bin/sh PRINTF=/usr/bin/printf MACHINE_NAME=`uname -n` TIME=`date +"%H"` $PRINTF "Welcome to $MACHINE_NAME. What is your name?\n" read NAME if ; then $PRINTF "Good morning $NAME, how are you?\n" elif ; then $PRINTF "Good afternoon $NAME, how are you?\n" else $PRINTF "Good... (2 Replies)
Discussion started by: ikeQ
2 Replies

7. Shell Programming and Scripting

String comparison

Hi, I have the below logic. Here 'X' is a variable having some string. if then echo "i dont need to go to ofc" else echo "i need to go to ofc" Please help me to write it in unix. Thanks. (2 Replies)
Discussion started by: 46019
2 Replies

8. Shell Programming and Scripting

Help with String Comparison

I'm running the following script to compare string values to a regexp: for entry in $(lpinfo -v | cut -c 1-); do if then echo "blah" continue fi done Whenever I run it, each token of lpinfo is being interpreted as a command and I get errors such as: ... (2 Replies)
Discussion started by: hypnotic_meat
2 Replies

9. Shell Programming and Scripting

string comparison

Hello experts, (tcsh shell) Quite new to shell scripting... I have got a file with a single word on each line. Want to be able to make a comparison such that i can read pairs of words that are ROT13 to each other. Also, i would like to print the pairs to another file. Any help... (5 Replies)
Discussion started by: Jatsui
5 Replies

10. Shell Programming and Scripting

string comparison

The script will read a bunch of names, and test if it contains "John", but as below apparently ~ does not work, so what is the easiest way to perform string comparison in bash shell script? thanks ... elif then echo "get John" .... (2 Replies)
Discussion started by: fedora
2 Replies
Login or Register to Ask a Question