请教Shell下的一个函数问题

Sanm_Lee 2015-08-18 10:26:50
Shell脚本如下:#!/bin/bash
count()
{

if [ $# -ne 3 ]

then

echo "The number of arguments is not 3!"
fi



let "s = 0"

case $2 in

+)

let "s = $1 + $3"

echo "$1 + $3 = $s";;

-)

let "s = $1 - $3"

echo "$1 - $3 = $s";;

\*)

let "s = $1 * $3"

echo "$1 * $3 = $s";;

\/)

let "s = $1 / $3"

echo "$1 / $3 = $s";;

*)

echo "What you input is wrong!";;

esac
}

echo "Please type you word:(e.g. 1 + 1)"
read a b c
count $a $b $c


脚本测试如下:

1.测试加法

[root@localhost 13study]# sh function5.sh

Please type you word:(e.g. 1 + 1)

3 + 3

3 + 3 = 6

2.测试减法

[root@localhost 13study]# sh function5.sh

Please type you word:(e.g. 1 + 1)

3 - 3

3 - 3 = 0

3.测试乘法

[root@localhost 13study]# sh function5.sh

Please type you word:(e.g. 1 + 1)

3 * 3

The number of arguments is not 3!

What you input is wrong!

4.测试除法

[root@localhost 13study]# sh function5.sh

Please type you word:(e.g. 1 + 1)

3 / 3

3 / 3 = 1
为什么乘法测试结果不对,问题出在哪里?是因为“*”星号符么,要如何操作才正确。请高手指点下。谢谢,谢谢
...全文
141 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_29937191 2015-12-22
  • 打赏
  • 举报
回复
来看看,学习下
nswcfd 2015-08-19
  • 打赏
  • 举报
回复
a="*" echo $a <----- 显示当前目录的所有文件名 echo "$a" <----- 显示*字符 调用count的时候,三个参数分别用引号括起来。
  • 打赏
  • 举报
回复
调用count前把 a b c都echo看看

742

社区成员

发帖
与我相关
我的任务
社区描述
该论坛主要探讨Linux系统在IBM Power平台的安装、部署、应用开发等话题,并为网友们提供自由交流的平台。
社区管理员
  • Power Linux社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧