]> git.ipfire.org Git - thirdparty/autoconf.git/commitdiff
Document $(( )) pitfalls.
authorEric Blake <ebb9@byu.net>
Tue, 6 May 2008 15:31:55 +0000 (09:31 -0600)
committerEric Blake <ebb9@byu.net>
Tue, 6 May 2008 15:46:34 +0000 (09:46 -0600)
* doc/autoconf.texi (Shell Substitutions): Mention octal
vs. decimal.  Mention autotest's at_func_arith.

Signed-off-by: Eric Blake <ebb9@byu.net>
ChangeLog
doc/autoconf.texi

index 59a311916bf1a82291fd34dfefeba800ab5f3ca7..c2d0f17accedbfd38079867ebd2bd3afbadeb668 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2008-05-06  Eric Blake  <ebb9@byu.net>
 
+       Document $(( )) pitfalls.
+       * doc/autoconf.texi (Shell Substitutions): Mention octal
+       vs. decimal.  Mention autotest's at_func_arith.
+
        Improve behavior of './testsuite 01'.
        * lib/autotest/general.m4 (AT_INIT) <at_func_validate_ranges>:
        Alter usage to eval its arguments, in order to normalize away
index a5e880ac198360848705f1608be55a8601951bef..cd9a389822016c0ff8f4d1d07f06d7062c85e092 100644 (file)
@@ -13203,6 +13203,44 @@ echo $(case x in x) echo hello;; esac)
 Arithmetic expansion is not portable as some shells (most
 notably Solaris 10 @command{/bin/sh}) don't support it.
 
+Among shells that do support @samp{$(( ))}, not all of them obey the
+Posix rule that octal and hexadecimal constants must be recognized:
+
+@example
+$ @kbd{bash -c 'echo $(( 010 + 0x10 ))'}
+24
+$ @kbd{zsh -c 'echo $(( 010 + 0x10 ))'}
+26
+$ @kbd{zsh -c 'emulate sh; echo $(( 010 + 0x10 ))'}
+24
+$ @kbd{pdksh -c 'echo $(( 010 + 0x10 ))'}
+pdksh:  010 + 0x10 : bad number `0x10'
+$ @kbd{pdksh -c 'echo $(( 010 ))'}
+10
+@end example
+
+When it is available, using arithmetic expansion provides a noticeable
+speedup in script execution; but testing for support requires
+@command{eval} to avoid syntax errors.  If shell function support has
+also been detected, then this construct can be used to assign @samp{foo}
+to an arithmetic result, provided all numeric arguments are provided in
+decimal and without a leading zero:
+
+@example
+if ( eval 'test $(( 1 + 1 )) = 2' ) 2>/dev/null; then
+  eval 'func_arith ()
+  @{
+    func_arith_result=$(( $* ))
+  @}'
+else
+  at_func_arith ()
+  @{
+    func_arith_result=`expr "$@@"`
+  @}
+fi
+func_arith 1 + 1
+foo=$func_arith_result
+@end example
 
 
 @item ^