Archive for January 10th, 2008

basic arithmetics

So I think it is very simple to do some shell scripting, but when you have to calculate some simple variables it is not very handy. So I asked my friend Google and here are some of the result, simply to add, subtract, multiply vars in the bash language!

VAR=55             # Assign integer 55 to variable VAR.
((VAR = VAR + 1))  # Add one to variable VAR.  Note the absence of the '$' character.
((++VAR))          # Another way to add one to VAR.  Performs C-style pre-increment.
((VAR++))          # Another way to add one to VAR.  Performs C-style post-increment.
echo $[VAR * 22]   # Multiply VAR by 22 and substitute the result into the command.
echo $((VAR * 22)) # Another way to do the above.
 
 
# some date arithmetics (yestarday and one month ago)
date -d “-1 day” +%d%m     #will give me the yesterdays date.
date -d “-1 month” +%d%m  #will give me date one month previous.
 
Example: 
YESTERDAY=`date -d"-1 day" "+%Y.%m.%d"`;
echo $YESTERDAY;