|
|
String comparisons don't work in shell scripts?I'm a bit baffled. I'm trying to compare string variables in a shell script to figure out if one value is lexically less (that is, would appear earlier in the dictionary) than another, but it doesn't seem to work at all? Help! I'm pretty sure you're asking about the conditional string expressions in the test command, typically denoted in shell scripts with the [ and ] symbols (the first of which, as a complete piece of trivia, is actually a hard link to the test binary. Do the following to see it: ls /usr/bin/? or /bin/? instead!) Anyway, you're trying to do something like this: if [ $stringvar1 > $stringvar2 ] ; then The problem with this notation, one that surprisingly few shell script books and articles mention, is that you need to escape the angle brackets or the shell will interpret them as redirection symbols! Indeed, if you have stringvar1=test and stringvar2=me then running that test would actually create an output file called "me", not at all what you want to accomplish! Instead, here's a snippet of code to experiment with that demonstrates the correct use of these string conditionals: #!/bin/sh Hope that helps clear up the mystery!
More Useful Shell Script Programming Articles:
✔ Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔ Parsing "id" strings in a Shell Script?Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔ Copy and Paste from the Mac OS X Command Line?I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔ Script to test line lengths for Twitter compatibility?I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔ Shell script to convert lowercase to title case?As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help
right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
Categorized:
Shell Script Programming
(Article 5867,
Written by Dave Taylor)
Tagged: linux, shell script programming, unix Previous: Can I use Asterisk with Net2Phone or Vbuzzer? Next: Motorola RAZR V3c: how do I use voice memos? Reader Comments To Date: 3Bruce Brown said, on January 23, 2012 7:03 PM:
In string comparisons in shell scripts, instead of using "test expression" or "[ expression ]," I suggest that folks try using the "extended test facility" (not available in all shells). The extended test facility is "[[ expression ]]," like so: [[ $str1 < $str2 ]] Notice that I purposely left out the backslash character before the angle bracket above. It seems to work fine on my system (OSX Snow Leopard ver. 10.6.8). The extended test facility is supposed to require less quoting (because it doesn't do word splitting or pathname expansion), and indeed, that seems to be the case here. Notice that I've included spaces liberally around everything above. It's been my experience in working with language translators (interpreters [like the shell command interpreters] and compilers) that this helps with debugging, because it often seems to make it easier for the translator to give more meaningful error messages when it encounters a syntax error. If you compress and run things together as much as possible, with as little white space as possible, then it makes things worse--not only for the error processing routines to try and guess at what you meant, but also for humans trying to read the code! So, I try to space things apart whenever possible; removing extra spaces only when absolutely necessary. It seems like, in some programming environments, programmers have traditionally done just the opposite. Of course, there are some situations where spaces must not be used; the trick seems to be in knowing when they're required, when they're optional, and when they must be left out. If you happen to know for sure that spaces won't work in a particular place, then don't put them in. Otherwise, if you're not sure whether spaces will work or not, I suggest that you either put them in (and test your code accordingly) or else read enough of the documentation to determine whether to include them or leave them out in that particular place. Note that using the extended test facility probably will make a shell script less portable. So, if portability is important to you, you probably should either do some testing on the different systems and shells you plan to use, or else stick to the other, more universal, ways of doing this, such as the "test expression" and "[ expression ]" facilities that are in most shells. The new code then becomes: #!/bin/sh a="aardvark" # then try 'zebra' if [[ $a < $b ]] ; then exit 0 The above code produced the correct output for me, both for "aardvark" and for "zebra". Mayank said, on September 21, 2012 3:04 AM:
Thanks a lot. Exactly what I was looking for.
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
Thanks for the Hint , For whomever you wrote it it helped my purpose