LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 08-27-2014, 07:06 AM   #1
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Rep: Reputation: Disabled
syntax for two different conditions in if statement


hello
i am trying to write a command like
if [ a == b ] && [ c == d ]
it gives me an error [: missing `]'
can any one help me with the syntax and also explain what this error means
thanks in advance
 
Old 08-27-2014, 07:12 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,172

Rep: Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672
that line looks actually ok, so you should tell us explicitly what caused your problem.
 
Old 08-27-2014, 07:23 AM   #3
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Original Poster
Rep: Reputation: Disabled
I have no idea what the problem is. Maybe [: missing `]'means that there is a bracket missing. I checkd and all the brackets are closing. Is there any other way of writing the syntax?
 
Old 08-27-2014, 08:12 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Do you have spaces around the brackets? Remember that [ is a synonym for test and you would not write, iftesta==b]
 
Old 08-27-2014, 08:25 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,105

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
this worx for me:
Code:
[schneidz@hyper Documents]$ bash --version
GNU bash, version 4.2.10(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[schneidz@hyper Documents]$ if [ a == b ] && [ c == d ]
> then
>  echo hello world
> else
>  echo l33t h4x0rz
> fi
l33t h4x0rz

Last edited by schneidz; 08-27-2014 at 08:27 AM.
 
Old 08-27-2014, 10:12 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
Well... assuming a b and c are shell variables, you need $a $b and $c to test the VALUE of the variables.

As in:

Code:
a=1
b=1
c=1

if [ a == b ] && [ b == c ]; then
    echo true
else
    echo false
fi
comes up false, but to test the value you need:

Code:
a=1
b=1
c=1

if [ $a == $b ] && [ $b == $c ]; then
    echo true
else
    echo false
fi
will come up true.

Note "==" is a string comparison, thus a == b is always false... If a/b/c are supposed to be numeric, then use -eq.

Last edited by jpollard; 08-27-2014 at 10:17 AM.
 
  


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
[SOLVED] standard for usage statement syntax hydraMax Programming 2 12-25-2011 07:34 PM
[SOLVED] Bash: if statement syntax ghantauke Linux - Newbie 3 12-03-2010 09:04 AM
Syntax error for multiple if conditions predatorz Programming 2 01-29-2009 01:44 PM
Syntax Question for scripting - if conditions jim.thornton Linux - Newbie 2 04-08-2008 09:51 PM


All times are GMT -5. The time now is 07:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration