LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-14-2018, 09:58 PM   #1
shlomo.hovir
Member
 
Registered: Oct 2018
Posts: 66

Rep: Reputation: Disabled
is is possible to use two conditions in one line shell scripting


is is possible to use two conditions in one line shell scripting

like this
if [ "$foo" = bar && "$foo" = bar ];

i tried this example but didn't work
 
Old 12-14-2018, 10:29 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
userx@MinTy:~
$ foo1=boo
$ foo2=bear

[ "$foo1" = "boo" ] && [ "$foo2" = "bear" ] && echo "say what?"
try that.
plus it is malformed
Code:
if [ "$foo" = bar && "$foo" = bar ];
two foo's same named variables. they can only hold one value so it will always come up false.
and
using 'if'
Code:
 if [ "$foo1" = "boo"  &&  "$foo1" = "bear" ]; then  echo "true"; else echo "false"; fi
bash: [: missing `]'
false
here
Code:
$ if [ "$foo1" = "boo" ] && [ "$foo1" = "bear" ]; then 
> echo "true"
> else
> echo "false"
> fi
false
and
Code:
 if [ "$foo1" = "boo" ]  &&  [ "$foo2" = "bear" ]; then  echo "true"; else echo "false"; fi
true
notice the naming of the vars.

Last edited by BW-userx; 12-14-2018 at 10:42 PM.
 
1 members found this post helpful.
Old 12-14-2018, 11:32 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
It's 'man test' that explains the usage of 'test'. ('[' is an alias for 'test'.)

Edit: example:
Code:
if [ "$A" = 'a' -a "$B" = 'b' ]; then echo Both equal; fi

Last edited by NevemTeve; 12-15-2018 at 06:38 AM.
 
Old 12-15-2018, 02:25 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
or
Code:
help test
since test is usually a shell builtin (and will take precedence over the coreutils binary).
also try
Code:
help [
and
Code:
help [[
 
Old 12-15-2018, 09:28 PM   #5
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by shlomo.hovir View Post
Code:
if [ "$foo" = bar && "$foo" = bar ];
i tried this example but didn't work
It works with bash improvement [[:
Code:
if [[ "${var1}" == "foo" && "${var2}" == "bar" ]];
So you have at least 4 possibilities:
  • if [[ cond1 && cond2 ]];: see above
  • if [ cond1 -a cond2 ];: see NevemTeve's post
  • if [ cond1 ] && [ cond2 ];: see BW-userx's post (works as well with [[)
  • [ cond1 ] && [ cond2 ] && ...: see BW-userx's post (works as well with [[)
 
1 members found this post helpful.
Old 12-16-2018, 12:39 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
and there is an additional trick which will work in some cases, so
Code:
# instead of
if [ "$var1" = bar && "$var2" = foo ];
# you can write:
if [ "${var1}${var2}" = barfoo ];
using -a -o && (or similar) may not do what you wish inside [ ], but works properly outside.
using [[ ]] is preferred (instead of [ ]
 
Old 12-16-2018, 01:35 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
Quote:
Originally Posted by NevemTeve View Post
'[' is an alias for 'test'.
No, unfortunately it is not really true. There is a binary /usr/bin/test, there is another /user/bin/[ which are not the same (different apps) - although yes, [theoretically?] they should work exactly the same way, but I'm not really sure about that (if they really identical). And additionally there is a bash built-in [ which may differ.
 
Old 12-16-2018, 11:30 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by pan64 View Post
No, unfortunately it is not really true. There is a binary /usr/bin/test, there is another /user/bin/[ which are not the same (different apps) - although yes, [theoretically?] they should work exactly the same way, but I'm not really sure about that (if they really identical). And additionally there is a bash built-in [ which may differ.
The GNU coreutils versions of [ and test do work the same way, with the exception that test doesn't handle common options like --version and --help: https://www.gnu.org/software/coreuti...nvocation.html

Bash's [ built-in is a synonym for its test built-in.

Code:
~$ help [
[: [ arg... ]
    Evaluate conditional expression.
    
    This is a synonym for the "test" builtin, but the last argument must
    be a literal `]', to match the opening `['.
~$ help test
test: test [expr]
    Evaluate conditional expression.
    
    Exits with a status of 0 (true) or 1 (false) depending on
    the evaluation of EXPR. [...]
 
Old 12-16-2018, 01:07 PM   #9
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by pan64 View Post
and there is an additional trick which will work in some cases, so
Code:
# instead of
if [ "$var1" = bar && "$var2" = foo ];
# you can write:
if [ "${var1}${var2}" = barfoo ];
Yes it works in some cases indeed, but it's less accurate, see below

Code:
var1=bar
var2=foo
if [ "${var1}${var2}" = "barfoo" ]; then echo "same"; else echo "different"; fi
-> returns "same"

Code:
var1=barf
var2=oo
if [ "${var1}${var2}" = "barfoo" ]; then echo "same"; else echo "different"; fi
-> returns "same" as well...

That's why, if you really want to merge conditions, I would prefer something like the following:
Code:
var1=bar
var2=foo
if [ "${var1}${IFS}${var2}" = "bar${IFS}foo" ];
 
Old 12-16-2018, 02:28 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
That last example is to me just getting to damn fancy for the "normal" reader. Where
Code:
if [[ "$foo" = "bar" ]] && [[ "$bar = "foo" ]] ; then ...
is to me more understandable to the lay person BASHing. namely me, and perhaps a few others..
 
Old 12-18-2018, 08:49 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204
Quote:
Originally Posted by pan64 View Post
No, unfortunately it is not really true. There is a binary /usr/bin/test, there is another /user/bin/[ which are not the same (different apps) - although yes, [theoretically?] they should work exactly the same way, but I'm not really sure about that (if they really identical). And additionally there is a bash built-in [ which may differ.
In Linux they are separate binaries that are 99% identical.
Traditionally in Unix /usr/bin/[ and /usr/bin/test are linked.
 
2 members found this post helpful.
Old 12-18-2018, 09:08 AM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
No links here
Code:
userx@voided.org:~
$ ls -la /usr/bin/[
-rwxr-xr-x 1 root root 51448 Jul 11 19:42 '/usr/bin/['
userx@voided.org:~
$ ls -la /usr/bin/test
-rwxr-xr-x 1 root root 47360 Jul 11 19:42 /usr/bin/test
 
Old 12-20-2018, 09:53 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As per pan64
Quote:
using [[ ]] is preferred
which I always(!) use, ref http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS and also when I was on the Korn shell course @ HP
 
  


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
sed one line extract between two strings on one line - several occurences Jykke Linux - Desktop 2 12-05-2012 09:08 AM
LXer: How To Use Conditions in Shell Scripts LXer Syndicated Linux News 1 04-02-2010 12:56 AM
Syntax Question for scripting - if conditions jim.thornton Linux - Newbie 2 04-08-2008 09:51 PM
New to shell scripting. Need help with conditions koobi Programming 12 11-09-2007 07:28 AM
Possible to use two network interfaces (one primary, one for backup)? massysett Linux - Networking 2 01-12-2006 09:01 PM

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

All times are GMT -5. The time now is 01:02 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
Open Source Consulting | Domain Registration