简单的 CShell 程序中发生奇怪的事情

发布于 2024-08-16 21:08:45 字数 1800 浏览 4 评论 0原文

嘿伙计们,我有一个奇怪的错误,我不知道如何修复它,如果你能帮忙,我会很高兴。

#!/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:01am,例如。然后整个设定的月份、设定的日期、设定的年份开始奇怪并返回给我那个输出:

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 的新手,所以如果这是一些简单的错误,我很抱歉,但我似乎找不到它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你如我软肋 2024-08-23 21:08:45

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

set time = 00:01am

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

既然你提到你是 C shell 的新手,我觉得有必要引导你阅读 Tom Christiansen 的这篇文章:

Csh 编程被认为是有害的

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

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.

总攻大人 2024-08-23 21:08:45


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

the
set time = x;
allows users to set a threshold for when to log time of commands. If this threshold is set low, it will time all commands that take longer than that threshold. If x is set to a relatively high number, only those which exceed that threshold are timed, and produce the output that you're seeing, hence that is why you only see it when you're setting time to a 00: number. How it deals with they time format you're using and translating that to seconds, however, is beyond my understanding of this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文