Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-02-2008, 11:06 PM   #1
Registered: Mar 2006
Posts: 170

Rep: Reputation: 15
bash a file also a string?

i'm writing a cron job that will see if a program is running, and if it isn't, it will start it.

first of all this doesn't work
if [pgrep feh = ""]; then
echo "starting feh"
so I did this

pgrep feh | cat > /home/chaz/blerg

if [ blerg = ""]; then
echo "starting feh"
however this clearly isn't working becaues it does not run the 'if' when blerg is empty. I guess you can't compare a file to a string?

How can I implement this in a nonsucky way? Can I locally assign thhe result of pgrep to a variable as in pgrep feh = x?
Old 09-02-2008, 11:25 PM   #2
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 121Reputation: 121
I'd test the return code from pgrep. If it returns results then the return code is 0, if it fails to return anything then it is 1.

pgrep process_name
if [ $? -eq 1 ]; then
   echo start process_name
Old 09-03-2008, 12:07 AM   #3
Registered: Mar 2006
Posts: 170

Original Poster
Rep: Reputation: 15
cool, I think that's working.
Old 09-03-2008, 03:38 AM   #4
Senior Member
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
Your first attempt should have read
if [ -z "`pgrep feh`" ]; then
You second attempt should have read

if [ ! -s /home/chaz/blerg ]; then
Not space after a [ and before ] and quoting of string values/variables to be tested. -z test means test for a zero length string and return true if the string is zero length. -s tests for a file with zero size and returns true if it is not zero size.
If you wish to do any more bash scripting I suggest reading BASH Programming - Introduction HOW-TO and once you've read that, Advanced Bash-Scripting Guide.


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting capitalizing first letter or each word in a string hypexr Programming 6 06-16-2011 09:58 AM
extract string from file to variable [BASH] NikosNL Programming 13 05-07-2008 09:43 AM
Need bash script to get string from file marhen Programming 2 03-25-2008 09:01 AM
Bash Scripting Empty file john8675309 Linux - Software 2 08-08-2007 04:05 PM
BASH: Replace string in file with another eur0dad Programming 5 07-27-2006 04:29 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:01 PM.

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