Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In a file i have a string like this which is the date of today

Sa 4. Dez

Date format is like this

stringtoday=`date +%Y-%m`

now i wanna use a if condition to check if the date of today exists in this file and if that's the case i want stop the bash script. if the date is not in this file. the script should run. How can i do this?

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

Something like:

#!/bin/sh

DATE=$(date +%Y-%m)

if grep "$DATE" file >/dev/null; then
    echo yes
else
    echo no
fi

where file is the name of your file should work. It basically uses grep to search for the value of $DATE in file, and branches depending on whether it was found or not (the exit codes of grep indicate this, see man grep "EXIT STATUS" for more information). You can use if grep …; then exit 0; fi to exit the script if the text is found to get the behaviour you want.

share|improve this answer
    
thanks a lot. helped me much. –  NES Dec 4 '10 at 14:38
2  
man grep. See what -q does. –  geirha Feb 3 '11 at 23:11
    
That s cool! Thanks! –  Iain Lane Feb 4 '11 at 14:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.