
What's acceptable syntax in Unix and Linux sh scripts?
Dave, I ran across your sites and Wicked Cool Shell Scripts book while trying to convince myself that `sleep 0` is a valid and relatively portable command to use freely in my unix shell scripts.
I looked at a few of the Bourne shell scripts and they seem to use constructs like $( ... ) and $(( ... )) which are not standard Bourne shell (to my understanding). So I actually have three questions, rather than just one:
Well, this is a bonus shell scripting question, I'd say! I think that the first thing to realize is that the Posix standard for the "sh" shell includes a number of structures and constructs that are still not fully supported in some commercial Unixes (in particular Solaris 9's default /bin/sh isn't Posix compliant). According to Posix's sh man page, the $( ) notation is valid and legit. Of course, the man page doesn't explicitly talk about the notation, but an example about half-way down the page shows it in use, in the line: set $(getconf PATH). That's not entirely true. The man page might not be complete, but this section on command substitution documents that the sequence $(x) is functionally identical to `x`. On the same page, the section on arithmetic expansion explains how the $(( )) notation allows for easy access to rudimentary integer arithmetic functionality within a shell script. This is, of course, a good thing because it finally means that the long-used expr command can be retired and many scripts sped up quite a bit. So in terms of your question about what flavor of /bin/sh the scripts are written in, I'd say "the Posix flavor". By this point at the end of 2004 there's not much excuse why the default shell on your Linux or Unix box wouldn't be completely Posix-compliant. In terms of references on writing portable scripts, I'd say that your best bet is to familiarize yourself with the Posix standard and make sure that you stick with the commands, parameters and shell functionality detailed therein. There are also some Web sites worth a possible visit in this regard too: Writing Portable Bourne Shell Scripts, Shell Script Portability Guidelines, and What to Watch Out for When Writing Portable Shell Scripts. There's also a book on the subject, though I haven't read it so can't say whether it's good or not: Portable Shell Programming. It is out of print, so half.com or another used book venue might be a lot cheaper than Amazon's marketplace. I hope that helps clarify these issues with the evolution of the Bourne Shell into the world of Posix!
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Shell Script Programming
, Unix and Linux Help
(Article 3794)
Tagged: Previous: Why does so much of my spam have spelling errors? Next: Why is Google's images.google.com visiting my Web site? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. I checked the bash FAQ and it appears that the $() construct has been available in gnu bash since version 2.2 and the $(()) arithmetic function has been around since 2.0 I am running Ubuntu 7.04 right now and it uses 3.2.13. A quick check of some boxes that I have access too shows: Solaris 8 -- bash 2.03 I have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ 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 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
Join the List!
Book Links
|