
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!
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon
Categorized:
Shell Script Programming
(Article 5867)
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? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. Thanks for the Hint , For whomever you wrote it it helped my purpose Posted by: Athreya at July 30, 2007 6:12 AMI have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Book Links
|