shell腳本基礎進階(四)----作業

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章