案例:C 中 switch 的参数过多错误 shell

Case: Too many arguments error for switch in C shell

是的,我正在使用 csh。不,我别无选择。

我目前正在尝试使用今天的日期计算昨天的日期。 但是,当今天是一个月的第一天时,我用来尝试设置“天”号的开关会抛出错误 Case: Too many arguments
这是我的代码片段:

#!/bin/csh


set todaysMonth = `date '+%m'`
set todaysDay = `date '+%d'`
set todaysYear = `date '+%Y'`
set yestMonth
set yestDay
set yestYear

set todaysMonth = 1
set todaysDay = 1

if ($todaysDay == 1) then
   switch ($todaysMonth)
     case 5:
     case 7:
     case 10:
     case 12: set yestDay = 30
              breaksw
     case 1:
     case 2:
     case 4:
     case 6:
     case 8:
     case 9:
     case 11: set yestDay = 31
              breaksw

     case 3:  # don't care about leap year right now
              echo "Setting yestDay to 28"
              set yestDay = 28
              breaksw
     default: echo "Failied to set yestDay"
   endsw

echo "Today:" $todaysDay
echo "Yesterday:" $yestDay
endif

输出:Case: Too many arguments

有人知道这是怎么回事吗?

不是:

case <arg>: <command>

是:

case <arg>:
   <command>

手册页确实显示了这一点,但它并没有告诉您如果不这样做就会出错。

您需要在 4 行上执行此操作。

你需要一个 endif.