1、做业(练习)内容:
shell
一、描述shell程序的运行原理(可附带必要的图形说明);编程
二、总结shell编程中所涉及到的全部知识点(如:变量、语法、命令状态等等等,要带图的哟);数组
三、总结课程所讲的全部循环语句、条件判断的使用方法及其相关示例;(if (jpg|png is not exist);echo ”You say a XX“)bash
SHELL整理汇总:ide
四、总结文本处理工具sed及awk的用法;(必须附带示例)函数
Sed、Awk整理汇总:工具
五、写一个脚本:若是某路径不存在,则将其建立为目录;不然显示其存在,并显示内容类型;(不要怀疑,就是这么简单) 测试
代码实现:ui
1 2 3 4 5 6 7 8 9 spa |
|
#!/bin/bash read -p"Please Enter a PATH: " pathname if [ -e $pathname ];then echo "PATH--> $pathname exists!" echo "TYPE--> `file $pathname|cut -d: -f2`" else echo "PATH isn't existed.Then we create it" mkdir -p $pathname fi |
实验效果:

六、写一个脚本,完成以下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;
代码实现:
1 2 3 4 5 6 7 8 9
|
|
#!/bin/bash read -p "Please Enter 2 Numbers:" num1 num2 if [ $num1 -eq $num2 ];then echo "Tease me? They are the same number!" elif [ $num1 -lt $num2 ];then echo "$num1 < $num2" else echo "$num1 > $num2" fi |
实验效果:

七、求100之内全部奇数之和(至少用3种方法。是的这是咱们的做业^_^)
代码1:until+自增+$[]的方法
1 2 3 4 5 6 7 8 9
|
|
#!/bin/bash i=1 sum=0 until [ $i -gt 100 ] do sum=$[$sum+$i] i=$[$i+2] done echo "1+3+5+...+97+99 = $sum" |
代码2:for+seq+let的方法
1 2 3 4 5 6 7
|
|
#!/bin/bash sum=0 for i in $(seq 1 2 100) do let "sum=$sum+$i" done echo "$sum" |
代码3:奇偶判断+((运算))+for实现
1 2 3 4 5 6 7 8 9 10 11
|
|
#!/bin/bash sum=0 for i in $(seq 100) do var=$[$i%2] if [ $var -eq 1 ];then ((sum=$sum+$i)) i=`expr $i + 1` fi done echo "1+3+5+...+97+99 = $sum" |
实验效果:

八、写一个脚本实现以下功能:
(1) 传递两个文本文件路径给脚本;
(2) 显示两个文件中空白行数较多的文件及其空白行的个数;
(3) 显示两个文件中总行数较多的文件及其总行数;
代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
|
#!/bin/bash read -p "Please Give me two File_PATH-->" file1 file2 all_line1=`wc -l $file1 |cut -d' ' -f1` all_line2=`wc -l $file2 |cut -d' ' -f1` space_line1=`grep "^$" $file1 | wc -l |cut -d' ' -f1` space_line2=`grep "^$" $file2 | wc -l |cut -d' ' -f1` #Case 1: Which file has more space_lines and print. echo "Which has more space lines?" if [ $space_line1 -eq $space_line2 ];then echo "They have same space lines. Counts: $space_line1 Lines." elif [ $space_line1 -lt $space_line2 ];then echo "File $file2 . Counts: $space_line2 Lines." esle echo "File $file1 . Counts: $space_line1 Lines." fi #Case 2: Which file has more lines and print. echo "Which has more lines?" if [ $all_line1 -eq $all_line2 ];then echo "They have same lines. Counts: $all_line1 Lines." elif [ $space_line1 -lt $space_line2 ];then echo "File $file2 . Counts: $all_line2 Lines." esle echo "File $file1 . Counts: $all_line1 Lines." fi |
实验效果:

九、写一个脚本
(1) 提示用户输入一个字符串;
(2) 判断:若是输入的是quit,则退出脚本;不然,则显示其输入的字符串内容;
代码实现:
1 2 3 4 5 6 7
|
|
#!/bin/bash read -p "Please Enter a String: " str if [ $str = "quit" ];then exit else echo "$str" fi |
实验效果:

补充说明:若是用户输入的str中包含了空格如何处理呢?read后作一次判断操做便可!
1 2 3 4 5 6 7 8 9 10 11
|
|
#!/bin/bash read -p "Please Enter a String: " str echo $str | grep "[[:space:]]\+" &>/dev/null var=$? if [ $var -eq 0 ];then echo "No a standard string." elif [ $str = "quit" ];then exit else echo "$str" fi |
十、写一个脚本,打印2^n表;n等于一个用户输入的值;(很差意思,我调皮了)
代码1:[For循环实现]
1 2 3 4 5 6
|
|
#!/bin/bash read -p "Please Enter the N: " n for i in $(seq 0 $n) do echo "2^$i=$[2**$i]" done |
代码2:[while循环实现]
1 2 3 4 5 6 7 8
|
|
#!/bin/bash read -p "Please Enter the N: " n i=0 while [ $i -le $n ] do echo "2^$i=$[2**$i]" i=`expr $i + 1` done |
代码3:[until循环]
1 2 3 4 5 6 7 8
|
|
#!/bin/bash read -p "Please Enter the N: " n i=0 until [ $i -gt $n ] do echo "2^$i=$[2**$i]" i=`expr $i + 1` done |
实验效果:

十一、写一个脚本,写这么几个函数:
函数一、实现给定的两个数值的之和;
函数二、取给定两个数值的最大公约数;
函数三、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将经过交互式输入来提供。
思路分析:
求最大公倍数涉及数学方法,百度百科获得一般使用:展转相除法、更相减损法、质因数分解法等,用代码实现便可,而最小公倍数=(A * B ) / 最大公约数。
代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
|
#!/bin/bash echo "+---------------------+" echo "|Select Operation: |" echo "| 1 -> Sum |" echo "| 2 -> Common Divisor |" echo "| 3 -> Common Multiple|" echo "+---------------------+" read -p "Select Operation:-->" op read -p "Please Two Numbe:-->" a b
function sum() { sum_value=`expr $a + $b` }
function visor() { r="1" while [ $r != 0 ] do r=$[$a%$b] a=$b b=$r done visor_value=$a }
function mul() { product=$[$a*$b] visor $a $b mul_value=$[$product/$visor_value] }
case $op in 1) sum $a $b echo "SUM = $sum_value" ;; 2) visor $a $b echo "VISOR = $visor_value" ;; 3) mul $a $b echo "MUL= $mul_value" ;; *) echo "You mock me?" ;; esac |
实验效果:
