20150913-15作业

1、描述shell程序的运行原理(可附带必要的图形说明)

shell脚本基础进阶(一)----shell介绍

2、总结shell编程中所涉及到的所有知识点(如:变量、语法、命令状态等等等,要带图的哟)

shell脚本基础进阶(二)----变量及运算符

3、总结课程所讲的所有循环语句、条件判断的使用方法及其相关示例;(if (jpg|png is not exist);echo ”You say a XX“)

shell脚本基础进阶(三)----流程控制语句

4、总结文本处理工具sed及awk的用法;(必须附带示例)

sed详解

5、写一个脚本:如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型;(不要怀疑,就是这么简单)

shell脚本基础进阶(三)----流程控制语句 练习1

修改版:可以让用户自定义路径

#!/bin/bash
#
if [ -e $1 ];then
   echo "$1 exists."
   file $1
else
   mkdir -p $1
fi

6、写一个脚本,完成如下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;(使用read,依然如此简单)

shell脚本基础进阶(三)----流程控制语句 练习5

命令交互:

#!/bin/bash
#
read -p "plz input two integer:" -t 10 num1 num2
if [ -z $num1 ]||[ -z $num2 ];then
   echo "your input parameters are less than 2.plz re-enter."
   exit 1
fi
if [[ $num1 =~ ^[0-9]+$ ]]&&[[ $num2 =~ ^[0-9]+$ ]];then
   if [ $num1 -gt $num2 ];then
     echo "the max number is $num1."
     echo "the min number is $num2."
   else
     echo "the max number is $num2."
     echo "the min number is $num1."
   fi
else
   echo "the number $num1 or $num2 is not a integer.at least have a string."
fi

7、求100以内所有奇数之和(至少用3种方法。是的这是我们的作业^_^)

方法一:

#!/bin/bash
declare -i sum
for i in {1..100};do
  if [ $[$i%2] -eq 1 ];then
    sum+=$i
  fi
done
echo $sum

方法二:

#!/bin/bash
declare -i sum
for i in `seq 1 2 100`;do
    sum+=$i
done
echo $sum

方法三:

#!/bin/bash
#
declare -i sum
declare -i i=1
while [ $i -lt 101 ];do
  sum+=$i
  i+=2
done
echo $sum

8、写一个脚本实现如下功能:(1) 传递两个文本文件路径给脚本;(2) 显示两个文件中空白行数较多的文件及其空白行的个数;(3) 显示两个文件中总行数较多的文件及其总行数;

shell脚本基础进阶(三)----流程控制语句  练习9


9、写一个脚本(1) 提示用户输入一个字符串;(2) 判断:如果输入的是quit,则退出脚本;否则,则显示其输入的字符串内容;

#!/bin/bash
#
read -p "plz enter a string:" -t 10 str
if [ $str == quit ];then
  exit 1
else
  echo $str
fi


10、写一个脚本,打印2^n表;n等于一个用户输入的值;(不好意思,我调皮了)

#!/bin/bash
#
read -t 5 -p "please enter a integer: " n
if [ -z $n ]||[ $n -lt 0 ];then
 echo "your enter is error."
else
count=2
 for((i=0;i<=$n;i++));do
    if [ $i -eq 0 ];then
       echo -e "1"
    elif [ $i -eq 1 ];then
       echo -e "2"
    elif [ $i -gt 1 ];then
     count+=x2          
     echo $count=$[2**$i]
    fi
done
fi

11、写一个脚本,写这么几个函数:函数1、实现给定的两个数值的之和;函数2、取给定两个数值的最大公约数;函数3、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将通过交互式输入来提供。

#!/bin/bash
#
read -p "plz enter two integer:" -t 20 num1 num2
if [ -z $num1 ]||[ -z $num2 ];then
  echo "your enter is error.plz enter two integer."
  exit 1
fi
sum() {
declare -i sum
sum=$[$num1+$num2]
#echo "the sum of two integer is $sum."
}
GCD() {
while [ $num1 != $num2 ];do
if [ $num1 -gt $num2 ];then
  poor=$[$num1-$num2]
  num1=$poor
 elif [ $num2 -gt $num1 ];then
  poor=$[$num2-$num1]
  num2=$poor
fi
done
#echo "the GCD of two integer is $poor."
return $poor
}
LCM() {
pro=$[$num1*$num2]
while [ $num1 != $num2 ];do
if [ $num1 -gt $num2 ];then
  poor=$[$num1-$num2]
  num1=$poor
 elif [ $num2 -gt $num1 ];then
  poor=$[$num2-$num1]
  num2=$poor
fi
done
lcm=$[$pro/$poor]
}
case $1 in
 sum)
   sum
   echo "the sum of two integer is $sum."
   ;;
 gcd)
   GCD
   echo "the GCD of two integer is $poor."
   ;;
 lcm)
   LCM
   echo "the LCM of two integer is $lcm."
   ;;
 *)
  echo "Usage:$0 sum|gcd|lcm"
  exit 1
esac