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.
No - Since you don't tell us what YOUR expected result is and what result you are getting that you think is wrong.
However, a couple of things that I notice:
1) You have a space between test= and the variable - that is not correct - you need to remove the space.
2) You have quotes around the variable ("A") which are unnecessary and might be treated literally in some contexts.
3) You do not have a space before your final right bracket which means the test is not recognized as such.
4) You not need quotes around $test in your test statements.
Try not to use negative checks (!=) try positive once (==) whenever possible. Have a look at these 2 examples:
Code:
#!/bin/bash
test="$1"
if [ ! -z $test ]
then
if [ "$test" == "AB" ]
then
echo "first: Match"
else
echo "first: No Match"
fi
fi
if [ "$test" == "AB" ] || [ "$test" == "BC" ]
then
echo "second: Match"
else
echo "second: No Match"
fi
I am getting False result no match I have now tried to pass it in as command line
like so
Code:
test="$1"
if [ ! -z $test ]
then
if [ "$test" != "AB"] || [ "$test" != "BC"]
then
echo "No Match"
else
echo "Exact Match"
fi
fi
so when i pass in "AB" i still get the wrong match -can any one spot whats going on ?
Why can i not get the exact match
So you want it to say "Exact Match" when the string is "AB" or BC"?
Basically, you're saying that it doesn't match if it's not "AB" or not "BC". If it's "AB", then it's not "BC", and you wrote that that means "No Match". Get it now?
so to speak ... it won't work quite as-writ but "you get the idea."
A "shell command" can be written in any programming language, thanks to "shebang" (#!), and you've probably got half-a-dozen of 'em out there on your computer right now.
Last edited by sundialsvcs; 06-14-2011 at 06:38 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.