shell脚本基础进阶(四)----做业

20150913-15做业shell

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

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

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

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

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

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

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

sed详解spa

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

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

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

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

六、写一个脚本,完成以下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;(使用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

七、求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

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

shell脚本基础进阶(三)----流程控制语句  练习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


十、写一个脚本,打印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

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

#!/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
相关文章
相关标签/搜索