Script not working as expected

Hi,

I have prepared a script and trying to execute it but not getting expected output. Could you please help and advise what is going wrong.
"If else" part in below script is not working basically.
I am running it on HP-UX.

for i in slpd puma sfmdb
do
echo "******\t$i\t*******"
echo "--------------"
if {"$i"=="puma"}; then
ps -ef|grep -i $i|grep -v grep |head -1|awk -F ' '  '{print "Username: "$1 "\nPID: "$2 "\tProcessname:"  $9}';ps -ef|grep puma|grep -v grep|sed -n '2,$p' |awk -F ' '  '{print "Username: "$1 "\nPID: "$2 "\tProcessname:"  $8}'
else
ps -ef|grep -i $i|grep -v grep |awk 'BEGIN {print "Username\tPID\t\tProcessname";}
{print $1,"\t\t",$2,"\t\t",$9;}
END{print "Report Generated\n--------------";}' 
fi
done

What is the output of your script? What is expected output?

1 Like
if [  "$i" == "puma"  ]; then
1 Like

No doing with below also not working

if [  "$i" == "puma"  ]; then

---------- Post updated at 06:50 AM ---------- Previous update was at 06:45 AM ----------

Error I am getting as below

ksh[5]: {slpd==puma}:  not found

expected output

******  puma    *******
--------------

Username: sfmdb
PID: 2282       Processname:postgres:
Username: puma
PID: 2434       Processname:/osmf/mgmt/UCPS/puma/webserverssl/bin/httpd
Username: puma
PID: 18823      Processname:/osmf/mgmt/UCPS/puma/webserverssl/bin/httpd
Username: puma
PID: 18844      Processname:/osmf/mgmt/UCPS/puma/webserverssl/bin/httpd

******  slpd    *******
--------------
Username        PID             Processname
root             2239            /opt/iscsi/bin/islpd
daemon           2237            slpd
Report Generated
--------------
******  sfmdb   *******
--------------
Username        PID             Processname
sfmdb            2282            postgres:
sfmdb            2277            /opt/sfmdb/pgsql/bin/postmaster
sfmdb            2283            postgres:
Report Generated
--------------

To get rid of the error try :

if [ "$i" = "puma" ] ; then

and here better use a double quote for sed instead of the single:

ps -ef|grep puma|grep -v grep|sed -n '2,$p' |awk -F ' '  '{print "Username: "$1 "\nPID: "$2 "\tProcessname:"  $8}'
1 Like

Hi I tried using below. but again getting error. Also use double quotes in sed

 
if [ "$i" = "puma" ] ; then

ksh[5]: {slpd: not found

Use [ .. ] not {..}

Hey Thanks for your reply

unfortunately no luck with this as well

ksh[5]: [slpd=puma]: not found

Post the fifth line of your script.

Can you pull out the version of your ksh and let us know which operating system you are using :slight_smile:

I've tried your script (with the [] mod and it works on my RHEL servers.

1 Like

Hi, may you double check that you have modified this part of your code with chacko193's advice

if [ "$i"="puma" ]; then

the space " " between [ and "$i"
and the space " " between "puma" and ]
is so important that without it there will be a error "[slpd=puma]: not found"

so make sure the code is not

if ["$i"="puma"]; then
1 Like

Thanks guys for your quick help Now script is working fine and as expected.
finally My script looks like as below and executed.

for i in slpd puma sfmdb
do
echo "******\t$i\t*******"
echo "--------------"
if [ "$i" = "puma" ]; then
ps -ef|grep -i $i|grep -v grep |head -1|awk -F ' '  '{print "Username: "$1 "\nPID: "$2 "\tProcessname:"  $9}';ps -ef|grep puma|grep -v grep|sed -n '2,$p' |awk -F ' '  '{print "Username: "$1 "\nPID: "$2 "\tProcessname:"  $8}'
else
ps -ef|grep -i $i|grep -v grep |awk 'BEGIN {print "Username\tPID\t\tProcessname";}
{print $1,"\t\t",$2,"\t\t",$9;}
END{print "Report Generated\n--------------";}' 
fi
done

So its a good learning for me to provide space in if [] statement
Also script is working fine with single quotes in sed command only otherwise with double quotes throwing error as
ksh[6]: p: parameter not set

Many Thanks