Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
02-07-2017, 03:30 PM
|
#1
|
LQ Newbie
Registered: Feb 2017
Posts: 15
Rep:
|
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.
|
|
|
02-07-2017, 04:32 PM
|
#2
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,804
|
Quote:
Originally Posted by secomax
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.
|
|
|
02-07-2017, 04:35 PM
|
#3
|
LQ Newbie
Registered: Feb 2017
Posts: 15
Original Poster
Rep:
|
why the quotes is not on the third line like this
val2="another text"
instead of writing it within the if statement?
|
|
|
02-07-2017, 04:47 PM
|
#4
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,810
|
Quote:
Originally Posted by secomax
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.
|
|
|
02-07-2017, 04:49 PM
|
#5
|
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
|
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.
Last edited by BW-userx; 02-07-2017 at 05:09 PM.
|
|
|
02-07-2017, 04:50 PM
|
#6
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
Quote:
Originally Posted by secomax
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
|
|
|
02-07-2017, 04:58 PM
|
#7
|
LQ Newbie
Registered: Feb 2017
Posts: 15
Original Poster
Rep:
|
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?
Last edited by secomax; 02-07-2017 at 05:02 PM.
|
|
|
02-07-2017, 05:04 PM
|
#8
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,810
|
Quote:
Originally Posted by secomax
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
|
|
|
02-07-2017, 05:15 PM
|
#9
|
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
|
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
Last edited by BW-userx; 02-07-2017 at 05:17 PM.
|
|
|
02-07-2017, 05:18 PM
|
#10
|
LQ Newbie
Registered: Feb 2017
Posts: 15
Original Poster
Rep:
|
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
|
|
|
02-07-2017, 05:30 PM
|
#11
|
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
|
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
|
Last edited by BW-userx; 02-07-2017 at 05:34 PM.
|
|
|
02-07-2017, 05:37 PM
|
#12
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep:
|
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
|
|
|
02-07-2017, 05:44 PM
|
#13
|
LQ Newbie
Registered: Feb 2017
Posts: 15
Original Poster
Rep:
|
No, it is used for strings
I checked the documentation
although it work but I have no explanation !!! till now
|
|
|
02-07-2017, 05:57 PM
|
#14
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,804
|
Quote:
Originally Posted by Habitual
|
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
|
|
|
02-07-2017, 05:59 PM
|
#15
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,810
|
Quote:
Originally Posted by secomax
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:42 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|