Script not returning what I am expecting

Hi there

I am trying to create a script where I am checking the process is being run by the correct user, when I go to run the script it is not returning what I am expecting and I am not 100% sure why!!!

First off I have determined what the process user should be at the top of the script

enterprise_manager_user="casupport"

Then I have ran the process as I would normally in the command line

em_process_user=`ps -efl | grep 'Introscope_Enterprise_Manager.lax' | grep -v grep| awk '{print $3}'`
  • An example below on the return from that:
[casupport@wycvlapph048 enterprisemanager]$ ps -efl | grep 'Introscope_Enterprise_Manager.lax' | grep -v grep| awk '{print $3}'
root
[casupport@wycvlapph048 enterprisemanager]$

I have then wrote my script saying if the em_process_user = enterprise_manager_user then do this if not do this. I am expecting it to return as root but as determined at the top of the script I have said enterprise_manager_user should = user casupport so should alert however it doesn't

if [ "$em_process_user"="$enterprise_manager_user" ]
then
        em_process_user_flag=0
else
        em_process_user_flag=1
fi

echo $em_process_user_flag
echo $em_process_user

When I execute the script it comes with the following:

[casupport@wycvlapph048 enterprisemanager]$ ./test.sh
0
root

Surely it should come back as 1?

Thanks in advance for any help!

Without digging deeper, on first sight there's two spaces missing around the = sign in the test condition.

While we're at it, did you consider using

ps -efl | awk '/Introscope_Enterprise_Manager.lax/ {print $3}'

in lieu of the looong grep ping chain?

Hi RudiC

Thanks the spaces seemed to fix the issue! Can't believe something so small caused the problem!

Thanks again

Have you considered using pgrep ?

pgrep -u "$enterprise_manager_user" 'Introscope_Enterprise_Manager.lax'
pgrep -v -u "$enterprise_manager_user" 'Introscope_Enterprise_Manager.lax'
 

Assuming that Introscope_Enterprise_Manager.lax is the name of the process you are looking for, the first pgrep will return all PIDs of those processes being run by the required user, while the second one will return all PIDs not being run by that user.

Andrew