20150913-15做業shell
一、描述shell程序的運行原理(可附帶必要的圖形說明)編程
二、總結shell編程中所涉及到的全部知識點(如:變量、語法、命令狀態等等等,要帶圖的喲)less
三、總結課程所講的全部循環語句、條件判斷的使用方法及其相關示例;(if (jpg|png is not exist);echo 」You say a XX「)函數
四、總結文本處理工具sed及awk的用法;(必須附帶示例)ui
sed詳解spa
五、寫一個腳本:若是某路徑不存在,則將其建立爲目錄;不然顯示其存在,並顯示內容類型;(不要懷疑,就是這麼簡單)orm
修改版:可讓用戶自定義路徑
#!/bin/bash # if [ -e $1 ];then echo "$1 exists." file $1 else mkdir -p $1 fi
六、寫一個腳本,完成以下功能;判斷給定的兩個數值,孰大孰小;給定數值的方法:腳本參數,命令交互;(使用read,依然如此簡單)
命令交互:
#!/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) 顯示兩個文件中總行數較多的文件及其總行數;
九、寫一個腳本(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