LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script string compare (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-string-compare-4175599207/)

secomax 02-07-2017 02:30 PM

bash script string compare
 
Hello
I'm new to Linux bash scripting
I was reading a tutorial about string comparison in bash scripts
I understand most of BUT one thing stops me.
the comparison of greater than syntax is very weird actually I don't understand it totally !!
I want a reasonable answer.
Why did he not put quotations on the text that contains spaces which are val2 while he puts the quotations on the if statements?
it actually works but not quite understandable !!!

Code:

#!/bin/bash
val1=text
val2=another text
if [ $val1 \> "$val2" ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi

and this is the tutorial I get that snippet

https://likegeeks.com/bash-script-easy-guide/

I hope you help me get something I can understand
thanks in advance.

rknichols 02-07-2017 03:32 PM

Quote:

Originally Posted by secomax (Post 5666712)
Why did he not put quotations on the text that contains spaces which are val2 while he puts the quotations on the if statements?
Code:

#!/bin/bash
val1=text
val2=another text
if [ $val1 \> "$val2" ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi


If you are referring to the use of $val2 in the echo commands, it is already within a quoted string. Otherwise, I don't understand what you are questioning.

secomax 02-07-2017 03:35 PM

why the quotes is not on the third line like this
val2="another text"

instead of writing it within the if statement?

ntubski 02-07-2017 03:47 PM

Quote:

Originally Posted by secomax (Post 5666712)
Why did he not put quotations on the text that contains spaces which are val2 while he puts the quotations on the if statements?
it actually works but not quite understandable !!!

Are you sure it works? It's giving an error for me:
Code:

$ ./str-compare.sh
./str-compare.sh: line 3: text: command not found
text is greater than

Quote:

Originally Posted by secomax
why the quotes is not on the third line like this
val2="another text"

instead of writing it within the if statement?

It's needed in both places. I guess he forgot it in the assignment line.

BW-userx 02-07-2017 03:49 PM

that is a logical not
Code:

> greater than
< less than

\< not less than
\> not greater than

that is my thought. Even though it is not working on the command line. :(

ok maybe that is for strings only.
Code:

(userx@voider.org⚡️~)>>$ val1=text
(userx@voider.org⚡️~)>>$ 
(userx@voider.org⚡️~)>>$ val2="another text"
(userx@voider.org⚡️~)>>$ 
(userx@voider.org⚡️~)>>$ if [ $val1 \> "$val2" ]

> then

> echo "$val1 is greater than $val2"

> else

> echo "$val1 is less than $val2"

> fi
text is greater than another text

which has more chars?

not greater \>

as text is not greater than another text it sends the first echo line, the response is written wrong is all.


Code:

val1=text

val2="another text"

if [ val1 not greater than val2 ] then

echo "$val1 is (not) greater than $val2"

else

echo "$val2 is greater than $val1"
fi

it should read the other way is all.

ondoho 02-07-2017 03:50 PM

Quote:

Originally Posted by secomax (Post 5666712)
the comparison of greater than syntax is very weird actually I don't understand it totally !!

neither do i, at least not for string variables.
i only ever use it inside the arithmetics (()) brackets:
Code:

if ((1 > 2))
then
  echo "Something is seriously wrong here!"
fi


secomax 02-07-2017 03:58 PM

Sorry my mistake
I forget the quotation
but this is not my question
I'm asking about the second quotation of the if statement
if [ $val1 \> "$val2" ]

why the quotation here?

ntubski 02-07-2017 04:04 PM

Quote:

Originally Posted by secomax (Post 5666781)
I'm asking about the second quotation of the if statement
if [ $val1 \> "$val2" ]

why the quotation here?

It's required to put quotes around $val2 because of word splitting. It would be better to also put quotes around $val1, even though in this example its value has no spaces so it won't be split.

Further reading:
http://mywiki.wooledge.org/WordSplitting
https://www.gnu.org/software/bash/ma...Splitting.html

BW-userx 02-07-2017 04:15 PM

the person that posted that example on that other page made a boo boo is all.

Code:

val1="another text plus 2"
 
val2="another text"
 
if [ "$val1" \> "$val2" ]

then
 
echo "$val1 is greater than $val2"
 
else
 
echo "$val1 is less than $val2"
 
fi

returns
Code:

(userx@voider.org⚡️~)>>$ val1="another text plus 2"
(userx@voider.org⚡️~)>>$ 
(userx@voider.org⚡️~)>>$ val2="another text"
(userx@voider.org⚡️~)>>$ 
(userx@voider.org⚡️~)>>$ if [ "$val1" \> "$val2" ]
>
> then

> echo "$val1 is greater than $val2"

> else

> echo "$val1 is less than $val2"

> fi
another text plus 2 is greater than another text


secomax 02-07-2017 04:18 PM

I'm sorry
last question

when I type it like this

Code:

#!/bin/bash
val1=text
val2="another text"
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi

it gives that error
./myscript: line 4: [: too many arguments

BUT when I type it as the tutorial like this

Code:

#!/bin/bash
val1=text
val2="another text"
if [ $val1 \> "$val2" ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi

it works well
that's what I want to say from the beginning

BW-userx 02-07-2017 04:30 PM

why use a not before the greater than is beyond me less than works because that is what it is actually doing

Code:

val1=1
 
val2=3
 
if [ $val1  \> $val2 ]

then
 
echo "$val1 is greater than $val2"
 
else
 
echo "$val1 is less than $val2"
 
fi

Code:

(userx@voider.org⚡️~)>>$ val1=1
(userx@voider.org⚡️~)>>$ 
(userx@voider.org⚡️~)>>$ val2=3
(userx@voider.org⚡️~)>>$ 
(userx@voider.org⚡️~)>>$ if [ $val1  \> $val2 ]
>
> then

> echo "$val1 is greater than $val2"

> else

> echo "$val1 is less than $val2"

> fi
1 is less than 3

that string thing is screwy actually

m taking time to read everything (almost)
Quote:

One tricky note about the greater than and less than for string comparisons MUST be escaped with the back slash because by just using the greater-than symbol itself in the script, no errors are generated, but the results are wrong. The script interpreted the greater-than symbol as an output redirection. So you should do it like that

Habitual 02-07-2017 04:37 PM

http://tldp.org/LDP/abs/html/comparison-ops.html

I've always used if [[ ... ]] for test constructs in bash.
Greater Than I believe is reserved for mathematical comparisons, Not strings?

Code:

man test && man bash
also

secomax 02-07-2017 04:44 PM

No, it is used for strings
I checked the documentation
although it work but I have no explanation !!! till now

rknichols 02-07-2017 04:57 PM

Quote:

Originally Posted by Habitual (Post 5666797)
http://tldp.org/LDP/abs/html/comparison-ops.html

I've always used if [[ ... ]] for test constructs in bash.
Greater Than I believe is reserved for mathematical comparisons, Not strings? also

In both the "[ ... ]" and "[[ ... ]]" constructs in bash, the ">" and "<" operators perform a string comparison. If you want numeric comparison you have to use "-gt" and "-lt".
Code:

# String comparison sees that "1" is less than "9"
$ if [ 10 \> 9 ]; then echo greater; else echo not greater; fi
not greater
# Numeric comparison looks at the whole number
$ if [ 10 -gt 9 ]; then echo greater; else echo not greater; fi
greater

$ if [[ 10 > 9 ]]; then echo greater; else echo not greater; fi
not greater
$ if [[ 10 -gt 9 ]]; then echo greater; else echo not greater; fi
greater

Note that the "[[ ... ]]" construct parses its arguments differently, and the ">" must not be escaped if you want it to be recoginzed as an operator.

Another wrinkle is that in the "(( ... ))" construct, the ">" and "<" operators have their traditional, mathematical meaning.
Code:

$ if (( 10 > 9 )); then echo greater; else echo not greater; fi
greater


ntubski 02-07-2017 04:59 PM

Quote:

Originally Posted by secomax (Post 5666789)
it gives that error
./myscript: line 4: [: too many arguments

That's because $val2 gets word split, like I said before.


All times are GMT -5. The time now is 08:54 AM.