Limit in bash for conditions?
I'm getting this error when I run a script.
Code:
[root@lnxapp bin]# ./update-chapter-webpage Code:
state=`cat $webroot/$basename/state` |
I was able to recreate the same error when $state held more than one word. So it fails when
Code:
echo $state Code:
word1 word2..etc |
Hi.
There are a few ways of dealing with this: Code:
#!/bin/bash - Code:
% ./s1 |
Quote correctly your variables unless you are SURE that they will never hold special characters like spaces.
|
Thanks for the help. I didn't realize the 2 word ones.
I fixed it by changing it from Code:
if [ $state != "Alabama" ] ..... Code:
if [ "$state" != "Alabama" ] ..... |
Quote:
Code:
if [ "x$state" != "xAlabama" ] ..... |
Quote:
case "x$state" in xAlabama | \ xAlaska | \ xArizona | \ xLastValue) DoSomething ;; *) DoSomethingElse ;; esac |
Hi.
The uninitialized variable situation could be addressed generally with bash setting nounset. The double brackets also handle uninitialized variables -- for example: Code:
#!/bin/bash - Code:
% ./s2 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. Best wishes ... cheers, makyo |
All times are GMT -5. The time now is 07:27 PM. |