天天看點

shell腳本-題

1:用source執行腳本和用bash執行Shell腳本的差別是什麼?

  source 執行腳本,其中的變量指派是在目前shell的;bash 執行腳本,其中的shell是在下一級的bash的是以,這個變量值,并不在目前shell中,而是在子shell中

2:如何自定義環境變量?

  環境變量在 /etc/profile 或者 ~/.protfile 檔案中定義,使用export 參數來定義環境變量同時,使用source或.指令讓其生效。

3:定義變量内容,不加引号、單引号、雙引号、反引号結果有什麼不同?應該怎麼用?

  單引号表示輸入是什麼,顯示的就是什麼(單引号将剝奪其中的所有字元的特殊含義)

  雙引号為固定字元串,解決空格問題

  反引号表示輸出的是指令執行的結構,同$()

4:請分别說出2個以上用于整數運算特殊符号或指令,以及适合小數運算的指令。

  整數:+ - let  

  小數:bc awk /

5:寫出特殊變量對應的意義及應用

  $0、$n、$#、$*、$@、$?、$$、$!、$_

  $0  腳本名字

  $n  $1,$2...${10}

  $*  所有變量--字元串顯示

  $@ 所有變量--清單形式

  $?  執行上一個指令的傳回值的狀态

  $!  執行上一個背景指令的PID(背景運作的最後一個程序的程序ID号)

  $$  腳本運作的目前程序ID号

  $_  顯示shell使用的目前選項,與set功能相同

6:寫出下面特殊變量擴充的意義

${test}、${#test}、${test:offset:length}、${test#word}、${test##word}、${test%word}

${test%word}、${test/pattern/string}、${test//pattern/string}、${test:-word}、${test: -word}

  ${test}  變量調用

  ${#test}  調用變量的長度

  ${test#word}   從變量${test}開頭開始删除最短比對的word子串

  ${test##word}  從${test}開頭開始删除最長比對的word子串

  ${test%word}  從變量${test}結尾開始删除最短的比對的word子串

  ${test%%word}  從變量${test}結尾開始,删除最長比對的word子串

  ${test/pattern/string}  使用string替代第一個比對的pattern

  ${test//pattern/string}  使用string替代所有比對的pattern

  ${test:-word}  如果test變量值為空,或未指派,就回傳回word字元替換變量值。

7:在條件測試與表達式中,[]和[[]]有何差別?

  [ 測試表達式 ]:一條判斷語句,如果是多個條件判斷,使用-a -o來聯合起來

  [[ 測試表達式 ]]:多條語句的聯合判斷,使用 && || 來關聯測試語句

8:說出适合在[]以及test中整數比較的特殊運算符号?

  -eq -en -lt -le -gt -ge

  -e -f -L -d -b -r -w -x

  -a -o !

  -z -n = !=

9、輸出下面菜單

  1.install MySQL

  2.install Tomcat

  3.exit當使用者選擇對應的數字就開始安裝對應的服務(可echo輸出替代),需要對使用者輸入的數字進行判斷是否為整數(用條件測試表達式實作)。

#!/bin/bash
# Author: 韓萌萌
# Blog: https://www.cnblogs.com/hmm01031007/
# Time: 2019-08-21 01:20:37
# Name: caidan.sh
# Version: v1.0
# Description: This is my Script.
cat << EOF
=====================
1.install MySQL
2.install Tomcat
3.exit
====================
EOF
read -p "請輸入需要安裝對應服務的數字:" num
expr $num + 1 &>/dev/null
if [ $? -ne 0 ];then
    echo "請輸入需要安裝對應服務的數字{1|2|3}"
    exit 1
fi
if [ $num -eq 1 ];then
    yum install MySQL
    exit 1
elif [ $num -eq 2 ];then
    yum install Tomcat
    exit 2
elif [ $num -eq 3 ];then
    echo exit
else
    echo "請輸入一個整數"
fi      
shell腳本-題

10、随機生成十個數進行大小比較,并列印最大數值(用條件表達式實作)。

#!/bin/bash
# Author: 韓萌萌
# Blog: https://www.cnblogs.com/hmm01031007/
# Time: 2019-08-21 22:36:45
# Name: suiji.sh
# Version: v1.0
# Description: This is my Script.
declare -a a
declare -i max=0
for i in `seq 0 9`;do
     a[$i]=$RANDOM
    [ ${a[$i]} -gt $max ] && max=${a[$i]}
done
echo ${a[@]}
echo "最大值為:$max"      
shell腳本-題

11、開發Shell腳本判斷系統根分區剩餘空間的大小并列印出來,如果使用率高于70%就提示“硬碟空間不足”,否則提示“硬碟空間充足”。

#!/bin/bash
# Author: 韓萌萌
# Blog: https://www.cnblogs.com/hmm01031007/
# Time: 2019-08-21 17:12:32
# Name: disk.sh
# Version: v1.0
# Description: This is my Script.
num=`df -m |grep "/dev/mapper" |cut -d" " -f9 |sed "s/%//"`
if [ $num -gt 70 ];then
    echo "硬碟空間不足"
else
    echo "硬碟空間充足,使用率為$num%"
fi      
shell腳本-題

12、分别使用變量定義、read讀入及腳本傳參方式實作比較2個整數的大小。

#!/bin/bash
# Author: 韓萌萌
# Blog: https://www.cnblogs.com/hmm01031007/
# Time: 2019-08-24 23:06:36
# Name: compare.sh
# Version: v1.0
# Description: This is my Script.
read -p "請輸入兩個數字: " num1 num2
if [ $num1 -gt $num2 ];then
    echo "$num1大于$num2"
elif [ $num1 -lt $num2 ];then
    echo "$num1小于$num2"
else
    echo "$num1等于$num2"
fi      
shell腳本-題

13、列印一個菜單如下,當使用者選擇對應的數字時,就執行對應項的應用,最好對使用者的輸入進行是否為整數判斷。

  1.install lamp

  2.install lnmp

  3.exit

#!/bin/bash
# Author: 韓萌萌
# Blog: https://www.cnblogs.com/hmm01031007/
# Time: 2019-08-21 18:02:52
# Name: menu.sh
# Version: v1.0
# Description: This is my Script.
read -p "請輸入一個數字:" num
expr $num + 1 &>/dev/null
if [ $? -ne 0 ];then
echo "請輸入:{1|2|3}"
exit 1
fi
if [ $num -eq 1 ];then
    yum install lamp
    exit 1
elif [ $num -eq 2 ];then
    yum install lnmp
    exit 2
elif [ $num -eq 3 ];then
    echo exit
fi      
shell腳本-題

14、通過腳本傳參的方式,檢查Web網站URL是否正常(要求主體使用函數)。

#!/bin/bash
# Author: 韓萌萌
# Blog: https://www.cnblogs.com/hmm01031007/
# Time: 2019-08-21 16:37:43
# Name: url.sh
# Version: v1.0
url=http://www.baidu.com
check_code(){
    status_code=$(curl -m 5 -s -o /dev/null -w %{http_code} $url)
}
while true;do
check_code
date=`date +%Y%m%d-%H:%M:%S`
echo "目前時間為:$date"
echo "$url伺服器異常,狀态碼為$status_code.請盡快排查異常" >/tmp/http$$.pid
if [ $status_code -ne 200 ];then
    mail -s Warning root < /tmp/http$$.pid 
else
    echo "$url連接配接正常" >> /var/log/http.log
fi
sleep 5
done      
shell腳本-題

15、利用case語句開發Rsync服務啟動停止腳本,并能通過chkconfig實作開機自啟動。

16、猜數字遊戲。首先讓系統随機生成一個數字,給這個數字定一個範圍(1-60),讓使用者輸入猜的數字,對輸入進行判斷,如果不符合要求,就給予高或低的提示,猜對後則給出猜對用的次數,請用while語句實作。

#!/bin/bash
# Author: 韓萌萌
# Blog: https://www.cnblogs.com/hmm01031007/
# Time: 2019-08-21 16:42:54
# Name: caishuzi.sh
# Description: This is my Script.
guess=$[$RANDOM % 60+1]
declare -i count=1
while true;do
        read -p "請輸入你猜的數字:" num
        if [ $num -lt $guess ];then
                echo "太小了!"
        elif [ $num -gt $guess ];then
                echo "太大了!"
        elif [ $num -eq $guess ];then
     echo "恭喜你猜對了,共猜了$count次"
     exit 0
    fi
    let count++
done      
shell腳本-題

17、分析apache通路日志(自備),把日志中每行的通路位元組數對應字段數字相加,計算出總的通路量。給出實作程式,請用while循環實作。

18、計算從1到100中步長為3的所有數值之和(要求用for和while,至少給出兩種方法)。

#!/bin/bash
# Author: 韓萌萌
# Blog: https://www.cnblogs.com/hmm01031007/
# Time: 2019-08-21 01:58:00
# Name: 100.sh
# Version: v1.0
# Description: 使用for循環寫1-100步長為3的和..
declare -i sum=0
for i in `seq 1 3 100`;do
    sum=$[sum+i]
done
echo "1-100的和為:$sum"      
shell腳本-題
#!/bin/bash
# Author: 韓萌萌
# Blog: https://www.cnblogs.com/hmm01031007/
# Time: 2019-08-21 02:02:12
# Name: 1001.sh
# Version: v1.0
# Description: 使用while循環寫1-100步長為3的和.
declare -i i=1
declare -i sum=0
while  [ $i -le 100 ];do
    sum=$[sum+i]
        i=$i+3
done
echo "1-100的和為:$sum"      
shell腳本-題

19、利用awk指令和bash for循環兩種方式列印下面這句話中字母數不大于6的單詞(某企業面試真題)。I am dfzy teacher welcome to dfzy training class

#!/bin/bash
# Author: 韓萌萌
# Blog: https://www.cnblogs.com/hmm01031007/
# Time: 2019-08-21 18:40:28
# Name: char.sh
# Version: v1.0
# Description: 使用for循環列印.
for i in I am dfzy teacher welcome to dfzy training class;do
    [ `echo $i|wc -L` -le 6 ] && echo $i
done      
shell腳本-題

使用awk指令列印:

shell腳本-題

20、使用read讀入方式比較兩個整數大小,要求對使用者輸入的内容嚴格判斷是否為整數,是否輸入了兩個數字。

#!/bin/bash
# Author: 韓萌萌
# Blog: https://www.cnblogs.com/hmm01031007/
# Time: 2019-08-21 18:46:36
# Name: num.sh
# Version: v1.0
# Description: This is my Script.
read -p "請輸入兩個數:" num1 num2
str1=$num1
str2=$num2
[ -z "$str1" -o -z "$str2" ]&&echo "錯誤的輸入,你可能隻輸入了一個數"&& exit 1
expr $str1 + 0 &>/dev/null
[ $? -ne 0 ] && echo "$str1必須是整數!"&&exit 2
expr $str2 + 0 &>/dev/null
[ $? -ne 0 ] && echo "$str2必須是整數!"&&exit 2
if [ $str1 -eq $str2 ];then
        echo "兩個數相等"
elif [ $str1 -lt $str2 ];then
        echo "$str1小于$str2"
else
        echo "$str1大于$str2"
fi      
shell腳本-題

轉載于:https://www.cnblogs.com/hmm01031007/p/11397925.html