Operating System - HP-UX
1833012 Members
2694 Online
110048 Solutions
New Discussion

variable definition in posix shel.l

 
SOLVED
Go to solution
Lee Tae-kyung
Regular Advisor

variable definition in posix shel.l

for F_SIZE in $(cat /var/adm/cron_src/fbackup_cold_backup.log | awk '{print $1}')
do
let F_SUM=$F_SIZE+$F_SUM
echo $F_SUM
done
echo $F_SUM
**************************************
The above shell is the sum of the files's size listed in the fbackup_cold_backup.log.
F_SUM is the type of integer.
I need to define the type of float.
How do I do?
Thanks for your answer in advance.
I think I am a specialist in IT Korea^^. I am a programmer and SE and DBA
5 REPLIES 5
Tim Adamson_1
Honored Contributor

Re: variable definition in posix shel.l

Hi,

I'm not sure if it is possible to define a float in the shell.

You can define an integer using the typeset command. Refer to the sh-posix man page for further details. It might provide other options which will give what you are after.

Hope it helps.


Tim
Yesterday is history, tomorrow is a mystery, today is a gift. That's why it's called the present.
curt larson_1
Honored Contributor
Solution

Re: variable definition in posix shel.l

the posix shell doesn't support floating point variables.

/usr/dt/bin/dtksh does though. and of course shells newer then 11/88.

using dtksh or ksh93, etc. the syntax would be typeset -E for floating point, scientific notation and typeset -F for floating point, fixed precision. and usually there is a preset alias "float" to declare floating point variables.

float var1
typeset -E var2
etc
Lee Tae-kyung
Regular Advisor

Re: variable definition in posix shel.l

Thanks~~
I found a posix manual..
the 'typeset' command haven't the type of float.
Is there any solution to define the float type?

Thanks for your answer in advance
I think I am a specialist in IT Korea^^. I am a programmer and SE and DBA
curt larson_1
Honored Contributor

Re: variable definition in posix shel.l

you can do it all within awk also

cat /var/adm/cron_src/fbackup_cold_backup.log |
awk '{
sum += $1;
printf("%f\n",$1);
} END {
printf("%f\n",sum);
}
curt larson_1
Honored Contributor

Re: variable definition in posix shel.l

if you want to do it in the shell

#!/usr/dt/bin/dtksh

float F_SUM

cat /var/adm/cron_src/fbackup_cold_backup.log |
while read F_SIZE rest
do
F_SUM=$(($F_SIZE + $F_SUM))
printf "%d\n" $F_SUM
done
printf "%d\n" $F_SUM