home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!m2.dseg.ti.com!ernest!cmptrc!marc
- From: marc@cmptrc.lonestar.org (Marc Elewitz)
- Subject: ksh: expression eval feature or bug?
- Message-ID: <C0GF6p.JxJ@cmptrc.lonestar.org>
- Date: Wed, 6 Jan 1993 22:44:49 GMT
- Organization: CompuTrac Inc., Richardson TX
- Lines: 93
-
- I finally figured out a way to get inline arithmetic expressions
- to evaluate properly, anywhere in a command line. Unfortunately,
- I got their by trial and error, and don't understand how it works!
-
- Can someone help me understand the following command sequence?
-
- $set -x
-
- Turn on ksh tracing to show what ksh is thinking about.
-
- $date
- + date
- Wed Jan 6 12:02:43 CST 1993
- $print $(date)
- + date
- + print Wed Jan 6 12:02:50 CST 1993
- Wed Jan 6 12:02:50 CST 1993
-
- The date command sends the date string to stdout. The construct
- $( ... ) is defined to execute ... as a command, where everything
- written to stdout is returned as the value. See example above, the
- output of the date command is returned, and printed.
-
- $((1+2))
- + let 1+2
-
- Double open parens is defined to be equivalent to the ksh let builtin
- which evaluates its parms as an expression. Nothing is, or should be,
- written to stdout.
-
- $print $(((1+2)))
- + print 3
- 3
-
- Although '((1+2))' sends nothing to stdout, 'print $(((1+2)))' displays
- the expression result. Why??
-
- And further,
-
- $(1+2)
- + 1+2
- ksh: 1+2: not found
-
- Single parens execute enclosing commands in a subshell environment.
- This attempts to execute '1+2' in a subshell, shouldn't and doesn't
- evaluate the expression, and unable to find a program of that name
- generates an error to stderr; but, sends nothing to stdout. Nothing
- interesting.
-
- $print $((1+2))
- + print 3
- 3
-
- Somehow again, while nothing should be sent by the command '(1+2)'
- to stdout, the expression result is printed.
-
- While I like the functionality of expression evaluation inline, I don't
- understand how it's happening, whether it's designed in or a bug? Should
- I stay away from it in scripts??
-
- This additional example seems to clearly be a bug:
-
- $x="$(((1+2)))"
- + x=3
- $print \"$x\"
- + print "3"
- "3"
-
- The assignment sets x to the expression result. The '\"' is used to
- surround the value of x with double quotes.
-
- $x="$( ((1+2)) )"
- + + let 1+2
- x= let 1+2
- og$ (This line actually displays)
- $print \"$x\"
- + print " let 1+2 og$"
- " let 1+2 og$"
-
- In this case, a let statement and some junk was assigned into x.
- Sometimes it's just the let. I've frequently seen a portion of my
- prompt or .profile. It doesn't seem right to me that the text of
- a statement should be assigned, ever - it should evaluate to the null
- string. bug. bug. bug.
-
-
- Thanks for any assistance.
-
- ============================================================================
- Marc Elewitz CompuTrac, Inc.
- 222 Municipal Drive 214/235-7176 x3300
- marc@cmptrc.lonestar.org Richardson, TX 75080 214/234-6280 (FAX)
- ============================================================================
-