We can use arithmetic operations inside shell script function:
function mess
{
if (( "$1" > 0 )) ; then
total=$1
else
total=100
fi
tail -$total /var/adm/messages | more
}
I try to do this arithmetic operations on the function args:
#!/bin/bash
byte="Bytes"
kilo="KB"
mega="MB"
giga="GB"
function bytesToUnites() {
if (( "$1" < 1000 ))
then
echo $1" "$byte
elif (( $1 < 1000000 ))
then
let $1/=1000
echo $1" "$kilo
fi
}
bytesToUnites 28888
But I get this error:
line 12: let: 28888/=1000: attempted assignment to non-variable (error token is "/=1000")
28888 KB
How can I fix this?
"
characters is not optimal. In almost all cases, expansion of$var
should be inside"
, not outside of it. So for exampleecho $1" "$byte
is bad,echo "$1 $byte"
is good. – kasperd Aug 03 '14 at 10:38