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:

#!/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

Try this:

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

Guru.

I tried the same, still im facing the same problem

Then go with the lock file concept ..

Add the below lines as the first and last line of your a.sh script.

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.

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 :slight_smile: