|
Home > Archive > Unix Shell > December 2007 > Comparing two scripts...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Comparing two scripts...
|
|
| apogeusistemas@gmail.com 2007-12-10, 7:20 pm |
| Hi Unix Masters:
Can You tell me why these 2 scripts show me non equal results ?
# cat cp6
#!/bin/ksh
clear
echo
LA="01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24 25 26 27 28 29 30 31"
echo "Entre com os sabados, domingos e feriados a serem excluidos -
separados por espaco: "; read weekend_days
lav=`/usr/xpg4/bin/grep -vf <(echo $weekend_days | tr ' ' '\n') <(echo
$LA |tr ' ' '\n')`
echo " Entre com o endereco do arquivos sar binarios: " ; read
endereco
dias=`echo $lav | awk '{ print NF }'`
linecounter=1
integer num
integer avg
sarpath="$endereco"
who -a > /tmp/sar_cpu99
rm /tmp/sar_cpu??
for I in $lav
do
sar -u -f $endereco/sa$I > /tmp/sar_cpu$I
done
while read dados
do
for dia in `echo $lav`
do
linha=$(head -n $linecounter /tmp/sar_cpu$dia | tail -1)
set -A linha `echo $linha`
case "${linha[4]}" in
[0-9]*)
num[4]=$((num[4]+linha[4]))
;;
*)
echo "" > /dev/null
;;
esac
done
case "${linha[4]}" in
[0-9]*)
avg[4]=$((num[4]/$dias))
echo "${linha[0]} ${avg[4]}"
;;
*)
echo "" > /dev/null
;;
esac
linecounter=$((linecounter+1))
done < sar_template2
# cat qgz
#!/bin/bash
clear
echo
LA="01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24 25 26 27 28 29 30 31"
echo "Entre com os sabados, domingos e feriados a serem excluidos -
separados por espaco: "; read weekend_days
lav=`/usr/xpg4/bin/grep -vf <(echo $weekend_days | tr ' ' '\n') <(echo
$LA |tr ' ' '\n')`
echo " Entre com o endereco do arquivos sar binarios: " ; read
endereco
dias=`echo $lav | awk '{ print NF }'`
LINECOUNTER="1"
SAPATH="/tmp" # dir of your sa-files
SUM=(0,0,0,0)
DAYS="$dias" # number of days
rm /tmp/sa??
for I in $lav
do
sar -u -f $endereco/sa$I > /tmp/sa$I
done
while read FOO
do
SUM=(0,0,0,0)
for I in $lav
do
ELEMENT=($(head -n $LINECOUNTER $SAPATH/sa$I | tail -1))
case "${ELEMENT[4]}" in
[0-9]*)
let SUM[4]=${SUM[4]}+${ELEMENT[4]}
;;
*)
NEWLINE="${ELEMENT[@]}"
;;
esac
done
case ${ELEMENT[4]} in
[0-9]*)
let AVG[4]=${SUM[4]}/$DAYS
printf "%-8s %7d\n" ${ELEMENT[0]} ${AVG[4]}
;;
*)
echo "$NEWLINE"
;;
esac
let LINECOUNTER=LINECOUNTER+1
done < "sar_template2"
cp6 result:
00:05:00 77
00:10:00 152
00:15:01 227
00:20:00 303
00:25:00 380
00:30:00 456
00:35:00 533
00:40:00 610
00:45:00 687
00:50:00 764
00:55:00 841
01:00:00 918
01:05:00 993
01:10:01 1065
01:15:00 1135
01:20:00 1206
01:25:00 1276
01:30:00 1345
qgz result:
00:05:00 77
00:10:00 74
00:15:01 75
00:20:00 75
00:25:00 76
00:30:00 75
00:35:00 76
00:40:00 77
00:45:00 77
00:50:00 76
00:55:00 76
01:00:00 76
Thank You in advance !
| |
| Janis Papanagnou 2007-12-10, 7:20 pm |
| apogeusistemas@gmail.com wrote:
> Hi Unix Masters:
> Can You tell me why these 2 scripts show me non equal results ?
May I ask you what you haven't understood with the proposals that you
have been given before?
It makes no sense to analyse and compare your programs as long as you
didn't consider what has been suggested before.
Compare the effects of the following assignments...
SUM=(0,0,0,0)
SUM[0]: 0,0,0,0
SUM[1]:
SUM[2]:
SUM[3]:
SUM[4]:
SUM=(0 0 0 0)
SUM[0]: 0
SUM[1]: 0
SUM[2]: 0
SUM[3]: 0
SUM[4]:
Hope that helps.
Janis
> [big snip]
>
>
> Thank You in advance !
| |
| apogeusistemas@gmail.com 2007-12-10, 7:20 pm |
| On 10 dez, 22:07, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> apogeusiste...@gmail.com wrote:
>
> May I ask you what you haven't understood with the proposals that you
> have been given before?
>
> It makes no sense to analyse and compare your programs as long as you
> didn't consider what has been suggested before.
>
> Compare the effects of the following assignments...
>
> SUM=3D(0,0,0,0)
> SUM[0]: 0,0,0,0
> SUM[1]:
> SUM[2]:
> SUM[3]:
> SUM[4]:
>
> SUM=3D(0 0 0 0)
> SUM[0]: 0
> SUM[1]: 0
> SUM[2]: 0
> SUM[3]: 0
> SUM[4]:
>
> Hope that helps.
>
> Janis
>
>
>
>
>
> - Mostrar texto entre aspas -
I put this in the scritp:
while read dados
do
num=3D( 0 0 0 0 )
for dia in `echo $lav`
do
but I=B4m having this error:
=2E/cp6[14]: syntax error at line 14 : `(' unexpected
Please help me...and be patient...
| |
|
| On 11 Dez., 01:48, apogeusiste...@gmail.com wrote:
> On 10 dez, 22:07, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> I put this in the scritp:
>
> while read dados
> do
> num=3D( 0 0 0 0 )
> for dia in `echo $lav`
> do
>
> but I=B4m having this error:
>
> ./cp6[14]: syntax error at line 14 : `(' unexpected
>
> Please help me...and be patient...
I notice that you have your two programs interpreted by two different
shells, ksh and bash. Please clarify which of the two shells you
intend to use, and it would be helpful to know which of the two posted
output data is the desired one.
You may want to change your program(s) in a way that they will be
correct in both shells, by using only POSIX features. For the array
you can use the positional parameters...
set - 0 0 0 0
and work on $1 $2 $3 $4, or, since you seem to just access a single
array element anyway, just use a fixed variable (like sum4).
I want to bring to your attention, once again, that array elements
start with index 0 - this is different to using positional parameters
that are accessed as I showed above startinf wit $1.
You have a lot more quirks in your program we should talk about. But
first we should focus on _one_ shell and _one_ of your solutions.
Janis
| |
| apogeusistemas@gmail.com 2007-12-11, 1:24 pm |
| On 11 dez, 06:54, Janis <janis_papanag...@hotmail.com> wrote:
> On 11 Dez., 01:48, apogeusiste...@gmail.com wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> I notice that you have your two programs interpreted by two different
> shells, ksh and bash. Please clarify which of the two shells you
> intend to use, and it would be helpful to know which of the two posted
> output data is the desired one.
>
> You may want to change your program(s) in a way that they will be
> correct in both shells, by using only POSIX features. For the array
> you can use the positional parameters...
>
> set - 0 0 0 0
>
> and work on $1 $2 $3 $4, or, since you seem to just access a single
> array element anyway, just use a fixed variable (like sum4).
>
> I want to bring to your attention, once again, that array elements
> start with index 0 - this is different to using positional parameters
> that are accessed as I showed above startinf wit $1.
>
> You have a lot more quirks in your program we should talk about. But
> first we should focus on _one_ shell and _one_ of your solutions.
>
> Janis- Ocultar texto entre aspas -
>
> - Mostrar texto entre aspas -
Thank You , Janis.
Reading your answer and reading a shell book I found
set -A array_name command:
while read dados
do
set -A num 0 0 0 0 0
for dia in `echo $lav`
Now KSH script works fine!
00:05:00 73
00:10:00 70
00:15:01 71
|
|
|
|
|