Use grep result to execute next command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use grep result to execute next command
# 1  
Old 02-16-2009
Use grep result to execute next command

Hi I am trying to run 2 servers using a script one after the other.

I start the first one:

Code:
run.sh -c servername >> jboss_log.txt &

Then I have to wait until I see Started message in the log file before I launch the other server.

I can't use sleep because I am not sure how long it'll take for the server to startup. So I need something like

Code:
while (true) {
if [tail -f jboss_log.txt | grep Started ] //meaning when it was found
then 
   break
fi
}

start the next command

obviously this doesn't work,

any hint on how I could write something like this?


Thanks in advance
# 2  
Old 02-16-2009
Code:
run.sh -c servername >> jboss_log.txt &
wait $!

# 3  
Old 02-16-2009
Thanks for the reply,

however wait$! doesn't work because
run.sh -c servername >> jboss_log.txt &

is never terminated, never returns, it keeps writing to boss_log.txt forever. So my 2nd server doesn't start.
# 4  
Old 02-16-2009
So far I have

Code:
      5 
      6 
      7 while [ 1 ]
      8 do
      9 
     10    myvar= fgrep -c 'Started in' jboss_log.txt
     11 
     12    echo $myvar
     13 
     14   if [ $myvar==1 ]
     15   then
     16       echo "JBoss was launched successfully"
     17         break
     18       fi
     19       sleep 1
     20     done

But the if statement is always true even thou line 12 prints 0.
Can anyone tell me why?
# 5  
Old 02-16-2009
Quote:
Originally Posted by iririr
So far I have

Code:
      5 
      6 
      7 while [ 1 ]


The standard idiom is:

Code:
while :

Or:

Code:
while true

Quote:
Code:
      8 do
      9 
     10    myvar= fgrep -c 'Started in' jboss_log.txt


Syntax error. There can be no space after the equals sign.
Quote:
Code:
     11 
     12    echo $myvar
     13 
     14   if [ $myvar==1 ]


That will always be true as you are testing a non-empty string. There is no test operator in that statement.

And the test for numerical equality is -eq:

Code:
if [ $myvar -eq 1 ]

Quote:
Code:
     15   then
     16       echo "JBoss was launched successfully"
     17         break
     18       fi
     19       sleep 1
     20     done

But the if statement is always true even thou line 12 prints 0.
Can anyone tell me why?
# 6  
Old 02-16-2009
Thanks a lot for your help.

I fixed it :


while [ 1 ]
do
myvar=`fgrep -c 'Started in' jboss_log.txt`

if [ "$myvar" -eq "1" ]
then
echo "JBoss was launched successfully"
break
fi
sleep 5
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a multi-line shell command output and execute logic based on result

The following is a multi-line shell command example: $cargo build Compiling prawn v0.1.0 (/Users/ag/rust/prawn) error: failed to resolve: could not find `setup_panix` in `human_panic` --> src/main.rs:14:22 | 14 | human_panic::setup_panix!(); | ... (2 Replies)
Discussion started by: yogi
2 Replies

2. Shell Programming and Scripting

Execute command and show result in web page

Hi everyone, I have two question 1- I want to execute command in shell and after execution result show in a web server. (kind of making UI ) e.g. in shell root ~: show list item1 item2 item(n)in web server in a page draw a table and show those items in itno | name... (1 Reply)
Discussion started by: indeed_1
1 Replies

3. Shell Programming and Scripting

Grep command giving different result for different users for same command

Hello, I am running below command as root user #nodetool cfstats tests | grep "Memtable switch count" Memtable switch count: 12 Where as when I try to run same command as another user it gives different result. #su -l zabbix -s /bin/bash -c "nodetool cfstats tests | grep "Memtable switch... (10 Replies)
Discussion started by: Pushpraj
10 Replies

4. Shell Programming and Scripting

Grep result from dd command

Hi, I am running following command in a bash script for testing IO and use grep to get throughput number, but it did not work, it displayed everything: dd if=/dev/zero of=/dev/null bs=1G count=1 oflag=dsync | grep bytes | awk '{print $7}' 1+0 records in 1+0 records out 536870912 bytes... (2 Replies)
Discussion started by: hce
2 Replies

5. Shell Programming and Scripting

Different cmd to execute and based on some pattern parse it and then store result in xlx format

Hi I have a different requirement, I need to run some application on my device from a file app_name.txt one by one which is like this: /usr/apps/email /usr/apps/message /usr/apps/settings after each app while it is running I need to execute again one cmd like ps -ef |grep... (2 Replies)
Discussion started by: Sanjeev Roy
2 Replies

6. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

7. Shell Programming and Scripting

If the grep command returns any result set

my intension is to use a grep command inside the shell script and if any row is returned or not.. depending on the resultset i have to code on further. how to check this i mean.. could anyone help me out with the if condition how to use it here !! (4 Replies)
Discussion started by: gotam
4 Replies

8. UNIX for Dummies Questions & Answers

Execute grep command within a folder

Experts, i need your help to made an enhance query using the existing one. i'm using a command "find . -exec grep -l 'search string' {} \;" to search a particular sting within the file within a folder. Basically, my idea is to fine tune my search. What i want is, instead of searching all files... (2 Replies)
Discussion started by: gav_dhiman
2 Replies

9. Shell Programming and Scripting

Get execute result of su

I wrote a shell script. su user << EOF some commands result=$? EOF echo $result Other program determined by $result It outputs nothing. How to pass the variable 'result' "inside su" to "outside su"? (5 Replies)
Discussion started by: uativan
5 Replies

10. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies
Login or Register to Ask a Question