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.
while IFS= read -r line; do
echo "line is $line
if [ ! $line == "#*" ]; then
echo "# not found"
else
echo "# was found"
fi
There is a bit more in there but I cannot copy paste and I think the essence is correct. The else side of the if statement is not printing. What do I have wrong?
Edit: I made a change further up the code that should have no effect here. Now when the script runs the terminal window displays:
./find_where_each_so_is_used.sh line 93: [: too many arguments.
It puts that line on my screen twice. Line 93 is the if statement repeated here:
Code:
if [ ! $line == "#*" ]; then
I tried putting the "then" on the next line with no effect.
i am unsure about this syntax.
what are you checking for? if a line starts with a comment, or if it starts with "#*" literally?
and why is $line not in doublequotes?
which shell is it? bash or something else?
i always thought == is bash-specific, but then one would use [[...]] instead of [...].
and maybe the negator ! should be outside the square brackets? like so:
hello ondoho,
Well, remember this is the beginner's forum. As noted in the OP it is bash and the first line is: #!/bin/bash
The main body of the if was producing only what is desired so I did not see a need for the double quotes.
However, things went a bit funky and I added an EDIT to the OP.
Then I saw your post and put double quote as noted to make:
Quote:
if [ ! "$line" == "#*" ]; then
And that got rid of the new problem. And yes, I just want to suppress the lines that begin with the # and there is some code to do some greps that is commented out while getting the if statement right. But the else of the if is not taken and that puzzles me. And changing to [[ statement ]] made no difference.
The parser for case is like [[ ]], and the pattern glob in case is like ==
So you can write the latter as
Code:
while read line
do
if [[ "$line" == #* ]]
then
: # ignore it
else
echo "do something with $line"
fi
done
The quotes for $line after case and after [[ are not needed, but it is never a mistake to do so.
The #* must not be in quotes in order to keep its glob meaning.
Do not mix up [[ ]] with [ ]! The latter uses the command parser where you need to quote arguments, you cannot use a glob safely, and therefore only the = operator is applicable.
--
In post#1 the first echo statement is missing the ending quote.
I usually use [ ] and case rather than [[ ]] for portability reasons: case provides a way of globbing for shells that don't have [[ ]]. The "unnecessary" quotes are similarly partly for portability, and partly for style.
What we both forgot to do however was escape the '#' *
the correct way you see in allot of Makefile scripts (often written by experience people long ago) is:
[ c"foo" = c"$x" ] && { echo hi ; }
or
if [ c"foo" = c"$x" ] ; then flathead ; fi
why use c? there's a reason. just do it.
the above syntax is compatible with many shells (bash, zsh, ksh, tcsh). don't use just any syntax or if you buy, say a freebsd or apple imac: your scripts may all be broken
Last edited by X-LFS-2010; 07-07-2018 at 05:10 PM.
That practice is historical baggage that hasn't been needed for a very long time. Unless you're writing some sort of generic shell script that might end up running on god only knows what (such as ./configure scripts) then there's absolutely no point in using that hack. It's certainly pointless if you know your script will be running in bash, dash, ksh, zsh and probably every other common shell not written 40 years ago.
People keep using it because someone told them they should at some point in the past. Ask them why and they frequently won't be able to answer.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818
Rep:
[QUOTE=bkelly;5876467]Here is an excerpt from the script.
Code:
while IFS= read -r line; do
echo "line is $line
if [ ! $line == "#*" ]; then
echo "# not found"
else
echo "# was found"
fi
I'm with ondoho... Did you try putting "$line" in double quotes. As a general rule, I always do string comparison with quoted strings. There's too many things that can go wrong.
Plus, you forgot to close the text in the initial "echo" statement. Probably just a snafu in making the post, though. But if it isn't, all sorts of odd errors can crop up as the shell closes the quote using a double quote from another statement. I'm not crazy about too much syntax coloring but you might try using an editor like vim. If memory serves, quoted strings are colored green (YMMV) so if you see a whole lot of green (or whatever color) where you weren't expecting it, look for a missing quote somewhere. Same thing could happen when accidentally using a brace instead of a square bracket, etc.
No, [ ] cannot handle wildcards safely (the shell glob-matches against the current directory unless set -f was done), and unquoted variables are expanded.
Only [[ ]] provides the robustness for * globbing and unquoted variables.
Correct is
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.