LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-02-2012, 12:48 PM   #1
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 154

Rep: Reputation: 3
Running if statement from command line?


Hello All,

We have some software that we use through a web browser that runs some reports by passing a command to the shell's command line.

Is it possible for me to write something like this (below) but make it into all one line to be issued on the command line...?

For example:
Code:
GREP_RETCODE=1

while (( $GREP_RETCODE != "0" ))
do
	ps -ef | grep -v grep | grep cmd_to_find.sh
	GREP_RETCODE=$?
	if [[ $GREP_RETCODE == 0 ]]
	 then
		#RUN THE REPORT COMMAND
		exit 0
	 else
		##If NOT found sleep 1 and try the ps command again...
		sleep 1
	fi
done
If this doesn't make sense let me know and I'll try to clarify.

Basically what I'm trying to do is:
If the command "cmd_to_find.sh" is found I want to sleep for a moment and try again. If the cmd is not found in the ps -ef statement then Run the Report...

Any suggestions would be much appreciated.

Thanks in Advance,
Matt
 
Old 04-02-2012, 03:02 PM   #2
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 86
If is possible to strings shell commands together on a single line with semicolons, like this:

Code:
for f in `ls *`; do echo "$f"; done
However, unless there is some very good reason not to, you might want to consider placing the shell commands in a file, and executing the file. If the commands I used above were intended to be executed by bash, and the commands are in the file $HOME/src/bash/my_loop.bash then:

Code:
bash $HOME/src/bash/my_loop.bash
could be used to execute the commands.
 
Old 04-02-2012, 03:04 PM   #3
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,712
Blog Entries: 1

Rep: Reputation: 221Reputation: 221Reputation: 221
Assuming I made no typing errors, this should work....
Code:
GREP_RETCODE=1; while (($GREP_RETCODE != "0")); do ps -ef | grep -v grep | grep cmd_to_finish.sh; GREP_RETCODE=$?; if [[ $GREP_RETCODE == 0 ]]; then exit 0; else sleep 1; fi; done
 
1 members found this post helpful.
Old 04-02-2012, 03:08 PM   #4
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 154

Original Poster
Rep: Reputation: 3
Hey kakaka, thanks for the reply!

Yea you may be right... I was thinking of something else and I'm thinking I was a bit off base.
But that for loop on the command line is going to help me out. Thanks...


Thanks Again,
Matt
 
Old 04-02-2012, 03:10 PM   #5
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 154

Original Poster
Rep: Reputation: 3
Hey weibullguy, thanks for the reply too!

Cool, thanks for that... I'm thinking I'm going to have to go back and re-think what I'm going to do. (sad face)
But really, thanks for your suggestion.

Thanks Again,
Matt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting question - feed an input file into an if statement line-by-line kmkocot Linux - Newbie 10 01-18-2010 11:49 AM
How Can I Use An "if" statement on a Single Command Line? jagooch Linux - General 4 02-10-2009 11:32 PM
Issue a command line statement from within a C/C++ program? DiBosco Programming 7 05-23-2008 07:34 PM
bash case statement with multiple command line options pwc101 Programming 16 06-01-2007 11:43 AM
Running programs from Command Line SirLostalot Linux - Newbie 1 11-11-2002 09:24 PM


All times are GMT -5. The time now is 05:39 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration