LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2011, 05:31 PM   #1
Icewolf0927
LQ Newbie
 
Registered: Apr 2010
Posts: 7

Rep: Reputation: 0
IF Statement that compares today's date with a date entered


Hi
I am trying to write a simple IF statement in VI that would look at the systems date and compare it to a date that someone has entered in to the terminal and depending if the date matches it would display a message on the screen. I have gotten it to prompt for the date on the terminal but it doesn't matter what date I put in it just give the ELSE prompt. I am not sure what I am doing wrong.

Code:
echo "Enter Date"
read option
if [ "$option" = date +"%m-%d-%Y"]; then
        echo "Homework is due."
else
        echo "Do your homework!"
fi
Here is a sample of the output. I pulled the date to see what it looks like so I can make sure the format is the same.
Code:
[Icewolf@Crazy ~]$ date +"%m-%d-%Y"
08-13-2011
[Icewolf@Crazy ~]$ ./lab5if
Enter Date08-13-2011
./lab5if: line 5: [: missing `]'
Do your homework!
Thanks
Andy

Last edited by Icewolf0927; 08-13-2011 at 06:04 PM.
 
Old 08-13-2011, 06:34 PM   #2
roger_heslop
Member
 
Registered: Oct 2009
Location: Leander, TX
Distribution: Fedora 20
Posts: 97

Rep: Reputation: 35
Whenever you want to compare a value to the result of a command, try surrounding the command with $( ). The below will work:
Code:
echo "Enter Date"
read option
if [ "$option" = $( date +"%m-%d-%Y" ) ]; then
        echo "Homework is due."
else
        echo "Do your homework!"
fi
 
1 members found this post helpful.
Old 08-13-2011, 07:00 PM   #3
Icewolf0927
LQ Newbie
 
Registered: Apr 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks again. That worked.
 
Old 08-13-2011, 08:41 PM   #4
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
= vs == in bash script

Here's mine with a small difference.

I always use the double = sign (==) to test for equivalency. Most languages will assign values when using only one = sign ... the == does not assign a value but tests for a value.

Code:
#!/bin/bash

read -p "Enter Date: " option

if [[ $option == $(date +"%m-%d-%Y") ]]
then
        echo "Homework is due"
else
        echo "Do your homework!"
fi
 
Old 08-13-2011, 10:21 PM   #5
roger_heslop
Member
 
Registered: Oct 2009
Location: Leander, TX
Distribution: Fedora 20
Posts: 97

Rep: Reputation: 35
Well played tonyfreeman.
 
  


Reply



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
perl date range calculation to create drop statement hattori.hanzo Programming 2 11-08-2010 06:46 PM
Working within content management system, how to sort with date >= today? mitchell7man Programming 2 07-18-2009 01:04 AM
script help - pull date format out of file name and compare to today MaureenT Linux - General 5 11-06-2008 11:14 AM
Setting system date and time affecting the clock and date on BIOS satimis Ubuntu 7 09-21-2007 08:02 AM
How to append today's date in a script anjaan Programming 4 06-12-2004 08:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration