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.
[root@lnxapp bin]# ./update-chapter-webpage
./update-chapter-webpage: line 270: [: too many arguments
./update-chapter-webpage: line 270: [: too many arguments
./update-chapter-webpage: line 270: [: too many arguments
./update-chapter-webpage: line 270: [: too many arguments
./update-chapter-webpage: line 270: [: too many arguments
This is the code. "if" is line 270 that the error refers too.
#!/bin/bash -
# @(#) s1 Demonstrate command test as bracket, quoting.
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset
echo
echo " Results with single [, no quotes:"
for state in Minnesota "New Jersey"
do
if [ $state != Minnesota ]
then
echo " state is not Minnesota: :$state:"
else
echo " state is Minnesota"
fi
done
echo
echo " Results with single [, quotes:"
for state in Minnesota "New Jersey"
do
if [ "$state" != Minnesota ]
then
echo " state is not Minnesota: :$state:"
else
echo " state is Minnesota"
fi
done
echo
echo " Results with double [, no quotes:"
for state in Minnesota "New Jersey"
do
if [[ $state != Minnesota ]]
then
echo " state is not Minnesota: :$state:"
else
echo " state is Minnesota"
fi
done
exit 0
Producing:
Code:
% ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
Results with single [, no quotes:
state is Minnesota
./s1: line 14: [: too many arguments
state is Minnesota
Results with single [, quotes:
state is Minnesota
state is not Minnesota: :New Jersey:
Results with double [, no quotes:
state is Minnesota
state is not Minnesota: :New Jersey:
The uninitialized variable situation could be addressed generally with bash setting nounset. The double brackets also handle uninitialized variables -- for example:
Code:
#!/bin/bash -
# @(#) s2 Demonstrate comparison of single and double brackets.
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
echo
echo " Results, single:"
if [ $state != Minnesota ]
then
echo " State is not Minnesota ( single [] ), :$state:"
fi
echo
echo " Results, double:"
if [[ $state != Minnesota ]]
then
echo " State is not Minnesota ( double [[]] ), :$state:"
fi
exit 0
Producing:
Code:
% ./s2
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
Results, single:
./s2: line 11: [: !=: unary operator expected
Results, double:
State is not Minnesota ( double [[]] ), ::
I like the case suggestion.
We don't know how often this code is executed, but if it is a "large number" of times, then there is a clever post at http://groups.google.com/group/comp....be3dcd9?hl=en# that may be useful. It shows how a hash function can be written for bash. The displayed code used a complicated method for the hash, which I replaced that with cksum because I found that easier to read. The array needs to be initialized, but after that, the hash of the text to lookup only needs to be calculated (and presumably checked for existence). If I had to guess, it's probably more useful for a large number of items through which to search, as opposed to a large number of executions, and also where a dynamic capability is required. Even if not useful in this situation, it might be for some other problem. It would be interesting to compare the methods -- an exercise for the reader.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.