shell脚本程序
2016-04-18 21:06
645 查看
学习shell脚本是需要经过大量的练习来巩固语法的(就像我们当初学习C语言是一样的)。下面为自己在用shell编写的脚本程序。
1.求1.....100所有数的总和(程序如下)
2.使用递归的方法求出1....100的所有数总和(程序如下)
4.找出数组元素的最大值,最小值,以及求出数组元素的平均值(代码如下)
1.求1.....100所有数的总和(程序如下)
#! /bin/bash i=0 sum=0 for (( ; i<=100; ++i )) do let sum+=i done echo $sum最终输出结果为5050
2.使用递归的方法求出1....100的所有数总和(程序如下)
<span style="font-family:SimSun;">#! /bin/bash function add() { local tmp1=$1 local tmp2=0 if [ $tmp1 -le 1 ];then sum=1 else tmp2=$(( $tmp1 - 1 )) add $tmp2 sum=(( $tmp1 + $tmp2 )) fi done echo $sum</span>3.编写一个进度条
<span style="font-size:18px;"><strong>#! /bin/bash function proc_bar() { rate=0 str=#" arr=("|" "/" "-" "\\") while [ $rate -le 100 ] do index=rate%4 printf " [%-100s] [%d%%] [%s] \r" "$str" "$rate" "${arr[$index]}" str=${str}"#" let rate++ usleep 10000 done printf "\n" } proc_bar</strong></span>
4.找出数组元素的最大值,最小值,以及求出数组元素的平均值(代码如下)
<strong>#! /bin/bash function proc() { arr=(1 2 33 44 55 66} max=${arr[0]} min=${arr[0]} argv=0 i=0 for (( ; i<${#arr[@]}; ++i )) do if [ $max -lt ${arr[$i]} ];then max=${arr[$i]} elif [ $min -gt ${arr[$i]} ];then min=${arr[$i]} fi let argv+=${arr[$i]} done echo $max exho $min exho "ibase=10;scale=2;${argv}/${#arr[@]}" | bc } proc</strong>
相关文章推荐
- sprintf(cmd_buff,"%s_bak.exe",pExeName);ShellExecuteA(NULL,"open",cmd_buff,argvs,
- Linux中shell脚本自动输入密码
- ns3中路由协议仿真的shell脚本编写
- shell中的IFS和$*变量
- 在secureCRT中解决hbase spark shell退格不能删除问题
- shell并发处理脚本
- shell脚本之sed使用详解
- shell脚本之grep使用详解
- adb shell 命令
- SHELL syntax error:unexpected end of file 提示错误
- 关于adb logcat
- shell下的进程
- 解决bash: mysql: command not found 的方法
- linux shell实现 URL 编码/解码方法
- linux shell 字符串操作(长度,查找,替换)详解
- 基于CentOS的MySQL学习补充三--使用Shell批量创建数据库表
- xshell的快捷键(非常实用)
- 使用zsh的十大优点
- shell的wc命令统计 head tail命令详解
- shell echo颜色例子