shell脚本:练习题
2016-06-19 21:48
671 查看
题目1:1-100求和
非递归版本:
递归版本:
bc计算:
2字符串转换,前后数字转换,字母大小写转换:
之前:
753asd456
789dfg123
789fdh456
改变后:
求文件中行代表数字的最大值最小值:
进度条:
斐波那契数列:
本文出自 “剩蛋君” 博客,请务必保留此出处http://memory73.blog.51cto.com/10530560/1790875
非递归版本:
#!/bin/bash sum=0 while [ $val -le 100 ] do let sum+=val let val++ done echo $sum sum=0 for i in {1..100} do if [ $i == 1 ]; then bar=${bar}${i} else bar=${bar}+${i} fi let sum+=i done echo $bar=$sum sum=0 for (( i=1 ; i<=100 ; i++)) do let sum+=i done echo $sum运行结果:
递归版本:
#!/bin/bash function add() { local count=$1 if [ $count -eq 1 ];then echo 1 return fi local tmp=$count let --tmp local ret=`add $tmp` let ret+=count echo $ret } read c ret=$(add $c) echo "$ret"运行结果:
bc计算:
2字符串转换,前后数字转换,字母大小写转换:
#!/bin/bash function str() { while read str do str1=`echo ${str:0:3}` str2=`echo ${str:3:3} | tr '[a-z]' '[A-Z]'` str3=`echo ${str:6:3}` printf "%s%s%s\n" "$str3" "$str2" "$str1" done<file } str运行结果:
之前:
753asd456
789dfg123
789fdh456
改变后:
求文件中行代表数字的最大值最小值:
#!/bin/bash read name avg=0 max=0 min=0 cur=0 sum=0 count=0 #cat $name | while read line while read line do let cur=$line if [ $count -eq 0 ];then max=$line min=$line fi if [ $cur -gt $max ];then max=$cur fi if [ $cur -le $min ];then min=$cur fi let count++ let sum+=cur done<$name echo "MAX:$max" echo "MIN:$min" echo "SUM:$sum COUNT:$count" echo "average: $((sum/count))"运行结果:
进度条:
斐波那契数列:
#!/bin/bash arry[0]=1 arry[1]=2 temp=0 sum=0 for i in {0..99} do if [ $i -gt 0 ];then let arry[$i+1]=arry[$i]+arry[$i-1] fi done echo ${arry[99]}运行结果:
本文出自 “剩蛋君” 博客,请务必保留此出处http://memory73.blog.51cto.com/10530560/1790875
相关文章推荐
- Shell 脚本知识回顾 (一) —— 基础篇
- [shell基础]——echo命令
- Shell 脚本知识回顾 (二) —— Shell变量
- Shell基本知识
- Shell 脚本知识回顾 (三) —— 替换、运算符、字符串、数组
- Shell 脚本知识回顾 (四) —— Shell 命令及Shell 相关语句
- Shell 脚本知识回顾 (五) —— Shell 循环
- Shell 脚本知识回顾 (六) —— Shell 函数
- Shell 脚本中如何使用make命令
- linux shell 流程控制
- shell脚本应用小程序
- shell脚本编程练习
- Linux 下shell编程
- shell脚本练习之代码
- [shell基础]——变量
- hbase shell基础和常用命令详解
- bash编程总结
- Shell Script编程——USB挂载/复制文件/查找文件/压缩文件
- Shell脚本_创建系统用户,从一个用户列表文件(6.18重构)。
- shell脚本:字符串的截取