0

So I'm trying to write a simple script that takes two stdin variables then tests to see if the string value of those two variables are equal to a set string. Here is my code:

echo "First Name:"
read fN

echo "Last Name"
read lN

if ((  $fN + $lN=="louis smith" ))
then
    echo "You are root, you may continue."
else
    echo "Access denied, you are not root."
first

When I run the script and enter the first name as "louis" and the last name as "smith" I get this error:

((: louis + smith==louis smith : syntax error in expression (error token is "smith ")

Then it tells me that I am not root when I clearly am. xD Any input helps :D

1
  • 1
    you think + is String concatenation, bash doesn't think so, Commented May 20, 2016 at 23:26

2 Answers 2

2

(( ... )) is for arithmetic expressions, you can't use it to compare strings. And + is for adding numbers, not concatenating strings. Use [[ ... ]] for conditional expressions.

if [[ "$fN $lN" = "louis smith" ]]
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot man, I thought that since I was adding the two variables together it would be considered Boolean, but I guess Boolean only refers to integers. Time to update the title...
@LouisSmith, no, integers aren't booleans either. A "boolean" is single-bit value -- something that's only either true or false, or coerced down to such a value.
@LouisSmith The boolean is the result of the comparison. But inside (()), you can only do arithmetic operations and numeric comparisons.
@CharlesDuffy That makes sense, so would it be like binary, with 1 being true and 0 being false?
@CharlesDuffy See the description of ((...)) in Conditional constructs
|
0

In addition to the other answer, you need to make sure you familiarize yourself with each of the ways the conditional can be properly written. There is no magic, just read the appropriate section of man bash. The [[ test is actually a Reserved Word in bash and is a bashism (it works in bash, but not in POSIX shell). It is the most flexible and forgiving test construct for bash and should be used if portability is not a concern. Make sure you understand the difference in quoting requirements, word splitting and pathname expansion, between the [[ test clause and the [ and test builtins.

In addition to [[, you can also make use of [ and test for testing. ([ and test are equivalent). The following are also correct and portable to POSIX shell:

if [ "$fN $lN" = "louis smith" ]

if test "$fN $lN" = "louis smith"

To answer your original question directly, you test for more than one condition by either using the -a (and) or -o (or) within the test expression itself (older syntax) or by separating multiple test expressions with && (and) or || (or). For instance to check for multiple conditions using your example you could do:

if [ "$fN" = "louis" -a "$LN" = "smith" ]

or

if test "$fN" = "louis" -a "$LN" = "smith"

Written using && or ||:

if [ "$fN" = "louis" ] && [ "$LN" = "smith" ]

or

if test "$fN" = "louis" && test "$LN" = "smith"

(note: no matter which you use you must always leave a space between the test expression and the [ and ] and a space or a newline (or line break indicator ;) between if test "$a" = b ; then...)

2 Comments

Wow, thanks a lot, that's a lot of new information to me. Definitely going to go test it all out. I'm really glad I joined this forum.
Sure, bash, like any shell or language, just takes time to learn and ferret out the details. Conditional expressions are one of those key pieces that you use in every aspect of scripting (or will see in other scripts). Good do know there are not just one flavor to look for, but at least 4 (I didn't cover the numeric comparisons (( ... )), but they are also part of the mix)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.