How Do I Compare Two Sets of Variables in My Code?

0
12
Asked By CuriousCoder93 On

I'm facing an issue with my code where it's only checking the last two variables. I have two pairs: 'a' should match 'b' and 'x' should match 'y'. I want to compare 'a' and 'b' together with 'x' and 'y'. If either pair doesn't match, I want the output to be 'no match'. Here's the snippet I'm using:

```bash
if [[ "$a" == "$b" && "$x" == "$y" ]]; then
echo "match"
else
echo "no match"
fi
```
Can anyone help me figure out why it's not working?

2 Answers

Answered By ScriptingSavvy On

What about trying to rewrite your condition like this?
```bash
if [[ "$a" == "$b" ]] && [[ "$x" == "$y" ]]; then
echo "match"
else
echo "no match"
fi
```
I’ve had success with this format before! Don't forget about spaces too; they can sometimes cause issues.

CodeNoob67 -

I tried that approach initially but had no luck. Maybe there’s something else going on?

Answered By DebuggingDude45 On

It looks like your code logic should work fine. You might want to add some echo statements to debug. Try this: `echo "match: '$a' = '$b', '$x' = '$y'"` to see what values are being compared. It might help you track down the issue.

CheckItAgain -

I checked all the variables, and they match perfectly. Just to note, 'b' and 'y' are generated by commands (like cat for 'a' and the date command for 'y'), so maybe that's affecting it.

UserTesting123 -

I tried your suggestion, but even after changing some values, it still shows no match. It feels like it's treating it as an 'or' instead of an 'and'. Any thoughts?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.