home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / shell / 5287 < prev    next >
Encoding:
Text File  |  1993-01-07  |  3.1 KB  |  103 lines

  1. Newsgroups: comp.unix.shell
  2. 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
  3. From: marc@cmptrc.lonestar.org (Marc Elewitz)
  4. Subject: ksh: expression eval feature or bug?
  5. Message-ID: <C0GF6p.JxJ@cmptrc.lonestar.org>
  6. Date: Wed, 6 Jan 1993 22:44:49 GMT
  7. Organization: CompuTrac Inc., Richardson TX
  8. Lines: 93
  9.  
  10. I finally figured out a way to get inline arithmetic expressions
  11. to evaluate properly, anywhere in a command line.  Unfortunately,
  12. I got their by trial and error, and don't understand how it works!
  13.  
  14. Can someone help me understand the following command sequence?
  15.  
  16. $set -x
  17.  
  18.     Turn on ksh tracing to show what ksh is thinking about.
  19.  
  20. $date
  21. + date
  22. Wed Jan  6 12:02:43 CST 1993
  23. $print $(date)
  24. + date
  25. + print Wed Jan  6 12:02:50 CST 1993
  26. Wed Jan  6 12:02:50 CST 1993
  27.  
  28.   The date command sends the date string to stdout.  The construct
  29.     $( ... ) is defined to execute ... as a command, where everything
  30.     written to stdout is returned as the value.  See example above, the
  31.     output of the date command is returned, and printed.
  32.  
  33. $((1+2))
  34. + let 1+2
  35.  
  36.     Double open parens is defined to be equivalent to the ksh let builtin
  37.     which evaluates its parms as an expression.  Nothing is, or should be,
  38.     written to stdout.
  39.  
  40. $print $(((1+2)))
  41. + print 3
  42. 3
  43.  
  44.     Although '((1+2))' sends nothing to stdout, 'print $(((1+2)))' displays
  45.     the expression result.  Why??
  46.  
  47.   And further,
  48.  
  49. $(1+2)
  50. + 1+2
  51. ksh: 1+2:  not found
  52.  
  53.     Single parens execute enclosing commands in a subshell environment.
  54.     This attempts to execute '1+2' in a subshell, shouldn't and doesn't
  55.     evaluate the expression, and unable to find a program of that name
  56.     generates an error to stderr; but, sends nothing to stdout.  Nothing
  57.     interesting.
  58.  
  59. $print $((1+2))
  60. + print 3
  61. 3
  62.  
  63.     Somehow again, while nothing should be sent by the command '(1+2)'
  64.     to stdout, the expression result is printed.
  65.  
  66. While I like the functionality of expression evaluation inline, I don't
  67. understand how it's happening, whether it's designed in or a bug?  Should
  68. I stay away from it in scripts??
  69.  
  70. This additional example seems to clearly be a bug:
  71.  
  72. $x="$(((1+2)))"
  73. + x=3
  74. $print \"$x\"
  75. + print "3"
  76. "3"
  77.  
  78.     The assignment sets x to the expression result.  The '\"' is used to
  79.     surround the value of x with double quotes.
  80.  
  81. $x="$( ((1+2)) )"
  82. + + let 1+2
  83. x= let 1+2
  84. og$                 (This line actually displays)
  85. $print \"$x\"
  86. + print " let 1+2 og$"
  87. " let 1+2 og$"
  88.  
  89.     In this case, a let statement and some junk was assigned into x.
  90.     Sometimes it's just the let.  I've frequently seen a portion of my
  91.     prompt or .profile.  It doesn't seem right to me that the text of
  92.     a statement should be assigned, ever - it should evaluate to the null
  93.     string.  bug. bug. bug.
  94.  
  95.  
  96. Thanks for any assistance.
  97.  
  98. ============================================================================
  99. Marc Elewitz              CompuTrac, Inc.                 
  100.                           222 Municipal Drive             214/235-7176 x3300 
  101. marc@cmptrc.lonestar.org  Richardson, TX 75080            214/234-6280 (FAX) 
  102. ============================================================================
  103.