improve this?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting improve this?
# 1  
Old 08-03-2005
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

# 2  
Old 08-03-2005
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-03-2005 at 02:58 AM..
# 3  
Old 08-03-2005
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-03-2005 at 03:04 AM..
# 4  
Old 08-03-2005
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.

# 5  
Old 08-03-2005
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
# 6  
Old 08-03-2005
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?
# 7  
Old 08-03-2005
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Noob trying to improve

Hi everyone! This is my first post here, I hope that I will not already be violating any rule! I also would like to apologize in advance as my post will definitely be a noob post... please have patience and faith :rolleyes:! Now that I have set the ground rules :D:D, my objective is trying... (39 Replies)
Discussion started by: Ardzii
39 Replies

2. Shell Programming and Scripting

Improve script

Gents, Is there the possibility to improve this script to be able to have same output information. I did this script, but I believe there is a very short code to get same output here my script awk -F, '{if($10>0 && $10<=15) print $6}' tmp1 | sort -k1n | awk '{a++} END { for (n in a )... (23 Replies)
Discussion started by: jiam912
23 Replies

3. Programming

Improve the performance of my C++ code

Hello, Attached is my very simple C++ code to remove any substrings (DNA sequence) of each other, i.e. any redundant sequence is removed to get unique sequences. Similar to sort | uniq command except there is reverse-complementary for DNA sequence. The program runs well with small dataset, but... (11 Replies)
Discussion started by: yifangt
11 Replies

4. Shell Programming and Scripting

How to improve an script?

Gents. I have 2 different scripts for the same purpose: raw2csv_1 Script raw2csv_1 finish the process in less that 1 minute raw2csv_2 Script raw2csv_2 finish the process in more that 6 minutes. Can you please check if there is any option to improve the raw2csv_2. To finish the job... (4 Replies)
Discussion started by: jiam912
4 Replies

5. Shell Programming and Scripting

Improve sftp script

Dear all, I have written two scripts to transfer files to another server outside the company. One is a batch script , and the other script calls the batch script, send the files and archive the file sent. The problem is, that I want to get the list of files which have been uploaded the the... (10 Replies)
Discussion started by: arrals_vl
10 Replies

6. UNIX for Dummies Questions & Answers

How to improve the performance of this script?

Hi , i wrote a script to convert dates to the formate i want .it works fine but the conversion is tkaing lot of time . Can some one help me tweek this script #!/bin/bash file=$1 ofile=$2 cp $file $ofile mydates=$(grep -Po '+/+/+' $ofile) # gets 8/1/13 mydates=$(echo "$mydates" | sort |... (5 Replies)
Discussion started by: vikatakavi
5 Replies

7. AIX

improve sulog

I just wrote a very small script that improves readability on system sulog. The problem with all sulog is there is lack of clarity whether the info you are looking at is the most current. So if you just need a simple soution instead of going thru the trouble of writing a script that rotate logs and... (0 Replies)
Discussion started by: sparcguy
0 Replies

8. IP Networking

How to improve throughput?

I have a 10Gbps network link connecting two machines A and B. I want to transfer 20GB data from A to B using TCP. With default setting, I can use 50% bandwidth. How to improve the throughput? Is there any way to make throughput as close to 10Gbps as possible? thanks~ :) (3 Replies)
Discussion started by: andrewust
3 Replies

9. UNIX for Dummies Questions & Answers

Improve Performance

hi someone tell me which ways i can improve disk I/O and system process performance.kindly refer some commands so i can do it on my test machine.thanks, Mazhar (2 Replies)
Discussion started by: mazhar99
2 Replies

10. Shell Programming and Scripting

Can I improve this script ???

Hi all, Still a newbie and learning as I go ... as you do :) Have created this script to report on disc usage and I've just included the ChkSpace function this morning. It's the first time I've read a file (line-by-bloody-line) and would like to know if I can improve this script ? FYI - I... (11 Replies)
Discussion started by: Cameron
11 Replies
Login or Register to Ask a Question