LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2010, 06:42 AM   #1
vishnukumar
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Rep: Reputation: 0
If condition is throwing error


Hi,

I have created a shell script to find, whether the process is running or not. Below is my shell script.

Code:
#!/bin/bash

result=$(ps -ef | grep -v grep | grep "applicationname" | awk '{print $2}')
if [$result == ""];
then
echo "process is not running"
else
echo "process is running"
fi
When I execute this script, while the process (applicationname) is running, I get an error message like

"line 5: [8017: command not found
process is running"

But, I can say that "8017" is my process id. I don't know, how to fix this error? Why it throws?

Thanks
-Vishnu
 
Old 11-01-2010, 07:09 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Mind the space(s) between [ and $result and " and ].

if [ $result == "" ];

Hope this helps.
 
Old 11-01-2010, 08:09 AM   #3
vishnukumar
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Thanks druuna, it works fine. But, after the fix I got another error "line 5: [: ==: unary operator expected", then I google it and found that I have to add double quotes to the $result to fix it. Below is my script

Code:
#!/bin/bash

result=$(ps -ef | grep -v grep | grep "reportgen" | awk '{print $2}')

if [ "$result" == "" ];
then
echo "process is not running"
else
echo "process is running"
fi
Thanks
-Vishnu
 
Old 11-01-2010, 08:09 AM   #4
valen_tino
Member
 
Registered: Jan 2008
Posts: 105

Rep: Reputation: 28
Code:
#!/bin/bash

result=`ps -ef | grep -v grep | grep "applicationname" | awk '{print $2}'`
if [ -z "$result" ];
then
echo "process is not running"
else
echo "process is running"
fi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySql throwing connection error mystic_durle12 Linux - Server 23 01-01-2008 09:56 PM
USB is autodetecting but when its pull out throwing error sathyguy Linux - Newbie 4 08-22-2007 07:39 AM
Shared Home Directory Throwing Error Ingla Ubuntu 6 08-06-2007 06:21 AM
IPSec w/ RHEL4- Racoon throwing error messages s0n|k Linux - Security 2 03-30-2007 04:34 PM
SuSE YOU - throwing error neocookie Linux - Software 3 10-25-2004 02:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration