shell脚本规范事项
1.脚本第一行加脚本解释器:#!/bin/bash 或 #!/bin/sh
2.若脚本中有中文,则需要在系统中加"export LANG="zh_CN.UTF-8"",并且在脚本中重新定义字符集,使其和系统中的字符集一致
3.shell脚本以.sh结尾,并且放到制定位置:例如"/server/scripts"
4.所以成对的符号,和循环语句的关键词,要一次性写完,防止遗漏
5.全局变量全部大写,局部变量可以全部小写或者使用驼峰语法进行书写,例如:"myBook"
6.函数命名可采用首字母大写,并且语义要清晰,例如:"createFile"
7.尽量在函数最后加上返回值,有些不会用到返回值的也一样
二、高级命名规范:
1.常规shell用.sh结尾:例如:shell.sh
2.模块的启动和停止脚本统一命名为start_模块名.sh和stop_模块名.sh
3.监控脚本一般以_mon.sh结尾
4.控制脚本一般以_ctl.sh为后缀
运行shell脚本的四种方式阿
-
方式一:绝对路径,需要当前用户对脚本文件有rx权限
[root@Centos7 ~]# /scripts/day02/hello.sh -bash: /scripts/day02/hello.sh: Permission denied [root@Centos7 ~]# ll !$ ll /scripts/day02/hello.sh -rw-r--r-- 1 root root 41 Aug 25 19:43 /scripts/day02/hello.sh [root@Centos7 ~]# chmod +x !$ chmod +x /scripts/day02/hello.sh [root@Centos7 ~]# /scripts/day02/hello.sh hello world
-
方式二:./脚本文件.sh,需要当前用户对脚本文件有rx权限
[root@Centos7 day02]# chmod o=x hello.sh [root@Centos7 day02]# ll hello.sh -rwxr-x--x 1 root root 41 Aug 25 19:43 hello.sh [root@Centos7 day02]# su - egon [egon@Centos7 ~]$ cd /scripts/day02/ [egon@Centos7 day02]$ ./hello.sh bash: ./hello.sh: Permission denied
-
方式三:指定解释器来解释执行脚本程序,需要当前用户对脚本文件有r权限
解释:我们执行的是bash命令,所有用户对bash命令都有执行权限,所以我们只需要考虑脚本文件的读权限即可
[root@Centos7 day02]# chmod o=- hello.sh #640 [root@Centos7 day02]# su - egon [egon@Centos7 ~]$ cd /scripts/day02/ [egon@Centos7 day02]$ ll -r-xr-x--- 1 root root 26 Aug 25 19:53 hello.sh [egon@Centos7 day02]$ bash hello.sh bash: hello.sh: Permission denied [egon@Centos7 day02]$ exit logout [root@Centos7 day02]# chmod o=x hello.sh #641 [root@Centos7 day02]# su - egon Last login: Tue Aug 25 19:53:31 CST 2020 on pts/0 [egon@Centos7 ~]$ !cd cd /scripts/day02/ [egon@Centos7 day02]$ bash hello.sh bash: hello.sh: Permission denied [egon@Centos7 day02]$ exit logout [root@Centos7 day02]# chmod o=r hello.sh #644 [root@Centos7 day02]# su - egon Last login: Tue Aug 25 19:54:07 CST 2020 on pts/0 [egon@Centos7 ~]$ !cd cd /scripts/day02/ [egon@Centos7 day02]$ bash hello.sh hello world [root@hass-11 test]# ll /usr/bin/sh lrwxrwxrwx. 1 root root 4 Jun 30 02:36 /usr/bin/sh -> bash
-
方式四:在当前bash进程中运行,需要当前用户对脚本文件有r权限
前三种方式都是在子bash进程中运行
[root@Centos7 ~]# echo $x [root@Centos7 ~]# source /scripts/day02/hello.sh hello world [root@Centos7 ~]# cat !$ cat /scripts/day02/hello.sh x=111 echo "hello world" [root@Centos7 ~]# echo $x 111 [root@Centos7 ~]# unset x [root@Centos7 ~]# [root@Centos7 ~]# . /scripts/day02/hello.sh hello world [root@Centos7 ~]# echo $x 111
shell脚本调试
1、使用dos2unix命令处理在Windows下开发的脚本
#!/bin/bash
i=1
sum=0
while((i<=100))
do
let i++
((sum+=i))
done
使用dos2unix进行格式化,会去除一些Windows的一些格式(例如空格)错误
---------------------------------------------------------------------------------------
2.bash命令调试
sh [-nvx] scripts.sh
-n :不会执行脚本,仅仅检查语法是否有问题,并且给出错误提示
-v :在执行脚本时,先将脚本的内容输出到屏幕上,然后执行脚本,如果有错误,也会给出错误提示
-x :不会执行脚本,将执行的脚本内容输出显示到屏幕上。
printf "totalsum is $sum"
export PS4='+${LINENO}' #此命令可以使追踪命令显示每行的行号
vim相关文件
~/.viminfo 用户使用vim的操作历史记录
~/.vimrc 当前用户的vim配置文件 #
/etc/vimrc 系统全局vim的配置文件
/usr/share/vim/vim74/colors 配色魔板文件存放路径
注:
1.当配置/etc/vimrc文件时,所有用户的vim均会受影响
2.当编辑个人用户家目录下的隐藏文件.vimrc,则只有此用户的vim编辑受影响。
配置某一用户的vim
编译之后,直接生效
[root@hass-11 ~]# vim ~/.vimrc
#兼容模式
set nocompatible
#设置最大历史数
set history=100
#检测文件类型
filetype on
#打开文件类型插件
filetype plugin on
#文件类型缩进
filetype indent on
#设置自动读取
set autoread
#设置鼠标
set mouse=a
#语法检测
syntax enable
set nofen
set fdl=0
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set smarttab
#自动缩进
set ai
set si
#自动换行
set wrap
set sw=4
set wildmenu
#显示行号
set nu
set cmdheight=1
set lz
set backspace=eol,start,indent
set whichwrap+=<,>,h,l
set magic
set noerrorbells
set novisualbell
set showmatch
set mat=2
#设置高亮
set hlsearch
set ignorecase
#设置编码
set encoding=utf-8
set fileencodings=utf-8
set termencoding=utf-8
#智能对齐
set smartindent
set cin
set showmatch
set guioptions-=T
set guioptions-=m
set vb t_vb=
set laststatus=2
set pastetoggle=<F9>
#设置背景=暗
set background=dark
#突出显示搜索ctermbg=黑色字符mfg=白色guifg=白色guibg=黑色
highlight Search ctermbg=black ctermfg=white guifg=white guibg=black
autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1, "#!/bin/bash")
call setline(2, "#Auther:itboy")
call setline(3, "#Time:".strftime("%F %T"))
call setline(4, "#Name:".expand("%"))
call setline(5, "#Version:V1.0")
call setline(6, "#Description:this is a test script.")
endif
endfunc
vim命令
ngg :调到n行
0 :到行开头
$ :到行结尾
L :移动到当前窗口最后一行 == G ,然后点"o",开始下一行编辑
H :移动到当前窗口最前面一行 ==gg
命令行模式下:"ctrl+:"后
/old :从上向下找"old"
%s/A/B/g :A全部替换为B "/"可以换成"#或@"
n1,n2 w filename : 将n1到n2行的内容保存到filename文件里
n1,n2 co n3 : 将n1到n2行的内容复制到n3的位置下
n1,n2 m n3 : 将n1到n2行的内容剪切到到n3的位置下
! “命令” :暂且退出vi,执行“命令”
vs filename : 竖直分屏显示filename ,q!退出分屏
1.多行注释:
1). 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式;
2). 在行首使用上下键选择需要注释的多行;
3). 按下键盘(大写)“I”键,进入插入模式;
4). 然后输入注释符(“//”、“#”等);
5). 最后按下“Esc”键。 注:在按下esc键后,会稍等一会才会出现注释,不要着急~~时间很短的
2.删除多行注释:
1). 首先按esc进入命令行模式下,按下Ctrl + v, 进入列模式;
2). 选定要取消注释的多行;
3). 按下“x”或者“d”. 注意:如果是“//”注释,那需要执行两次该操作,如果是“#”注释,一次即可
3.多行删除
1).首先在命令模式下,输入“:set nu”显示行号;
2).通过行号确定你要删除的行;
3).命令输入“:32,65d”,回车键,32-65行就被删除了,很快捷吧
如果无意中删除错了,可以使用‘u’键恢复(命令模式下)
也可以用"del"在可视化中全删选择的内容
4.多行缩进
1). 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式;
2). 在行首使用上下键选择需要缩进的多行;
3). 最后按"="即可
登录脚本
[root@hass-11 script]# vim login.txt
#!/bin/bash
db_username="syy"
db_password="123"
read -p "请输入用户: " username
read -p "请输入密码:" password
[ $username == $db_username -a $password == $db_password ] && echo 登录成功 || echo 登录失败
#&& 优先级高于 ||
[root@Centos7 day02]# chmod +x login.sh
[root@Centos7 day02]# ./login.sh
请输入您的账号: xxx
请输入您的密码: 123
登录失败
[root@Centos7 day02]# ./login.sh
请输入您的账号: syy
请输入您的密码: 123
登录成功
----------------------------------------------------------------------------------------
#!/bin/bash
db_username=syy
db_password=123
user=`who am i|awk '{print $1}'`
hostname=`hostname`
count=0
lj=`/root`
while true;do
read -p "请输入用户账号: " username
read -p "请输入用户密码: " password
((count++))
if [ $count -eq 3 ];then
echo "超出输入次数,程序退出!!!"
exit 0
fi
if [ "${username}" = ${db_username} -a "${password}" = ${db_password} ];then
while true;do
read -p "[${user}@${hostname} ${lj}]# " ml
if [ "$ml" = "exit" -o "$ml" = "q" -o "$ml" = "bash" ];then
break 2
else
$ml
lj=`pwd`
fi
done
else
echo "账号密码错误,请重新输入!!!"
fi
done
并发ping多个ip
[root@hass-11 script]# vim ip.sh
#!/bin/bash
for ip in $*
do
(ping -c1 $ip &>/dev/null && echo "$ip:up" >> ping_access.log || echo "$ip:down" >> ping_error.log) &
done
实现2位数加减乘除运算
#!/bin/bash
if [ $# -eq 0 ]; then
echo "输入值不能为空" //提示不能输入空值 注:$#代表环境变量个数
exit
fi
num=$* //定义一个全局变量,方便后边的调用
sum() {
sum=0
while [ $# -gt 0 ]
do
sum=$[ $sum+$1 ]
shift
done
echo "数字 $num 相加的和是 $sum"
}
cha() {
cha=$1
shift
while [ $# -gt 0 ]
do
cha=$[ $cha-$1 ]
shift
done
echo "数字 $num 相减的差是 $cha"
}
cheng() {
cheng=$1
shift
while [ $# -gt 0 ]
do
cheng=$[ $cheng*$1 ]
shift
done
echo "数字 $num 相乘的是 $cheng"
}
chu() {
chu=$1
shift
while [ $# -gt 0 ]
do
chu=$[ $chu/$1 ]
shift
done
echo "数字 $num 相除的商是 $chu"
}
read -p "请输入 + | - | * | / :" type
case "$type" in
"+") sum $* ;;
"-") cha $* ;;
"*")cheng $* ;;
"/") chu $* ;;
*) echo " 请输入{ +|-|*|/}"
esac
使用if语句
#!/bin/bash
# 这是一个计算器
read -t 30 -p "Please input the first number: " num1
read -t 30 -p "Please input the second number: " num2
read -t 30 -p "Please input the second operator("+", "-", "*", "/"): " op
if [ -n "$num1" -a -n "$num2" -a -n "$op" ]
then
# 1.校验两个操作数必须为数值
test1=$(echo $num1 | sed 's/[0-9]//g')
test2=$(echo $num2 | sed 's/[0-9]//g')
if [ -n "$test1" -o -n "$test2" ]
then
echo "Please input 2 number."
exit 1
fi
# 2.判断操作符是否正确
if [ "$op" == "+" ]
then
result=$(($num1 + $num2))
elif [ "$op" == "-" ]
then
result=$(($num1 - $num2))
elif [ "$op" == "*" ]
then
result=$(($num1 * $num2))
elif [ "$op" == "/" ]
then
result=$(($num1 / $num2))
else
echo "Please input correct operator, like "+", "-", "*", "/"."
exit 2
fi
# 3.打印结果
echo "$num1 $op $num2 = $result"
exit 0
else
echo "Number and oprator must not be empty"
exit 3
fi
使用case语句
#!/bin/bash
# 这是一个计算器
read -t 30 -p "Please input the first number: " num1
read -t 30 -p "Please input the second number: " num2
read -t 30 -p "Please input the second operator("+", "-", "*", "/"): " op
if [ -n "$num1" -a -n "$num2" -a -n "$op" ]
then
# 1.校验两个操作数必须为数值
test1=$(echo $num1 | sed 's/[0-9]//g')
test2=$(echo $num2 | sed 's/[0-9]//g')
if [ -n "$test1" -o -n "$test2" ]
then
echo "Please input number."
exit 1
fi
# 2.判断操作符是否正确
case $op in
"+")
result=$(($num1 + $num2))
;;
"-")
result=$(($num1 - $num2))
;;
"*")
result=$(($num1 * $num2))
;;
"/")
result=$(($num1 / $num2))
;;
*)
echo "Please input correct operator, like "+", "-", "*", "/"."
exit 2
;;
esac
# 3.打印结果
echo "$num1 $op $num2 = $result"
exit 0
else
echo "Number and oprator must not be empty"
exit 3
fi
检测硬盘根分区使用率
#!/bin/bash
disk_use=` df|grep "/$"|awk '{print $5}'|cut -d "%" -f 1`
if [ $disk_use -ge 90 ];then
echo "critical"
elif [ $disk_use -ge 70 ];then
echo "error"
elif [ $disk_use -ge 50 ];then
echo "warning"
else
echo "正常"
fi
系统硬件指标监控
#!/bin/bash
disk_use=`df|grep "/$"|awk '{print $5}'|cut -d "%" -f 1`
mem_free=`free|grep "Mem"|awk '{print $4}'`
mem_tatol=`free|grep "Mem"|awk '{print $2}'`
free_tatol=`echo "scale=2;$mem_free/$mem_tatol"|bc|cut -d . -f2`
if [ $disk_use gt 90 ];then
echo "硬盘根分区剩余率小于10%" | mail -s "`hostname`_`date +F%-M%-S%`_磁盘报警" 1757528181@qq.com
fi
if [ $free_tatol lt 10 ];then
echo "内存的剩余率小于10%" | mail -s "`hostname`_`date +F%-M%-S%`_内存报警" 1757528181@qq.com
fi
判断用户身份存在
[root@hass-11 ~]# vim yh.sh
#!/bin/bash
read -p "请输入要判断的用户:" yonghu
id $yonghu &>/dev/null
if [ $? -eq 0 ];then
echo "该用户存在"
else
echo "该用户不存在"
fi
检测端口
[root@hass-11 ~]# vim 80.sh
#!/bin/bash
netstat -lntup|grep "LISTEN"|grep "80" &>/dev/null
if [ $? -eq 0 ];then
echo "80端口启动正常"
else
systemctl start httpd &>/dev/null
if [ $? -eq 0 ];then
echo "80端口启动正常"
else
echo "80端口异常"
fi
fi
nginx的启动、停止、重启和查看状态脚本
该脚本可用来管理httpd
[root@hass-11 ~]# vim case.sh
#!/bin/bash
[ $# -eq 0 ] && exit 1
case $1 in
start)
netstat -lntup|grep "LISTEN"|grep "80" &>/dev/null
if [ $? == 0 ];then
echo "nginx已经启动"
else
systemctl start nginx
if [ $? == 0 ];then
echo "nginx已经启动"
else
echo "nginx启动异常"
fi
fi
;;
stop)
netstat -lntup|grep "LISTEN"|grep "80" &>/dev/null
if [ $? == 0 ];then
systemctl stop nginx
sleep 1
netstat -lntup|grep "LISTEN"|grep "80" &>/dev/null
if [ $? == 0 ];then
echo "nginx停止异常"
else
echo "nginx已经停止"
else
echo "nginx已经停止"
fi
;;
restart)
netstat -lntup|grep "LISTEN"|grep "80" &>/dev/null
if [ $? == 0 ];then
pkill nginx
sleep 1
systemctl start nginx
sleep 1
netstat -lntup|grep "LISTEN"|grep "80" &>/dev/null
if [ $? == 0 ];then
echo "nginx已经启动"
else
echo "nginx启动异常"
fi
else
systemctl start nginx
if [ $? == 0 ];then
echo "nginx已经启动"
else
echo "nginx启动异常"
fi
fi
;;
status)
systemctl status nginx
;;
*)
echo -e "语法错误,请输入正确的语法
usage: ${0} [start][stop][restart][status]"
;;
esac
猜字游戏
#!/bin/bash
read -p "请输入一个整数:" num
if [ -n "${num}" ];then
test=`echo ${num}|sed 's/[0-9]//g'`
if [ -n "$test" ];then
echo "只能输入纯数字哦~"
read -p "请输入一个整数:" num
exit 1
fi
if [ ${num} -gt 18 ];then
echo "猜大了"
elif [ ${num} -lt 18 ];then
echo "猜小了"
else
echo "猜对了"
fi
else
echo "没有输入,程序结束"
fi
#这里 -n 后面一定要加引号
猜拳游戏
#!/bin/bash
while true
do
read -p "来和我猜拳8: " quan
ran=$((RANDOM%2))
case $ran in
0)
num="石头"
echo -e "[电脑]: $num
[你]: $quan"
if [ "$quan" = "剪刀" ];then
echo "你输了"
elif [ "$quan" = "石头" ];then
echo "重来"
elif [ "$quan" = "布" ];then
echo "你赢了"
else
echo "输入错误"
fi
;;
1)
num="剪刀"
echo -e "[电脑]: $num
[你]: $quan"
if [ "$quan" = "布" ];then
echo "你输了"
elif [ "$quan" = "剪刀" ];then
echo "重来"
elif [ "$quan" = "石头" ];then
echo "你赢了"
else
echo "输入错误"
fi
;;
2)
num="布"
echo -e "[电脑]: $num
[你]: $quan"
if [ "$quan" = "石头" ];then
echo "你输了"
elif [ "$quan" = "布" ];then
echo "重来"
elif [ "$quan" = "剪刀" ];then
echo "你赢了"
else
echo "输入错误"
fi
;;
esac
done
#在匹配字符串时用了类似这样的语句
if[ $timeofday = "yes"]; then;echo "Good morning";exit 0
如果变量timeofday的值为空,那么就if语句就变成了if [ ="yes" ],这不是一个合法的条件。为了避免出现这种情况,我们必须给变量加上引号if [ "$timeofdat"="yes" ],这样即使是空变量也提供了合法的测试条件,,if [ " "="yes" ]
rsync管理脚本
#!/bin/bash
#限制参数个数
if [ $# -ne 1 ]
then
echo $"usage:$0 {start|stop|restart}"
exit 1
fi
#参数类型
if [ "$1" == "start" ];then
rsync --daemon
sleep 2 #休息2秒,这点很重要,停止或启动后建议休息2秒后在判断
if [ $(netstat -pantu | grep rsync | wc -l) -ne 0 ];then #如果过滤的rsync不是0,则说明启动了
echo "rsync is started"
exit 0
else
systemctl start rsync
if [ $(netstat -pantu | grep rsync | wc -l) -ne 0 ];then
echo "rsync is started"
exit 0
else
echo "rstnc 启动异常"
fi
elif [ "$1" == "stop" ];then
pkill rsync &>/dev/null #停止服务的方法很多,自选即可
sleep 2
if [ $(netstat -pantu | grep rsync | wc -l) -eq 0 ];then
echo "rsync is stopped"
exit 0
fi
elif [ "$1" == "restart" ];then
killall rsync
sleep 1
postKill=$(netstat -pantu | grep rsync | wc -l) #关闭前和启动后都有对应的标识字符
systemctl start rsyncd
sleep 1
postStart=$(netstat -pantu | grep rsync | wc -l)
if [ "$postKill" -eq 0 -a "$postStart" -ne 0 ];then
echo "rsync is restarted"
exit 0
fi
else
echo $"usage:$0 {start|stop|restart}" #若没有按照要求输入参数,则提示后退出脚本
exit 1
fi
----------------------------------或者---------------------------------------------------------
#/bin/bash
#chkconfig:2345 21 81
#description
if [ $# -ne 1 ];then
echo $"usage:$0 {start|stop|restart}"
exit 1
fi
case "$1" in
"start")
rsync --daemon
sleep 2
if [ $(netstat -pantu | grep rsync | wc -l) -ge 1 ];then
echo "rsync is started"
exit 0
else
systemctl start rsync
if [ $(netstat -pantu | grep rsync | wc -l) -ne 0 ];then
echo "rsync is started"
exit 0
else
echo "rstnc 启动异常"
fi
;;
"stop")
killall rsync &>/dev/null
sleep 2
if [ $(netstat -pantu | grep rsync | wc -l ) -eq 0 ];then
echo "rsyncd is stopped"
exit 0
fi
;;
"restart")
killall rsync &>/dev/null
sleep 1
postKill=$(netstat -pantu | grep rsync| wc -l)
systemctl start rsyncd
sleep 1
postStart=$(netstat -pantu | grep rsync | wc -l)
if [ $postKill -eq 0 -a $postStart -ne 0 ];then
echo "rsyncd is restarted"
exit 0
fi
;;
*)
echo $"usage:$0 {start| stop restart}"
exit 1
;;
esac
注:
1.修改权限:
chmod +x /etc/init.d/rsyncd
/etc/init.d/rsyncd start :可以执行
2.加入开机自动执行服务
方式一:
在上述脚本中把bash解释器下2行加入:
#chkconfig: 2345 21 81
#description
注:#不能丢,后面的数字要和/etc/init.d/rc3.d/下面的文件名数字不能重复
chkconfig --add rsyncd #加入自启动,rsyncd 一定要放到/etc/init.d/目录下
chkconfig --list | grep rsyncd #检测自启动
方式二:
在/etc/rc.local文件中加入
/etc/init.d/rsyncd start
判断一个目录下的文件类型
#!/bin/bash
read -p "请输入要检测的路径: " lj
for i in `ls $lj`
do
if [ -L "$lj"/"$i" ];then
((link++))
fi
if [ -d "$lj"/"$i" ];then
((dir++))
fi
if [ -b "$lj"/"$i" ];then
((yb++))
fi
if [ -c "$lj"/"$i" ];then
((yc++))
fi
if [ -g "$lj"/"$i" ];then
((yg++))
fi
if [ -u "$lj"/"$i" ];then
((yu++))
fi
if [ -k "$lj"/"$i" ];then
((yk++))
fi
if [ -p "$lj"/"$i" ];then
((yp++))
fi
if [ -s "$lj"/"$i" ];then
((ys++))
fi
if [ -f "$lj"/"$i" ];then
((file++))
fi
done
echo "
${lj}的检测结果:
--------------------------------------------
普通文件和硬链接数: $file
目录和软链接数: $dir
软链接文件数: $link
块设备文件数: $yb
字符设备文件数: $yc
设置SGID文件数: $yg
设置SUID文件数: $yu
沾滞位文件数: $yk
管道文件数: $yp
空文件数: $ys
"
计算所有参数(整数)之积
#!/bin/bash
function usage(){
if [ $# -lt 2 ];then
echo "usage: $0 num1 num2 ..."
fi
}
function zhengshu(){
for i in $*
do
((i++))
if [ $? -ne 0 ];then
usage
fi
done
}
function getsum(){
local sum=1
for n in $*
do
((sum*=n))
done
return $sum
}
function main(){
usage $*
zhengshu $*
getsum $*
echo $?
}
main $*
整数的加减,结果可以为负数
#!/bin/bash
function usage(){
if [ $# -lt 2 ];then
echo "usage: $0 num1 num2 ..."
fi
}
function zhengshu(){
for i in $*
do
((i++))
if [ $? -ne 0 ];then
usage
fi
done
}
function getsum(){
local sum=0
for n in $*
do
((sum+=n))
done
if [ $sum -lt 0 ];then
echo $sum
exit 0
else
return $sum
fi
}
function main(){
usage $*
zhengshu $*
getsum $*
echo $?
}
main $*
数的加减,结果可以为负数,小数可以作为参数
#!/bin/bash
function usage(){
if [ $# -lt 2 ];then
echo "usage: $0 num1 num2 ..."
fi
}
function getsum(){
local sum=0
for n in $*
do
sum=`echo "scale=2;${sum}+${n}"|bc`
done
echo $sum
}
function main(){
usage $*
getsum $*
}
main $*
数的乘积,结果可以为负数,小数可以作为参数
#!/bin/bash
function usage(){
if [ $# -lt 2 ];then
echo "usage: $0 num1 num2 ..."
fi
}
function getsum(){
local sum=1
for n in $*
do
sum=`echo "scale=2;${sum}*${n}"|bc`
done
echo $sum
}
function main(){
usage $*
getsum $*
}
main $*
数的商,结果可以为负数,小数可以作为参数
#!/bin/bash
function usage(){
if [ $# -lt 2 ];then
echo "usage: $0 num1 num2 ..."
fi
}
function getsum(){
local sum=`echo "scale=2;${1}*${1}"|bc`
for n in $*
do
sum=`echo "scale=2;${sum}/${n}"|bc`
done
echo $sum
}
function main(){
usage $*
getsum $*
}
main $*