Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-25-2012
Registered User
 
Join Date: Jun 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Launch shell script if string match is found

I'm trying to write a simple shell script that looks at a given text file and if the only word in the file is 'completed', it launches another shell script.

So far I have this almost working...

if grep 'completed' $datafile
then...

however, using this logic the secondary shell script gets launched if the word shows up anywhere in the file. Is there a way to have the logic key in on only the first word in the file?
Sponsored Links
    #2  
Old 06-25-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts

Code:
grep "^completed" $datafile && /shell/script/to/trigger.sh || echo "completed is not in the first word"

Sponsored Links
    #3  
Old 06-25-2012
gary_w's Avatar
Registered User
 
Join Date: Oct 2010
Posts: 446
Thanks: 31
Thanked 96 Times in 88 Posts
First you want to know if the ONLY word is "completed", later you say if the FIRST word is "completed". Which is it? Could there be multiple lines? Could the word "completed" be anywhere on the line or starting a line? Could there be lines before or after the word "completed"? Could there be error messages instead of the word "completed" that need to be accounted for? Need more info.
    #4  
Old 06-25-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Location: Switzerland - GE
Posts: 4,633
Thanks: 118
Thanked 256 Times in 245 Posts

Code:
zra1:/home/vbe/wks $ echo $CONTENT  

zra1:/home/vbe/wks $ cat test003.txt
completed
zra1:/home/vbe/wks $ cat test003
if [ $(cat test003.txt|wc -w) -eq 1 ]
then 
   echo still OK
   read CONTENT < test003.txt
   if [ "$CONTENT" = "completed" ]
      then echo cool
   fi
else 
   echo not cool
fi
zra1:/home/vbe/wks $ ./test003
still OK
cool
zra1:/home/vbe/wks $ 

zra1:/home/vbe/wks $ cat test003.txt
completed
blahblah
zra1:/home/vbe/wks $ ./test003
not cool

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl script to look for a string in a file and append few lines if the match is found arunkumarmc Programming 1 03-04-2011 05:05 PM
help with script to send email and if subject line match is found axdelg Shell Programming and Scripting 2 07-07-2010 04:43 PM
Shell Script to launch C program gazmcc182 Shell Programming and Scripting 12 04-11-2010 04:04 PM
launch vnc session from unix shell script a1_win Shell Programming and Scripting 0 06-05-2009 02:27 PM
Can we launch a shell script automatically upon ssh login? rockysfr UNIX for Advanced & Expert Users 1 07-02-2007 10:48 PM



All times are GMT -4. The time now is 10:43 AM.