2

嘿伙计们,我有一个奇怪的错误,我不知道如何修复它,如果你能提供帮助将非常高兴。

#!/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

基本上,程序所做的是接收 2 个参数,例如Sep-22-07 11:45am 并以这种格式返回它yyyymmddhhmm-200709071145

现在,当我发送开头带有两个零的第二个参数时,会发生奇怪的事情,例如……例如上午 00:01。然后整个设定的月份,设定的日期,设定的年份开始疯狂并返回我的输出:

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

+ 另一个输出。

程序中还有其他的回声,不过我只是用来调试的。

无论如何,提前谢谢。我是 CShell 的新手,所以如果这是一个简单的错误,我很抱歉,但我似乎找不到它。

4

2 回答 2

2

tcsh 中的命令set time(不带参数)用于启用 shell 执行的每个命令的计时。这就是您看到的额外输出的来源。显然

set time = 00:01am

或类似的具有相同的效果 - 我不知道为什么前导“00:”会有所不同,但它很容易在命令行中重现。

既然你提到你是 C shell 的新手,我觉得有点不得不将你引向 Tom Christiansen 的这篇文章:

Csh 编程被认为是有害的

许多程序员更喜欢使用 Bourne shell 或其衍生工具之一(ksh、bash...)来编写非交互式脚本,以避免上述文章中描述的许多问题。

于 2010-01-04T06:30:49.820 回答
0

设定时间 = x; 允许用户设置何时记录命令时间的阈值。如果此阈值设置得较低,它将对所有花费时间超过该阈值的命令进行计时。如果 x 设置为相对较高的数字,则仅对超过该阈值的那些进行计时,并产生您所看到的输出,因此这就是为什么您仅在将时间设置为 00: 数字时才看到它的原因。但是,它如何处理您正在使用的时间格式并将其转换为秒,这超出了我的理解。

于 2011-03-04T21:49:17.093 回答