1833653 Members
3572 Online
110062 Solutions
New Discussion

Caluclations in script

 
SOLVED
Go to solution
jayachandran.g
Regular Advisor

Caluclations in script

My problem

i have some xml files whcih i'm intersted to convert in to a execl file and merge it.

i have planned to convert amd merge more then 25 files to sigle large file with a tab space so that i will be able to view as a exel file.

but each and every xml file is different from other so i cant use simple cat head and tail cmd.

now i'm thinking of using line numbers.

Need Solution

for this i want to do some calculations in the script i want my script to add $TT with 141
where $TT will be changing how to do this.

3 REPLIES 3
RAC_1
Honored Contributor
Solution

Re: Caluclations in script

First, be aware that shell calculations are limited to integer values. so sticking it to shell arithmatic is not a good idea.

you can do it as follows.

total=$(($TT+2))

Better way to do it, is with bc/bs

echo "$TT+96" | bc
echo "$TT+96" | bs
There is no substitute to HARDWORK
Ralph Grothe
Honored Contributor

Re: Caluclations in script

Have a look at the arithmetic evaluation section in the manpage of sh-posix.
Bear in mind that the shell's arithmetic capabilities are however somewhat restricted
(compared to most scripting or programming languages, or programs such as bc)
The manpage will tell you.
Nevertheless you can define shell variables to be treated as integers explicitly by the typeset built-in.
Variables marked such can easily be evaluated in double parentheses.

e.g.


$ typeset -i line_no=0; while read line; do printf "%0.4d\n" $((line_no+=1));done < /etc/hosts|tail
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058


If you were using a Perl script you could refer to the special package global $.
which holds the current line number
(see "perldoc perlvar" for further explanations)
Madness, thy name is system administration
jayachandran.g
Regular Advisor

Re: Caluclations in script

Hi

Thanks for your reply.

As if i'm going to use only addition not more than 3 digits i hope the answer you people provided will do.

Thank You.
And i'm using the procedure give by RAC as if that is very easy one to follow.