The example
#!/bin/sh
export $REG=0x000000
export $DONE=0x4A9FFF
while $REG -le $DONE
do
(($REG+1))
printf 'wm8280_reg get 0x'$REG'\r\n'
sleep 1
done
should change:
- the two lines with
export
should not have $
prefixing the variables
- use
[
and ]
around the $REG -le $DONE
expression (or use test
before)
- quote variables when they are read, as in the expression.
- use
printf
for formatting, not string-concatenation
So you would have something like
#!/bin/sh
export REG=0x000000
export DONE=0x4A9FFF
while [ "$REG" -le "$DONE" ]
do
(($REG+1))
printf 'wm8280_reg get 0x%s\r\n' "$REG"
sleep 1
done
This line
while [ "$REG" -le "$DONE" ]
could also be written as
while test "$REG" -le "$DONE"
(both are legal).
A quick read of dash's manual page does not indicate that it honors hexadecimal constants. You may have to change those exports to use decimal values, e.g.,
export REG=0
export DONE=489063
and the printf could then use %06X
rather than %s
, to print the values in hexadecimal.
Using cuonglm's suggestion, instead you could use
while [ "$((REG))" -le "$((DONE))" ]
and
REG=$(($((REG))+1))
That refers to POSIX shell 2.6.4 Arithmetic Expansion:
Only the decimal-constant, octal-constant, and hexadecimal-constant constants specified in the ISO C standard, Section 6.4.4.1 are required to be recognized as constants.
However, testing dash on Debian 7 and 8, the increment-expression probably does not work as intended by OP, because the result is decimal, while the printf
assumes it is hexadecimal. Also, REG=((REG+1))
does not work in that version of dash. (bash is a different story, of course).
It is running, but not incrementing, so I will continue to plug away at it.
– The Other NumberSix Jan 29 '16 at 01:45echo $((0x<hex_string>))
to convert hex string to decimal in POSIX shell. So, just[ "$((REG))" -le "$((DONE))" ]
. – cuonglm Jan 29 '16 at 01:46((REG=REG+1))
And it seems to have done it. Thanks for all your help. I owe you a beer.
– The Other NumberSix Jan 29 '16 at 01:59x=$((x+1))
,: "$((x+=1))"
,x=$(($((x))+1))
,x=$(($x+1))
are all POSIX as long as$x
contains a valid integer constant (decimal, octal or hexa), but some old versions ofdash
only supported the latter as earlier versions of the POSIX spec were unclear about it (still not very clear but a bit better),: "$((x++))"
is not POSIX though allowed (as in$((x++-1))
can be either$(((x++) - 1))
or$((x + +(-1)))
– Stéphane Chazelas Feb 01 '16 at 20:54REG=((REG+1))
works. ITYM((REG = REG + 1))
(or((REG++))
in those shells that have that non-standard((...))
construct). – Stéphane Chazelas Feb 01 '16 at 20:59(($REG+1))
is not standard sh syntax, and even in shells that have((...))
, that wouldn't make much sense. The arithmetic operators of the[
command only recognise decimal numbers, not hexadecimal (even in bash or ksh or zsh). – Stéphane Chazelas Feb 01 '16 at 21:24$((x))
always safe to convertx
to integer.sh -c 'echo "$(($1))"' _ "-0xA"
worked in all POSIX shells I tried. Heirloom sh, schily osh and schily sh all printed the string as-is. – cuonglm Feb 02 '16 at 02:01$((...))
is a POSIX thing (coming from ksh), it's not Bourne. Yes, like I said, $((x)) is POSIX as long as x contains a valid integer constant (decimal, octal or hexadecimal), but didn't work in dash prior to 0.5.5.$(($x))
is also POSIX and works in all shells. Watch out for things like$((-$x))
that don't work properly for negative values if$x
in shells that support the--
operator. – Stéphane Chazelas Feb 02 '16 at 09:28