From: Geezer From The Freezer on
I want to increment a variable without using expr, and using
just shell builtins

i.e:-
VAR=1
VAR=`expr $VAR + 1`

I've seen it done before, but can't remember how its done using
sh or ksh - any pointers please?
From: Christophe Gaubert on
Geezer From The Freezer a ýcrit :
> I want to increment a variable without using expr, and using
> just shell builtins
>
> i.e:-
> VAR=1
> VAR=`expr $VAR + 1`
>
> I've seen it done before, but can't remember how its done using
> sh or ksh - any pointers please?

With bash, I can do :
(( var= $var + 1 ))
# or :
let "var=var+1"

I don't know if it's bash-specific ?


--
Christophe Gaubert
http://perso.wanadoo.fr/christophe.gaubert
Mail postý depuis un systýme libre GNU/Linux
From: Christophe Gaubert on
Christophe Gaubert a ýcrit :
> I don't know if it's bash-specific ?

At least, if I run bash with the "--posix" option, it works.


--
Christophe Gaubert
http://perso.wanadoo.fr/christophe.gaubert
Mail postý depuis un systýme libre GNU/Linux
From: Bill Marcum on
On Tue, 06 Sep 2005 16:05:43 +0100, Geezer From The Freezer
<Geezer(a)freezer.commy> wrote:
> I want to increment a variable without using expr, and using
> just shell builtins
>
> i.e:-
> VAR=1
> VAR=`expr $VAR + 1`
>
> I've seen it done before, but can't remember how its done using
> sh or ksh - any pointers please?

VAR=$((VAR+1))

--
Atlanta makes it against the law to tie a giraffe to a telephone pole
or street lamp.
From: William on
"Geezer From The Freezer" <Geezer(a)freezer.commy> wrote in message
news:dfkb88$cqf(a)netnews.net.lucent.com...
> I want to increment a variable without using expr, and using
> just shell builtins
>
> i.e:-
> VAR=1
> VAR=`expr $VAR + 1`
>
> I've seen it done before, but can't remember how its done using
> sh or ksh - any pointers please?

Do a groups search on Google within this newsgroup. There was a
LOT of traffic on ways to do this a couple of years ago. To get
you started, here's one version derived from that discussion
that I've used recently:

IncVar () # varname
{
eval 'IV_V="${'$1':-0}"' ; IV_C=1
while : ; do
case "${IV_V}.$IV_C" in
${IV_P}.*) break ;;
# This would trim up to four leading 0s
# 0${IV_P}.*|00${IV_P}.*|000${IV_P}.*|0000${IV_P}.*) break ;;
*9${IV_P}.1) IV_NV="0$IV_NV" ; IV_P="?$IV_P" ; continue ;;
*9${IV_P}.|*8${IV_P}.1) IV_NV="9$IV_NV" ;;
*8${IV_P}.|*7${IV_P}.1) IV_NV="8$IV_NV" ;;
*7${IV_P}.|*6${IV_P}.1) IV_NV="7$IV_NV" ;;
*6${IV_P}.|*5${IV_P}.1) IV_NV="6$IV_NV" ;;
*5${IV_P}.|*4${IV_P}.1) IV_NV="5$IV_NV" ;;
*4${IV_P}.|*3${IV_P}.1) IV_NV="4$IV_NV" ;;
*3${IV_P}.|*2${IV_P}.1) IV_NV="3$IV_NV" ;;
*2${IV_P}.|*1${IV_P}.1) IV_NV="2$IV_NV" ;;
*1${IV_P}.|*0${IV_P}.1) IV_NV="1$IV_NV" ;;
*0${IV_P}.) IV_NV="0$IV_NV" ;;
esac
unset IV_C ; IV_P="?$IV_P"
done
eval $1'="$IV_C$IV_NV"'
unset IV_C IV_P IV_NV IV_V
}

If you only need to increment through a limited
range, you can just do something like (untested
code):

# Maximum input value: 10
incVar() # varname
{
varName="$1"
set 1 2 3 4 5 6 7 8 9 10 11
eval shift '${'$varName':-0}'
eval $varName=\"$1\"
}


-Wm