LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 07-17-2010, 05:28 AM   #1
mtaliancich
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Rep: Reputation: 0
Bash Script


Hey, I am very new to Linux and trying to figure out some scripts. I am not too much of a programmer and trying to learn it too. I am trying to run a small script for school and have this:

#!/bin/bash

if [ $1 =date +"%A" ] ; then
echo "Homework is Due"
else
echo "Do your Homework"
fi

I keep getting a binary operator expected for the if statement or getting bad command

I am running it - ./lab5if Friday (current day)
which then should tell me "Homework is Due"
But I keep getting the else statement

Please help, I really appreciate it!!
(This is not for a grade, just trying to learn)
 
Old 07-17-2010, 05:48 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well I think the answer to your question is the else in your script, ie "Do your Homework"

I am guessing that as this is for school you will have been provided a syllabus and literature (either electronic or physical).
Look up the section related to 'Command Substitution', which by the way is what you are attempting inside the 'if'.
 
Old 07-17-2010, 10:49 AM   #3
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 446

Rep: Reputation: 59
You can't simply put a command in an if statement and expect it to be executed and evaluated. You need to put date in either `command ...` this way, or $(command ...) this way.

Last edited by Dinithion; 07-17-2010 at 10:52 AM.
 
Old 07-17-2010, 06:34 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by mtaliancich View Post
#!/bin/bash

if [ $1 =date +"%A" ] ; then
echo "Homework is Due"
else
echo "Do your Homework"
fi
This has at least two huge beginner mistakes in it.

First, remember that the '[' is just a command, like any other command. It is not part of bash syntax.

In fact,:
Code:
[ ... ]
is identical to:
Code:
test ...
.

Also, test knows nothing about executing commands. It expects the first argument to be a string, the next to be an equals sign, and lastly another string (you did not put spaces around the = sign). To cause bash to execute the date command and pass its output to test, use the $() operator.

It's also a good idea to quote variables in [], because if it's an empty string the test command sees a missing argument, because bash will remove it if it's empty and not quoted.

So here it is:

Code:
if [ "$1" = $(date +'%A') ] ; then
  echo "Homework is Due"
else 
  echo "Do your Homework"
fi
 
Old 07-19-2010, 12:25 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Using double square brackets is best practice http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
 
Old 07-19-2010, 04:24 AM   #6
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 446

Rep: Reputation: 59
oh? I wasn't aware of that. Is the syntax inside the brackets the same for [ and [[?
 
Old 07-19-2010, 08:41 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
So unlike [], the [[]] opertaor actaully IS bash syntax?
 
Old 07-19-2010, 08:44 AM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Yes, if I remember right the [[...]] is a "Bashism" and may not work in other shells; it's not POSIX either, in case anyone's interested.
 
Old 07-19-2010, 09:19 AM   #9
mtaliancich
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks alot guys, it is very helpful! I appreciate everything!!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 08:01 PM


All times are GMT -5. The time now is 09:53 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration