Operating System - HP-UX
1837097 Members
2261 Online
110112 Solutions
New Discussion

Re: How can I reduce count value in shell scripting

 
Saraswathy_1
Advisor

How can I reduce count value in shell scripting

I have set variable in script in between want to reduce the count by one.

Please advice.
9 REPLIES 9
Peter Godron
Honored Contributor

Re: How can I reduce count value in shell scripting

Hi,
man expr

a=10
a=`expr $a - 1`
echo $a
9
James R. Ferguson
Acclaimed Contributor

Re: How can I reduce count value in shell scripting

Hi:

Using the shell's arithmetic:

# X=10;X=$((X - 1));echo ${X}

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: How can I reduce count value in shell scripting

Another way which is easier to read is:

let MYVAR=$MYVAR-1

You can use normal math symbols in the arithmetic let statement:

let MYVAR=$MYVAR*3
let MYVAR=$VAR2-$VAR3+1


Bill Hassell, sysadmin
Ninad_1
Honored Contributor

Re: How can I reduce count value in shell scripting

There are several ways as mentioned above as well as

echo $MYVAR - 1 | bc

If you want to store in a variable
result=$(echo $MYVAR - 1 | bc)


Regards,
Ninad

Re: How can I reduce count value in shell scripting

In the superior Scripting language of Perl :) you can use the same pre-fix and post-fix operators as C/C++.

a++; increase by 1
a--; decrease by 1
Peter Nikitka
Honored Contributor

Re: How can I reduce count value in shell scripting

Hi,

I'm missing this format:

typeset -i var=12
((var-=1))

print $var says 11

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"

Re: How can I reduce count value in shell scripting

csh can also use the C type operators

--------------
#!/usr/bin/csh

@ a = 4
@ a--
echo "$a"
@ a += 2
echo "$a"
--------------
output:
3
5
Rodney Hills
Honored Contributor

Re: How can I reduce count value in shell scripting

You can say-

(( MYVAR=MYVAR - 1 ))
or equivalent
let "MYVAR=MYVAR - 1"

Note that you don't need $ in front of variables names since the shell is doing the arithmetic calculations.

HTH

-- Rod Hills
There be dragons...
Saraswathy_1
Advisor

Re: How can I reduce count value in shell scripting

thanks