Visit Our UNIX and Linux User Community


If condition help required


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If condition help required
# 1  
Old 09-10-2013
If condition help required

I have a if condition it checks its pid exist it means it is running, otherwise not running.

I am checking with ps

Code:
x=`ps -fu myuserid|grep java| |grep -v grep | awk '{print $2}'`

if [$x -eq 0 ]

then ............

Above code is giving integer error, because currently process of java is not running so its value is zero, so how i can i do that to check process exist then tell us process exist, otherwise tell us process not exist.
# 2  
Old 09-10-2013
If should have space between brackets...
can reduce one grep
and avoid use of back tick's

try

Code:
x=$(ps -fu myuserid| grep -v grep | awk '/java/{print $2}')

if [ $x -eq 0 ]

This User Gave Thanks to pamu For This Post:
# 3  
Old 09-10-2013
Code:
x=$(ps -fu myuserid | grep [j]ava | awk '{print $2}')
if [ ! -z $x ]
then
  echo process is running
else
  echo process not running
fi

This User Gave Thanks to krishmaths For This Post:
# 4  
Old 09-10-2013
Code:
if [ $(ps -fu myuserid | grep -c [j]ava) -gt 0 ]
then    echo 'process is running'
else    echo 'process not running'
fi

These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-10-2013
Or:
Code:
ps -fu myuserid | grep [j]ava > /dev/null &&
  echo 'process is running' ||
    echo 'process not running'

Code:
if ps -fu myuserid | grep [j]ava > /dev/null; then
  echo 'process is running' 
else  
  echo 'process not running'
fi

If the grep implementation supports -q you can get rid of the > /dev/null part.
If pgrep is available (Linux/Solaris) you could substitute the above ps .. | grep .. pipeline with: pgrep -u myuserid -f [j]ava
This User Gave Thanks to radoulov For This Post:

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. Shell Programming and Scripting

Help with extract info if fulfill condition required

Input file (4 DATA record shown in this case): DATA AA0110 ACCESSION AA0110 VERSION AA0110 GI:157412239 FEATURES Location/Qualifiers length 1..1170 1..1700 /length="1170" position ... (5 Replies)
Discussion started by: perl_beginner
5 Replies

3. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

4. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

5. Shell Programming and Scripting

Getting required fields from a test file in required fromat in unix

My data is something like shown below. date1 date2 aaa bbbb ccccc date3 date4 dddd eeeeeee ffffffffff ggggg hh I want the output like this date1date2 aaa eeeeee I serached in the forum but didn't find the exact matching solution. Please help. (7 Replies)
Discussion started by: rdhanek
7 Replies

6. Shell Programming and Scripting

Help Required

Below is my code which gives output like this: Every time we have to open the script file and change the value of TG in BEGIN. what i want to do is that i give two three values at the sametime and then run the script and it gives output one by one at the same time. I think i have to define... (2 Replies)
Discussion started by: wakhan
2 Replies

7. Shell Programming and Scripting

Please Help required.

Hello guys I am new to awk programming. I need the below required script which can perform the following. I have a file containing below data. SNA NoCRD = 131 SNA OBarr = 3 SNC NoCRD = 76 SNC OBarr = 1 SND NoCRD = 155 SND OBarr = 5 SNE NoCRD = 100 SNE OBarr = 1 SNF NoCRD = 131 SNF... (2 Replies)
Discussion started by: jurial
2 Replies

8. Shell Programming and Scripting

End of loop condition required???

Hi i have a variable with lots of tokens seperated with spaces. e.g VAR="ABC DEF GHSD GHQS TUTSD JHDTQ QDHQ CDQKDGQ WQUTQD DQUTQD DQJGDQ QDTQD WDQUTQDU QDUGQD QDJGQD DQUTDUQ QDUIDTQ" i want to separate all of the above tokens and call a script with each of the token e.g sh script.sh <TOKEN>... (4 Replies)
Discussion started by: skyineyes
4 Replies

9. UNIX for Dummies Questions & Answers

help required

Hi All, Please help me in the following query need a command to get all the files in the present directory except the next day file format :: abc_<DD>.log $ls -l abc_10.log abc_10.log abc_11.log Now i need the output in abc.list which should contains $more abc.list abc_10.log... (5 Replies)
Discussion started by: thaduka
5 Replies

10. Programming

help required

hi i am new to c programming and found this on the net could someone tell me what it actually does, many thanks in advance cheers #include <fcntl.h> main() { int fd; fd = open("in1", O_RDONLY); printf("%d\n", fd); } and this too please #include <fcntl.h> main() { int... (1 Reply)
Discussion started by: ruffenator
1 Replies

Featured Tech Videos