1、描述shell程序的運行原理(可附帶必要的圖形說明); |
SHELL是一種特殊的程序,官方定義爲是和內核的接口,類似於內核的代理人。人類和LINUX系統對話時,需要一個翻譯,而shell就是這個翻譯。我們用SHELL編輯自己想要執行的一系 列命令後,SHELL可以翻譯給系統內核聽,然後系統內核執行。 |
2、總結shell編程中所涉及到的所有知識點(如:變量、語法、命令狀態等等等,要帶圖的喲); |
//以下實例來自iredmail的安裝腳本 tmprootdir="$(dirname $0)" //定義臨時目錄,也就是shell中的變量 echo ${tmprootdir} | grep '^/' >/dev/null 2>&1 if [ X"$?" == X"0" ]; then //根據上文中最後的命令狀態,判斷是否執行 export ROOTDIR="${tmprootdir}" //同時使用了命令狀態和if判斷 else export ROOTDIR="$(pwd)" fi # Create SSL/TLS cert file. check_status_before_run generate_ssl_keys //check_status_before_run是個函數 #下面是這個函數的定義 check_status_before_run() { # If function was successfully executed, this function will write one line # in $STATUS_FILE: # # export status_[function_name]='DONE' # function_name="${1}" //利用位置參數來獲得變量值 function_status_name="status_${function_name}" function_status_value="$(eval echo \$${function_status_name})" if [ X"${function_status_value}" == X"DONE" ]; then ECHO_SKIP "Function: $1." else $function_name #if [ X"$?" == X'0' ]; then # echo "export ${function_status_name}='DONE'" >> ${STATUS_FILE} #fi fi } #另一個函數的定義,用到了for循環,if判斷 backup_file() { # Usage: backup_file file1 [file2 file3 ... fileN] if [ X"$#" != X"0" ]; then //$# 添加到Shell的參數個數 for conf_file in $@; do //利用for循環讀取配置文件, if [ -f ${conf_file} ]; then if [ X"${IREDMAIL_DEBUG}" == X"YES" ]; then echo -e "${_BACKUP_FLAG} ${conf_file} -> $(basename ${conf_file}).${DATE}." fi cp -f ${conf_file} ${conf_file}.${DATE} else : fi done else : fi } |
3、總結課程所講的所有循環語句、條件判斷的使用方法及其相關示例;(if (jpg|png is not exist);echo ”You say a XX“) |
4、寫一個腳本:如果某路徑不存在,則將其創建爲目錄;否則顯示其存在,並顯示內容類型;(不要懷疑,就是這麼簡單) |
//目前就判斷了目錄和文件,其他類型的未做判斷 cat a.sh #!/bin/bash if [ -z $1 ];then echo "命令用法: sh a.sh 文件名或者目錄" exit fi if [ -e $1 ];then if [ -d $1 ];then echo "$1存在且是一個目錄" elif [ -f $1 ];then echo "$1存在且是一個文件" fi else echo "文件或目錄不存在" mkdir $1 echo "$1創建成功!!" fi [@iZ94lrg5ub8Z ~]$ sh a.sh /etc /etc存在且是一個目錄 [@iZ94lrg5ub8Z ~]$ sh a.sh lamp.zip lamp.zip存在且是一個文件 [@iZ94lrg5ub8Z ~]$ sh a.sh lamp 文件或目錄不存在 $ sh a.sh lazz 文件或目錄不存在 lazz創建成功!! |
5、寫一個腳本,完成如下功能;判斷給定的兩個數值,孰大孰小;給定數值的方法:腳本參數,命令交互; |
$ vim read.sh #!/bin/bash echo "請輸入第一個數:" read first echo "請輸入第二個數:" read second if (( $first > $second ));then echo "第一個數比第二個數大" else echo "第二個數比第一個數大" fi $ sh read.sh 請輸入第一個數: 22 請輸入第二個數: 33 第二個數比第一個數大 |
6、求100以內所有奇數之和 |
di第一 第一種方法 [levis@iZ94lrg5ub8Z ~]$ sh b.sh 2500 [levis@iZ94lrg5ub8Z ~]$ cat b.sh sum=0 for i in `seq 1 2 100` do let sum=sum+$i done echo $sum 第二種方法: [levis@iZ94lrg5ub8Z ~]$ sh b.sh 2500 [levis@iZ94lrg5ub8Z ~]$ cat b.sh sum=0 for i in {1..100..2} do let sum=sum+$i done echo $sum 第三種方法: [levis@iZ94lrg5ub8Z ~]$ sh c.sh 2500 [levis@iZ94lrg5ub8Z ~]$ cat c.sh #!/bin/bash sum=0 for ((i=1;i<=100;i++)) do if [ `expr $i % 2` -ne 0 ];then let sum=$sum+$i fi done echo $sum |
7、寫一個腳本實現如下功能: (1) 傳遞兩個文本文件路徑給腳本; (2) 顯示兩個文件中空白行數較多的文件及其空白行的個數; (3) 顯示兩個文件中總行數較多的文件及其總行數; |
[levis@iZ94lrg5ub8Z ~]$ cat d.sh #!/bin/bash file1=$1 file2=$2 if [ $# != 2 ];then echo "命令格式不對, 命令格式: sh d.sh file1 file2" exit fi if [ ! -f $file1 ];then echo "file1 必須是文件格式" exit fi if [ ! -f $file2 ];then echo "file2 必須是文件格式" fi line1_space=`grep "^$" $file1 | wc -l` line2_space=`grep "^$" $file2 | wc -l` if (( $line1_space > $line2_space ));then echo "$file1的空白行數比較多,空行行數爲$line1_space" else echo "$file2的空白行數比較多,空行行數爲$line2_space" fi line1_sum=`cat $file1 | wc -l` line2_sum=`cat $file2 | wc -l` if (( $line1_sum > $line2_sum ));then echo "$file1行數比較多,總行數爲$line1_sum" else echo "$file2行數比較多,總行數爲$line2_sum" fi [levis@iZ94lrg5ub8Z ~]$ sh d.sh dd.txt /etc/init.d/functions /etc/init.d/functions的空白行數比較多,空行行數爲72 /etc/init.d/functions行數比較多,總行數爲812 [levis@iZ94lrg5ub8Z ~]$ sh d.sh /etc/rc. /etc/init.d/functions rc.d/ rc.local rc.sysinit [levis@iZ94lrg5ub8Z ~]$ sh d.sh /etc/rc.sysinit /etc/init.d/functions /etc/rc.sysinit的空白行數比較多,空行行數爲96 /etc/init.d/functions行數比較多,總行數爲812 |
8、寫一個腳本 (1) 提示用戶輸入一個字符串; (2) 判斷: 如果輸入的是quit,則退出腳本; 否則,則顯示其輸入的字符串內容; |
[levis@iZ94lrg5ub8Z ~]$ cat f.sh #!/bin/bash while [ 1 -eq 1 ] do echo "輸入一個字符串:" read answers if [ -z answers ];then echo "請輸入字符串" fi if [ $answers == quit ];then echo "系統退出" exit fi echo "你輸入的字符串是$answers" done [levis@iZ94lrg5ub8Z ~]$ sh f.sh 輸入一個字符串: ss 你輸入的字符串是ss 輸入一個字符串: erer 你輸入的字符串是erer 輸入一個字符串: quit 系統退出 |
9、寫一個腳本,打印2^n表;n等於一個用戶輸入的值;(不好意思,我調皮了) |
[levis@iZ94lrg5ub8Z ~]$ cat a.sh #!/bin/bash read -p "請輸入一個數字:" num count=2 for i in $(seq 0 $num);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] else echo "Error:請輸入一個數字" fi done [levis@iZ94lrg5ub8Z ~]$ sh a.sh 請輸入一個數字:3 1 2 2x2=4 2x2x2=8 |
11、寫一個腳本,寫這麼幾個函數:函數1、實現給定的兩個數值的之和;函數2、取給定兩個數值的最大公約數;函數3、取給定兩個數值的最小公倍數;關於函數的選定、兩個數值的大小都將通過交互式輸入來提供。 |
原地 源碼地址:http://20150721.blog.51cto.com/10649191/1695664 |
補充的一題sed和awk的用法 |
SED的用法總結 |
AWK的用法總結 |
2015年9月13日課程作業--關於SHELL的問題
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.