Weird script behaviour !


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Weird script behaviour !
# 1  
Old 01-26-2010
Weird script behaviour !

Hello,

I am getting an infinite loop from a script in Linux.
Here is the last version of the script in question. As you can see I tried to define everything properly:

Code:
#!/bin/ksh

# Script to loop over a series of dates 

set -ex

typeset -i start_date=20090701
typeset -i end_date=20090720

typeset -i current_date=${start_date}

### *PROBLEMATIC LINE BELOW * Remove to test without !!!

# (We increment the starting day by one so that the conditional loop
#  below will be an "=="
end_date=$(date --date="$end_date 1 day" +%Y%m%d)

until [ $current_date == $end_date ]
do
  
  echo "current date = $current_date "
  ### Calculations ####
  
  # Increase the date by one day 
  current_date=$(date --date="$current_date 1 day" +%Y%m%d)

done

The problem is simple: I want to loop over a series of dates: start_date and end_date.
I wanted to make it "nice" and intuitive and include the end_date in the calculations. If we remove the line:
Code:
 
end_date=$(date --date="$end_date 1 day" +%Y%m%d)

then the scripts works fine - it just obviously stops at a date before the end_date.

Problem is:
If I remove the line that increments the end_date then all is well and the script loops till end_date MINUS one day (obviously). By leaving the line there the scripts loops infinitely ! The current_date does increase but the loop's condition is never executed.

I sense that since the variable values do not affect the condition ( == ), then it probably has something to do with the variable type ???

I also tested with the variables not defined as integers but as strings.

IMPORTANT: The script works and I am not asking for an alternative way (would be nice to see a different approach but it's not necessary).
So, if you have any suggestions I want to understand what i am doing wrong with the *current* script and not a different way of doing this! Smilie

Thanks!

PS: I am a newbie but the fact that there are a few ways to define the same things in Korn , Bourne etc, begins to annoy me. I want to learn the proper syntax but it's difficult looking at other peoples code at work and on the net. Script synax and various shells seem to be permissive which makes me pick-up "bad habits" ...

---------- Post updated at 11:53 AM ---------- Previous update was at 10:57 AM ----------

Never mind !... I feel embarrassed.

I simplified the listeing here but in real life I had a second fixed nested llop. Inside that loop i was incrementing the date so it would basically "skip" an increement and the == would never apply.

Thanks anyways and my apologies!....

-S
# 2  
Old 01-26-2010
I would have used -lt instead of ==, so that if it somehow skipped past it would still break the loop.
# 3  
Old 01-26-2010
Absolutely ! Among a whole bunch of other improvements. But the point was for me to understand. Well I did ..... I found a silly logical bug.

Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Strange Script behaviour with Grep

Here is my script LOGDATE=`date '+%Y-%m-%d %k:%M' | cut -c1-15` echo $LOGDATE echo "grep '$LOGDATE' /tmp/logs/vel.log >10min_log" grep '$LOGDATE' /tmp/logs/vel.log>10min_log grep '$LOGDATE' /tmp/logs/vel.logHere is the output of the script -rw-r--r-- 1 wluser wluser 0 May 3... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. Shell Programming and Scripting

Weird redirection behaviour

Linux. Bash 4.2.10. Let's say that i want the stderr of a command redirected to one file (err), and both stdout and stderr redirected to another file (outanderr). Easy. Many ways to do it. But may times the file outanderr will be messed up (with the output and the error mixed toghether). ... (3 Replies)
Discussion started by: Lem
3 Replies

3. Ubuntu

Weird rm behaviour

I am little bit confused by the behaviour of rm in Ubuntu. It seems that as a regular user I can delete files owned by another user even when the permissions are set to 644. Here is an example: cjohnson@carbon:~/test$ sudo touch testfile cjohnson@carbon:~/test$ ls -al total 8 drwxr-xr-x... (2 Replies)
Discussion started by: ccj4467
2 Replies

4. Shell Programming and Scripting

find: "weird" regex behaviour

I have these two files in current dir: oos.txt oos_(copy).txt I execute this find command:find . -regex './oos*.txt'And this outputs only the first file (oos.txt)! :confused: Only if I add another asterisk to the find find . -regex './oos*.*txt' do I also get the second file... (7 Replies)
Discussion started by: courteous
7 Replies

5. Shell Programming and Scripting

Expect script strange behaviour

Hi people, I'm having some strange behaviour with an 'expect' script. spawn csession blah expect "Username: " send "userblah\r" expect "Password: " send "passwordblah\r" interact When I execute the script as root it runs perfectly. However, when executed as any other... (0 Replies)
Discussion started by: GarciasMuffin
0 Replies

6. Shell Programming and Scripting

Weird sed behaviour in script

I've written a small script to replace certain words in all the the files in a directory. #!/bin/sh #Get list of files to be edited file_list=`ls -p` for i in $file_list do echo "Processing $i" alteredi=`echo "$i" | sed -e 's/\//d/'` if then if then #actual altering (2 Replies)
Discussion started by: Peetrus
2 Replies

7. Shell Programming and Scripting

Strange behaviour from script in crontab

Apologies if this has been mentioned elsewhere, my search skills may be lacking somewhat today. I have a script that does the following (as a test): find . -name "*.txt" -exec file {} \; >>$sFullFilePath Now, the variable is set up up correctly in the script too. When I run the script... (1 Reply)
Discussion started by: PilotGoose
1 Replies

8. Shell Programming and Scripting

Help with my weird script!

So I have this script titled "testing.sh" #!/bin/ksh #PROGRAM INITIALIZATION HomeDir=/home/sap/gl/ftp server=testftp01 userid=ftp_uatollmsgbus password="f&p53715" MSGLOG=${HomeDir}/msglog.txt FTPLogTmp=${HomeDir}/testing.tmp FTPLogFile=${HomeDir}/testing.log... (1 Reply)
Discussion started by: kdyzsa
1 Replies

9. UNIX for Advanced & Expert Users

Weird sudo behaviour

Hi gurus. I implemented sudo and have the following in my sudo config file *************** # User alias specification User_Alias VENDOR = user1 # User privilege specification VENDOR ALL = NOPASSWD: /bin/, /sbin/, /usr/local/bin/, \ !/bin/su,... (1 Reply)
Discussion started by: geomonap
1 Replies

10. UNIX for Dummies Questions & Answers

Weird script

I need a script to do the following and have no idea how to do it...can someone help? I need to start Sql*Plus, load a query, say "unmatched.sql", run the query, then load unmatched.sc and run it, then print the output file that unmatched.sc created... any help greatly appreciated. Duckman (2 Replies)
Discussion started by: Duckman
2 Replies
Login or Register to Ask a Question