LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-07-2012, 09:55 AM   #1
buee
Member
 
Registered: May 2009
Posts: 81

Rep: Reputation: 16
Angry Multiple string variable comparisons in an if statement in bash


I have a number of variables that I need to verify that they match.

I have two databases of usernames, passwords, and speed packages. The database types are not the same. I need to make sure that everything is the same.

Basically:

Code:
if [ $db1user matches $db2user ] and [ $db1pass matches $db2pass ] and [ $db1package matches $db2package ] then

It's good
else

No bueno
$db1user, $db1pass, $db1package
$db2user, $db2pass, $db2package

fi
That's is obviously not a verbatim syntax. But if I posted every combination I've tried, this post would be 8 pages by itself.

I've been searching all morning but can't get the right combination of an if statement comparing multiple variables to other variables, etc. Tried different combinations of [], [[]], quotes, no quotes, -a, =, ==, etc. I'll either get that they all report OK when some are not, or I get that they're all bad when some are good. Or, I get a syntax error, usually with -a as the operator.

What's the proper way to compare 3 pairs of variables to make sure they match exactly.

Last edited by buee; 06-07-2012 at 09:56 AM. Reason: Clarification
 
Old 06-07-2012, 10:27 AM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
The following works for me
Code:
if[ $db1usr == $db2user -a $db1pass == $db2pass -a $db1package == $db2package ]; then
    echo "Its good"
else
    echo "Its not good"
    echo $db1user $db1pass $db1package
    echo $db2user $db2pass $db2package
fi

Last edited by weibullguy; 06-07-2012 at 10:29 AM.
 
Old 06-07-2012, 10:51 AM   #3
buee
Member
 
Registered: May 2009
Posts: 81

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by weibullguy View Post
The following works for me
Code:
if[ $db1usr == $db2user -a $db1pass == $db2pass -a $db1package == $db2package ]; then
    echo "Its good"
else
    echo "Its not good"
    echo $db1user $db1pass $db1package
    echo $db2user $db2pass $db2package
fi
That got it! I wish I had written down the dozens of different ways I tried it.
 
Old 06-07-2012, 12:33 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As long as you're using bash, and comparing text strings, the [[..]] test is safer and more flexible. You can use && and || inside them for and/or operators.

Code:
if [[ "$db1user" == "$db2user" && "$db1pass" == "$db2pass" && "$db1package" == "$db2package" ]]; then

	echo "It's good"
else

	echo "No bueno"

fi
Note also that "==" is not actually valid syntax in the old "[" test, although bash accepts it. You should properly only use the single-character "=" in single-bracket tests.


If you're comparing integers, however, you should use ((..)) instead.

Code:
if (( num1 == num2 && num3 == num4 && num5 == num6 )); then
Where num1-num6 are variables containing integer values.


http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression


Another option, and perhaps safer, more portable, and more readable in the long run, would be to simply use separate nested if statements.


Finally, remember, QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

This is particularly important when using the old "[" single-bracket test command.

(And actually, one of the benefits of "[[" is that it's one of the exceptions where quotes aren't needed, at least on the left hand side. The right hand side, however, does exhibit differing effects when using "==" and "=~". See the links I gave above for more.)
 
Old 01-02-2019, 08:21 AM   #5
tiikeri
LQ Newbie
 
Registered: Jan 2019
Posts: 1

Rep: Reputation: Disabled
Most simple way i think:

Compare if an item is in the array:

Quote:
array=("a" "bcd" "edf")

[[ "${array[@]}" =~ "a" ]] && echo ok || echo "false"
 
1 members found this post helpful.
  


Reply

Tags
bash scripting, compare, match, strings



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] Bash Multiple If statement michijo Programming 7 02-21-2012 07:22 AM
[SOLVED] Bash concatenating string to variable abercrombieande Programming 4 01-19-2011 07:04 AM
[SOLVED] BASH: if Variable -eq String not working worm5252 Programming 2 01-24-2010 03:07 PM
bash variable loses contents in mysql statement thedude2010 Programming 3 06-02-2006 04:15 AM
need to innitialize string variable with multiple spaces clsonnt Programming 3 08-11-2003 10:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:03 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