tags:

views:

53

answers:

1

Hey fellas, I have this weird bug, and I have no clue how to fix it, would be very glad if you could help.

#!/bin/tcsh -f
set date = ${1}
set time = ${2}
echo 1
set month = `echo $date | cut -f1 -d"-"`
set day = `echo $date | cut -f2 -d"-"`
set year = `echo $date | cut -f3 -d"-"`
echo $date $month $day $year
echo $date $time
if ($year > 69) then
    @ year = $year + 1900
else
    @ year = $year + 2000
endif   
echo 3
if ($month == "Jan") set month = 01
if ($month == "Feb") set month = 02
if ($month == "Mar") set month = 03
if ($month == "Apr") set month = 04
if ($month == "May") set month = 05
if ($month == "Jun") set month = 06
if ($month == "Jul") set month = 07
if ($month == "Aug") set month = 08
if ($month == "Sep") set month = 09
if ($month == "Oct") set month = 10
if ($month == "Nov") set month = 11
if ($month == "Dec") set month = 12
echo 4
set hour1 = `echo $time | cut -c1`
set hour2 = `echo $time | cut -c2`
set min1 = `echo $time | cut -c4`
set min2 = `echo $time | cut -c5`
set ampm = `echo $time | cut -c6`
echo $hour1 #$hour2 $min1 $min2

if ($ampm =~ [pP]) then
    @ hour1 = $hour1 + 1
    @ hour2 = $hour2 + 2
endif

if ($day < 10) then
    printf "%s%s0%s%s%s%s%s" $year $month $day $hour1 $hour2 $min1 $min2
else
    printf "%s%s%s%s%s%s%s" $year $month $day $hour1 $hour2 $min1 $min2
endif

Basically, what the program does is receives 2 arguments, for example Sep-22-07 11:45am and returns it in this format yyyymmddhhmm - 200709071145

Now the weird thing happens when I send the 2nd parameter with two zeros at the beginning, like that ... 00:01am, for example. then the whole set month, set day, set year starts to freak and returns me that output:

0.000u 0.001s 0:00.00 0.0%      0+0k 0+0io 0pf+0w
0.000u 0.001s 0:00.00 0.0%      0+0k 0+8io 0pf+0w
0.000u 0.001s 0:00.00 0.0%      0+0k 0+0io 0pf+0w

+ the other output.

There are in the program other echos, but I just used them to debug.

Anyway, thanks in advance. I am kind of a newbie in CShell, so if that's some easy bug I am sorry, but I can't seem to find it.

+2  A: 

The command set time (with no arguments) in tcsh is used to enable timing each command executed by the shell. That's the source of the extra output you're seeing. Apparently

set time = 00:01am

or similar has the same effect -- I don't know why the leading "00:" makes a difference, but it's easily reproducible at the command line.

Since you mention you're a newbie at C shell, I feel somewhat compelled to direct you to this article by Tom Christiansen :

Csh Programming Considered Harmful

Many programmers prefer using Bourne shell or one of its derivatives (ksh, bash...) for non-interactive scripting, to avoid many of the problems described in the above article.

Jim Lewis
Wow, thanks man, you have no idea how grateful I am, I have wasted hours on this, thanks.
GMan