Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Minimise my script pls, help...

Status
Not open for further replies.

hendnov

Technical User
Feb 27, 2006
73
AU
Hi guys,

Do you have any idea how to minimise my script :
Code:
#!/bin/ksh

if [ "$#" != "2" ]
then
	echo "usage : sumdisc [data from which vendor "airbus" or "boeing"] [path directory]"
	exit 1
fi

if [$1 = "airbus"]
then
cd $2 
du -sk */* | awk '{
        if ($2 ~ /SGML/){
                sum += $1
                print $0
        }

        if (($2 ~ /TIFF/)||($2 ~ /CGM/)){
                jumlah +=$1
                a[i++]=$0
        }

}
END {
        total = 30*sum
        semua = total + jumlah
        printf "TOTAL SGML FILES IS = %s \n SPACE FOR SGML : 30 * %s = %s \n\n", sum, sum, total
        for(j=0;j<i;++j)print a[j]
        printf "TOTAL GRAPHIC FILES = %s \n TOTAL SPACE REQUIRED = %s \n", jumlah, semua
}'
fi

if [$1 = "boeing"]
then
cd $2 
du -sk */* | awk '{
        if ($2 ~ /SGML/){
                sum += $1
                print $0
        }

        if (($2 ~ /TIFF/)||($2 ~ /CGM/)){
                jumlah +=$1
                a[i++]=$0
        }

}
END {
        total = 6*sum
        semua = total + jumlah
        printf "TOTAL SGML FILES IS = %s \n SPACE FOR SGML : 6 * %s = %s \n\n", sum, sum, total
        for(j=0;j<i;++j)print a[j]
        printf "TOTAL GRAPHIC FILES = %s \n TOTAL SPACE REQUIRED = %s \n", jumlah, semua
}'
fi

I'm sure It could be minimised, but I don't have a clue yet.
Thx Guys
 
have a look at the -v command line option.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
> Do you have any idea how to minimise my script :
What do you want to minimise?
- the time it takes to run
- the number of processes it runs
- through obfuscation, the number of people that could maintain it

Oh I see, it's the fact that both options end up running similar AWK code.

Here's the help for my AWK, check to see if yours supports the -v
Code:
$ awk -W help
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:          GNU long options:
        -f progfile             --file=progfile
        -F fs                   --field-separator=fs
        -v var=val              --assign=var=val

So by comparison, I see these differences
Code:
14c14
<         total = 30*sum
---
>         total = 6*sum
16c16
<         printf "TOTAL SGML FILES IS = %s \n SPACE FOR SGML : 30 * %s = %s \n\n", sum, sum, total
---
>         printf "TOTAL SGML FILES IS = %s \n SPACE FOR SGML : 6 * %s = %s \n\n", sum, sum, total

Then perhaps you could do something along these lines
Code:
# it's only this line you put in your if statements
thisScale=30

Code:
# this gets run all the time, once you've set
# a scale to 30 or 6 as appropriate
du -sk */* | awk -v scale=$thisScale '{
        if ($2 ~ /SGML/){
                sum += $1
                print $0
        }

        if (($2 ~ /TIFF/)||($2 ~ /CGM/)){
                jumlah +=$1
                a[i++]=$0
        }

}
END {
        total = scale*sum
        semua = total + jumlah
        printf "TOTAL SGML FILES IS = %s \n SPACE FOR SGML : %d * %s = %s \n\n", sum, scale, sum, total
        for(j=0;j<i;++j)print a[j]
        printf "TOTAL GRAPHIC FILES = %s \n TOTAL SPACE REQUIRED = %s \n", jumlah, semua
}'

--
 
Hi Salem...

Code:
du -sk */* | awk -v scale=$thisScale '{ ...

this line is not working...

Could you pls give me the answer ?

THX
 
Perhaps you have a very old awk, which does not support the -v command line option.

If you're on a Sun machine, try using 'nawk' rather than 'awk'.


--
 
Here's the error command :

awk: syntax error near line 1
awk: bailing out near line 1

I'll try using nawk.

 
Thx Salem,

you're the man, have a star :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top