The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How much can you improve network throughput with a high-end NIC? iBot UNIX and Linux RSS News 0 04-10-2008 08:40 AM
How to improve grep performance... pooga17 Shell Programming and Scripting 2 02-13-2008 04:34 AM
egrep is very slow : How to improve performance hidnana Shell Programming and Scripting 7 02-12-2008 04:13 AM
improve performance by using ls better than find Nicol UNIX for Advanced & Expert Users 3 03-05-2004 05:53 AM
Can I improve this script ??? Cameron Shell Programming and Scripting 11 10-22-2002 05:39 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-02-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,328
improve this?

Wrote this script to find the date x days before or after today. Is there any way that this script can be speeded up or otherwise improved?
Code:
#!/usr/bin/sh

check_done() {
        if [ $month -eq 1 -o $month -eq 3 -o $month -eq 5 -o $month -eq 7 -o $month -eq 8 -o $month -eq 10 -o $month -eq 12 ]
        then
                daysofmth=31
        elif [ $month -eq 2 ]
        then
                if [ `expr $year % 100` -eq 0 -a `expr $year % 400` -eq 0 ]
                then
                        daysofmth=29
                elif [ `expr $year % 100` -ne 0 -a `expr $year % 4` -eq 0 ]
                then
                        daysofmth=29
                else
                        daysofmth=28
                fi
        elif [ $month -eq 4 -o $month -eq 6 -o $month -eq 9 -o $month -eq 11 ]
        then
                daysofmth=30
        fi
        julday=`expr $julday - $daysofmth`
        if [ $julday -lt 0 ]
        then
                done=1
                julday=`expr $daysofmth + $julday`
        elif [ $julday -eq 0 ]
        then
                done=1
                julday=$daysofmth
        fi
}
#########  main script starts here
if [ $# -ne 1 ]
then
        echo "Usage: tonfro <delta>"
        exit 1
fi
julday=`expr \`date +%j\` + $1`
year=`date +%Y`
month=0
while [ $julday -le 0 ]
do
        year=`expr $year - 1`
        if [ `expr $year % 100` -eq 0 -a `expr $year % 400` -eq 0 ]
        then
                julday=`expr $julday + 366`
        elif [ `expr $year % 100` -ne 0 -a `expr $year % 4` -eq 0 ]
        then
                julday=`expr $julday + 366`
        else
                julday=`expr $julday + 365`
        fi
done

done=0

while [ $done -ne 1 ]
do
        month=`expr $month + 1`
        if [ $month -eq 13 ]
        then
                year=`expr $year + 1`
                month=1
        fi
        check_done
done

printf "%d-%.2d-%.2d\n" $year $month $julday
Reply With Quote
Forum Sponsor
  #2  
Old 08-02-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Here is a thought.

How is it different from

date --date="2 days ago"
date --date="2 days later"

and likewise...

Another thing. Isnt expr an external command ? Why not use $( ) construct, instead ?

For eg.

`expr $year % 100` changes to $(($year % 100))


Vino

Last edited by vino; 08-02-2005 at 10:58 PM.
Reply With Quote
  #3  
Old 08-02-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,328
It does exactly what those commands do. Just that we dont have GNU 'date' and aren't allowed to install that either. So I came up with this. Just that the extreme cases like a difference of 100000 days takes a lot of time. Here is the output of uname -a:
SunOS xxxxxxx 5.8 Generic_117350-24 sun4u sparc SUNW,Ultra-Enterprise

There is almost no load on the server (uptime reports 0.06...).

Here are some 'time' command outputs for my script.

# time ./tonfro.sh 100
2005-11-11

real 0m0.31s
user 0m0.08s
sys 0m0.20s

# time ./tonfro.sh 10000
2032-12-19

real 0m7.84s
user 0m2.49s
sys 0m4.81s

# time ./tonfro.sh 100000
2279-05-19

real 1m16.19s
user 0m23.08s
sys 0m47.55s

--EDIT--
Is $() valid in sh? I am not using ksh. Also, assuming it is valid, will it run any faster than expr?
--/EDIT

Last edited by blowtorch; 08-02-2005 at 11:04 PM.
Reply With Quote
  #4  
Old 08-02-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
I think you are looking for this.

From man sh

Code:
  Arithmetic Expansion
       Arithmetic expansion allows the evaluation of an arithmetic  expression
       and  the  substitution of the result.  The format for arithmetic expan-
       sion is:

              $((expression))

       The expression is treated as if it were within  double  quotes,  but  a
       double  quote  inside  the  parentheses  is not treated specially.  All
       tokens in the expression undergo parameter expansion, string expansion,
       command  substitution, and quote removal.  Arithmetic substitutions may
       be nested.

       The evaluation is performed according to the rules listed  below  under
       ARITHMETIC EVALUATION.  If expression is invalid, bash prints a message
       indicating failure and no substitution occurs.
Reply With Quote
  #5  
Old 08-02-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
How about combining the first two conditions into 1 ?

Code:
                if [ `expr $year % 100` -eq 0 -a `expr $year % 400` -eq 0 ]
                then
                        daysofmth=29
                elif [ `expr $year % 100` -ne 0 -a `expr $year % 4` -eq 0 ]
                then
                        daysofmth=29
                else
                        daysofmth=28
                fi
to
Code:
                if [ (($(($year % 100)) -eq 0 -a $(($year % 400)) -eq 0)) -o \
                (($(($year % 100)) -ne 0 -a $(($year % 4)) -eq 0)) ]
                then
                        daysofmth=29
                else
                        daysofmth=28
                fi
Haven't tested it tho'.

Aided by man sh

Code:
       ((expression))
              The expression is evaluated according  to  the  rules  described
              below  under ARITHMETIC EVALUATION.  If the value of the expres-
              sion is non-zero, the return status is 0; otherwise  the  return
              status is 1.  This is exactly equivalent to let "expression".
Vino
Reply With Quote
  #6  
Old 08-02-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,328
Sorry vino, the sh man page on my system does not show anything about Arithmetic Expansion. May be its not supported on SunOS 5.8?
Reply With Quote
  #7  
Old 08-02-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Very much possible !

I am on a GNU based system.

Linux xxxxx 2.4.21-27.ELsmp #1 SMP Wed Dec 1 21:59:02 EST 2004 i686 i686 i386 GNU/Linux

Use ksh as your shell, and I think performance will improve.

Vino
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0