I am trying to do the following thing the bash shell. Plz tell me how can I do this ?
ramsize=4002
ramsize=ramsize/1000
You can use expr Like this :
$ SIZE="4002"
$ DIV=$(expr "$SIZE" / "1000" )
$ MOD=$(expr "$SIZE" % "1000" )
$ echo $DIV
4
$ echo $MOD
2
back to your example:
$ ramsize=4002
$ ramsize=$( expr "$ramsize" / "1000" ) //ramsize = 4
Update the last statement according to konsolebox comment ,
the last line before modification :
$ ramsize=$("$ramsize" / "1000" )
the last line after modification :
$ ramsize=$(expr "$ramsize" / "1000" )
ramsize=$(("$ramsize" / "1000" )). And (( ramsize = ramsize / 1000 )) is more efficient, especially (( ramsize /= 1000 )).ramsize=$(( $ramsize / 1000 )) it doesn't works when you quote vars see Arithmetic Expansion, i think C-style (()) for loops aren't POSIX , can you please explain to me why c style is more efficient?ramsize=$(( $ramsize / 1000 )), $(()) would still pass the expanded value to the function that assigns the value to $ramsize.expr, and with your comment i realized that there is something wrong :)expr is essentially obsolete for arithmetic, as all of its functions (other than regular-expression matching) have been incorporated into the POSIX shell via the $((...)) expression.With floats:
ramsize=4002
ramsize=$(echo "scale=4; $ramsize / 1000" | bc)
echo "$ramsize" ## Outputs 4.0020
$ramsize: the integer division (4) or the float division (4.002)?