LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1029|回复: 5

switch

[复制链接]
发表于 2003-5-18 12:01:26 | 显示全部楼层 |阅读模式
我在入门教程里搜索了switch的用法.写了下面的程序.
#! /bin/bash
if [ $# -ne 2 ]
then
echo You must input the month and years
else
switch ($1)
        case "January":
                cal 1 $2
        case "February":
                cal 2 $2
        case "March":
                cal 3 $2
        case "April":
                cal 4 $2
        case "May":
                cal 5 $2
        case "June":
                cal 6 $2
        case "July":
                cal 7 $2
        case "August":
                cal 8 $2
        case "September":
                cal 9 $2
"mycal" 33L, 478C written
[st1866@localhost st1866]$ mycal May 2002
/home/st18xx/st1866/mycal: line 6: syntax error near unexpected token
/home/st18xx/st1866/mycal: line 6: `switch ($1)'
[st1866@localhost st1866]$ cat mycal
#! /bin/bash
if [ $# -ne 2 ]
then
echo You must input the month and years
else
switch ($1)
        case "January":
                cal 1 $2
        case "February":
                cal 2 $2
        case "March":
                cal 3 $2
        case "April":
                cal 4 $2
        case "May":
                cal 5 $2
        case "June":
                cal 6 $2
        case "July":
                cal 7 $2
        case "August":
                cal 8 $2
        case "September":
                cal 9 $2
        case "October":
                 cal 10 $2
        case "November":
                cal 11 $2
        case "December":
                cal 12 $2
        default:
        echo "lease input right month!
fi
为什么运行后会出现:
/home/st18xx/st1866/mycal: line 6: syntax error near unexpected token
/home/st18xx/st1866/mycal: line 6: `switch ($1)'
发表于 2003-5-18 13:26:12 | 显示全部楼层
switch (string) 允许在几条替换命令中选择,string为不同的模式
case pattern:
commands
breadsw
default:
commands
endsw

而你的是#!/bin/bash
 楼主| 发表于 2003-5-18 16:19:31 | 显示全部楼层
switch不能在bash里用吗?
但我改用了csh.还是在同样的地方提示错误啊.
breadsw
endsw需要加上去吗?我有很多个case.要把breadsw加到哪个case的command下?
发表于 2003-5-18 16:46:44 | 显示全部楼层
如果细心读一下文档,应该不会被这类问题困扰。
遇到问题首先应该多想一想,查查文档,尽可能靠自己去解决,只有这样才能更快的提高。如果指望一帆风顺或一遇到问题就有个高手马上帮你解决,那GNU/Linux玩起来也太没意思了;)
 楼主| 发表于 2003-5-18 22:29:33 | 显示全部楼层
你说得对.我的确是没有读清楚文档.
我一起是用ctrl+F查需要的信息.
发表于 2003-5-19 08:50:19 | 显示全部楼层
在man下搜索的方法是:/搜索字串
n可以往下继续搜索;
N可以往上继续搜索。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表