Linux - NewbieThis 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.
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.
Aaarg this hurts
-eq is for comparing numbers!
@Shadow_7, do you confuse it with perl where eq compares strings?
Not confused with perl, as I never really did much in perl. Using so many symbols to type your variables seemed inefficient. But I could be confused with a number of other programming options.
$ if [ "YES" -eq "NO" ]; then echo "true"; fi
bash: [: YES: integer expression expected
Seems like it is for "integer expression"'s.
$ if [ "YES" = "YES" ]; then echo "true"; else echo "false"; fi
true
$ if [ "YES" = "NO" ]; then echo "true"; else echo "false"; fi
false
I used to always get messed up in C because using an = in there SETS THE VALUE, and == is the COMPARE operator. Where setting the value will ALWAYS return true. Baring rare exception, solar flares, faulty hardware, ... The always false option would likely not compile.
$ if [ "YES" == "YES" ]; then echo "true"; else echo "false"; fi
true
$ if [ "YES" == "NO" ]; then echo "true"; else echo "false"; fi
false
In bash I don't think it matters as both seem to work.
I used to always get messed up in C because using an = in there SETS THE VALUE, and == is the COMPARE operator.
In bash you can use == for comparison, that will make the life easier (although = and == are exactly the same - as far as I know).
In bash use strings (like -eq, -gt ...) for numerical comparison and use signs (like =, > ..) for string comparison.
Yes, bash treats == like = in the test or [ command.
Because it is a command, you must escape < and > string comparison operators!
See also this
Perhaps you should get used to [[ ]] that is not a command:
== is preferred over =
< and > must not be escaped
$variable does not need quotes because no argument expansion takes place
And it has got a feature:
== treats the right operand as a glob pattern, unless it is quoted.
For demonstration:
In fact on most Unix OS (but not GNU/Linux) there is a link /usr/bin/[ --> /usr/bin/test
And when /usr/bin/test is invoked as [ it just requires a closing ] as the last argument.
In contrast, the [[ ]] is a built-in, and [[ is not a command.
Within [[ ]] the shell uses a different parser that avoids the problems with the command parser.
The only drawback with [[ ]] is that it is not yet standardized (Posix).
It works in bash/ksh/zsh but maybe not in /bin/sh.
I am new at Linux and Bash. I think I have gotten off track and I don’t understand most of what has been posted. I do not understand the post of GazL at all. MadeInGermany writes about “case” then the code does not look like case at all. Well, not what I am accustomed do.
The first part of my bash script builds a file that is a list of files to look for. In there I decided to put some comments and create them with the # as the first character. That seems to be in keeping with the Linux scripting concept. Now I want the IF statement to skip over them.
The second part does some greps and has that noted if statement. Right now that segment of code looks like this:
Code:
echo “4 line is $LINE >> $LIST_OF_REFERENCES
if [[ “$line” == “#*” ]];
then
echo “hash found”
else
echo “hash not found”
fi
The echo statement does output a line beginning with the 4 character and the contents of each line.
The first three lines of the file have the character # as the first character. The first part of the if is never taken. I don't understand why not.
Agreed we went a bit off with an experts discussion.
Still you should be able to get your bits out of the many posts.
Here is the annotated correction for your initial post:
Code:
while IFS= read -r line; do
echo "line is $line"
# The " at the end was missing!
if [[ "$line" != "#"* ]]; then
# The * is a glob match (or wildcard) character
# Only [[ ]] provides safe glob matches
# The literal # must be escaped: \# or "#" or '#'
echo "# not found"
else
echo "# was found"
fi
done
And your previous post:
Code:
if [[ "$line" == "#"* ]]
# The literal # must be escaped but not the * (unless you want to compare with a literal *)
then
echo "hash found"
else
echo "hash not found"
fi
Yes, there has been a lot of discussion and opportunities for learning here. It's been a most enlightening thread, IMO.
Code:
echo “4 line is $LINE >> $LIST_OF_REFERENCES
if [[ “$line” == “#*” ]];
then
echo “hash found”
else
echo “hash not found”
fi
Couple of obvious things. First, is the variable $LINE or $line? ...they are not the same. Case matters.
Use the correct variable name in the if statement.
Re-read the posts and find the one that tells you how to write the regexp for the match.
You are currently trying to match a line that is equal to "#*" That is, one that contains only a "shesplat".
[Oh...MadeInGermany reposted it for you...]
Did you ever put set -x in the script so you could see what it is doing?
[Part of the reason we've been rambling is your 6-day absence...more frequent feedback from you would be helpful, IMO]
scasey,
Sorry about the delay, yes, that makes it a bit difficult. I went through a bunch of iterations today and the working code, only the essential bits, is:
Code:
if [[ "$line" == "#"* ]];
then
echo "found"
else
echo "not found"
fi
The # character is not escaped. Then I simplified the IF to
Code:
if [ "$line" == "#"* ];
using only single brackets and it did not work.
OK, so that is the way it is.
I thank each of you for your time and patience with me.
Quite a while ago I coined this phrase and told my boss. I still use it:
The hard stuff?
I do that every day.
Its the easy stuff that kicks my ass!
scasey,
Sorry about the delay, yes, that makes it a bit difficult. I went through a bunch of iterations today and the working code, only the essential bits, is:
Code:
if [[ "$line" == "#"* ]];
then
echo "found"
else
echo "not found"
fi
The # character is not escaped. Then I simplified the IF to
Code:
if [ "$line" == "#"* ];
using only single brackets and it did not work.
OK, so that is the way it is.
I thank each of you for your time and patience with me.
Quite a while ago I coined this phrase and told my boss. I still use it:
The hard stuff?
I do that every day.
Its the easy stuff that kicks my ass!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.