ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
This is part of a nested if statement and what I am trying to do is say'" If there is data that has a '"v0" or a "v2" or "v3" in the variable $ver then print this or else print all other variables except $ver"
I think your syntax is incorrect for matching. Here's an example similar to yours:
Code:
#!/bin/bash -
# @(#) s1 Demonstrate == for matching within [[, ]].
show_me()
{
local ver="$1"
echo " $FUNCNAME: looking at ver, $ver"
if [[ "$ver" = "v[0-2]" ]]
then
echo " single \"=\", variable ver, $ver, matched."
fi
if [[ "$ver" == v[0-2] ]]
then
echo " double \"=\", variable ver, $ver, matched."
fi
}
echo
echo " calling with v3"
show_me v3
echo
echo " calling with v0"
show_me v0
exit 0
Producing:
Code:
% ./s1
calling with v3
show_me: looking at ver, v3
calling with v0
show_me: looking at ver, v0
double "=", variable ver, v0, matched.
When all else fails, reading the documentation:
Quote:
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the
conditional expression expression. Expressions are composed of
the primaries described below under CONDITIONAL EXPRESSIONS.
Word splitting and pathname expansion are not performed on the
words between the [[ and ]]; tilde expansion, parameter and
variable expansion, arithmetic expansion, command substitution,
process substitution, and quote removal are performed.
When the == and != operators are used, the string to the right
of the operator is considered a pattern and matched according to
the rules described below under Pattern Matching. The return
value is 0 if the string matches or does not match the pattern [sic],
respectively, and 1 otherwise. Any part of the pattern may be
quoted to force it to be matched as a string
-- excerpt from man bash
Note also this is an expression for 0-2, not including 3.
I just noticed that you were asking about the Korn shell, but I replied with a bash example.
I changed "bash" to "ksh" in the shebang and the results were the same as the bash example, i.e. the double "=" and omitted quotes allowed the match to succeed. I used pdksh.
Y'know, suit yourself doing things like this with shell programming... but as for me, "that's what a real programming language is for." Perl, Python, and probably PHP (not to mention the special-purpose but very handy "awk") are probably all right there at your disposal.
Sure, this is my "JM2CW" and absolutely nothing more, but my take on the situation is that shell scripting is best suited to what is "a very simple command," whereas a general-purpose language like one of the above is purpose-built for "programs." When you run them, thanks to the "shebang" #! syntax, it's impossible to tell the difference.
There were 2 issues with your code. First, the "==". That informs ksh that you want the right-hand-side to be interpreted as a pattern. You understood the pattern concept, because you used the character-class symbols "[]".
However, the use of quotes causes the enclosed characters to be interpreted as plain characters, not as a pattern.
So in the function I created, I first used what you had -- a single "=" and the quotes, followed by another test with "==" with no quotes. In the function calls later in the script, the first test failed, the second test succeeded.
The bottom-line is to use "==" and omit quotes in order to cause pattern-matching.
I have not checked all the differences between ksh and bash, but most people get a lot of good information from http://www.tldp.org/LDP/abs/html/index.html , and for the "[[ ]]" sequence specifically from http://www.tldp.org/LDP/abs/html/tes...ucts.html#EX11 , which discusses the "[ ]" sequence, then shows how "[[ ]]" improves on that. In fact, it notes that "[[ ]]" was adapted for bash from ksh88.
There may be some confusion between "==" and "=~" in bash3, unfortunately. The "=~" is intended to allow extended regular expressions to be used in tests. The "=~" operator does not exist in pdksh, but I don't know about ksh. The operator was designed to follow the use in perl.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.