On Wed, Dec 19, 2001 at 02:30:30PM +0800, xiong zhao <[EMAIL PROTECTED]> wrote: | Hello. I saw this in a shell script but don't know what it mean: | ... | read _ANSWER | :${_ANSWER:=$DEFAULT} | ... | What's the meaning of read and how to use it? And the same question | about the use of ":" and ":="? What's the difference between ":=" | and simply "="? Can someone explain?
The "read" reads a line of input (in an interactive script this will be from user, via the terminal) and stores the line in the variable $_ANSWER. The next line is actually mistyped. It should read: : ${_ANSWER:=$DEFAULT} (Note the space after the first colon.) This is actually a parameter substitution (see that section in "man sh"). The expression ${_ANSWER:=$DEFAULT} works as follows: - if $_ANSWER is set and not null (not empty) then use $_ANSWER - otherwise use $DEFAULT The := operator here is very much like :- except that also, as a side effect, it will set $_ANSWER to $DEFAULT if that was used. So from this point onwards the script can use $_ANSWER, which will have been filled in from $DEFAULT if needed. The ":" at the front in the null command, which does nothing (like "true" does, though historically ":" was built into the shell and "true" was a separate program). Its purpose here is so that the ${_ANSWER:=$DEFAULT} can take place (as an argument to ":") with no other side effects. In terms of what it achieves, this line: : ${_ANSWER:=$DEFAULT} and this line: [ -n "$_ANSWER" ] || _ANSWER=$DEFAULT do the same thing. Personally, I prefer the latter. Cheers, -- Cameron Simpson, DoD#743 [EMAIL PROTECTED] http://www.zip.com.au/~cs/ Jon Burns <[EMAIL PROTECTED]>: > I am willing to argue this, but be careful, I have plenty of > ammunition on my side, and I never lose a discussion. If this is true then you are either (a) incredibly lucky in that, by chance, you have always managed to be on the objectively correct side of things or (b) possessed of a head made out of solid granite. - William December Starr <[EMAIL PROTECTED]> _______________________________________________ Redhat-list mailing list [EMAIL PROTECTED] https://listman.redhat.com/mailman/listinfo/redhat-list