linux運維實戰練習-2015年9月13日-9月15日課程做業(練習)安排

一、描述shell程序的運行原理(可附帶必要的圖形說明);shell

Linux系統提供給用戶的最重要的系統程序是Shell命令語言解釋程序。它不屬於內核部分,而是在覈心以外,以用戶態方式運行。其基本功能是解釋並執行用戶打入的各類命令,實現用戶與Linux核心的接口。系統初啓後,核心爲每一個終端用戶創建一個進程去執行Shell解釋程序。它的執行過程基本上按以下步驟: 編程


(1)讀取用戶由鍵盤輸入的命令行。vim

(2)分析命令,以命令名做爲文件名,並將其它參數改造爲系統調用execve( )內部處理所要求的形式。 bash

(3)終端進程調用fork( )創建一個子進程。 ide

(4)終端進程自己用系統調用wait4( )來等待子進程完成(若是是後臺命令,則不等待)。當子進程運行時調用execve( ),子進程根據文件名(即命令名)到目錄中查找有關文件(這是命令解釋程序構成的文件),將它調入內存,執行這個程序(解釋這條命令)。 函數

(5)若是命令末尾有&號(後臺命令符號),則終端進程不用系統調用wait4( )等待,當即發提示符,讓用戶輸入下一個命令,。若是命令末尾沒有&號,則終端進程要一直等待,當子進程(即運行命令的進程)完成處理後終止,向父進程(終端進程)報告,此時終端進程醒來,在作必要的判別等工做後,終端進程發提示符,讓用戶輸入新的命令,重複上述處理過工具

程。ui

二、總結shell編程中所涉及到的全部知識點(如:變量、語法、命令狀態等等等,要帶圖的喲);this

      shell知識點總結spa

三、總結課程所講的全部循環語句、條件判斷的使用方法及其相關示例;(if (jpg|png is not exist);echo 」You say a XX「)

    循環語句及判斷條件

四、總結文本處理工具sed及awk的用法;(必須附帶示例)

五、寫一個腳本:若是某路徑不存在,則將其建立爲目錄;不然顯示其存在,並顯示內容類型;(不要懷疑,就是這麼簡單)

[root@pks tmp]# vim checkfile.sh 
[root@pks tmp]# bash -n checkfile.sh 
[root@pks tmp]# bash -x checkfile.sh 
+ '[' 0 -lt 1 ']'
+ echo 'please enter /path/to/somefile'
please enter /path/to/somefile
+ exit 1
[root@pks tmp]# bash -x checkfile.sh /etc/fstab 
+ '[' 1 -lt 1 ']'
+ '[' -e /etc/fstab ']'
+ echo '/etc/fstab is exists'
/etc/fstab is exists
+ file /etc/fstab
/etc/fstab: ASCII text
[root@pks tmp]# cat checkfile.sh 
#!/bin/bash
#
if [ $# -lt 1 ];then
   echo "please enter /path/to/somefile"
   exit 1
fi
if [ -e $1 ];then
  echo "$1 is exists"
  file $1 
else mkdir -p $1
fi


六、寫一個腳本,完成以下功能;判斷給定的兩個數值,孰大孰小;給定數值的方法:腳本參數,命令交互;(使用read,依然如此簡單)

[root@pks tmp]# bash -x bidaxiao.sh 
+ read -p 'enter two number' -t 30 a b
enter two number5 9
+ '[' 5 -gt 9 ']'
+ '[' 5 -lt 9 ']'
+ max=9
+ min=5
[root@pks tmp]# bash -x bidaxiao.sh 
+ read -p 'enter two number' -t 30 a b
enter two number10 7
+ '[' 10 -gt 7 ']'
+ max=10
+ min=7
[root@pks tmp]# cat bidaxiao.sh 
#!/bin/bash
#
read -p "enter two number" -t 30  a  b
if [ $a -gt $b ];then
    max=$a
    min=$b
elif [ $a -lt $b ];then
    max=$b
    min=$a
else
  echo"two number is same"
fi


七、求100之內全部奇數之和(至少用3種方法。是的這是咱們的做業^_^)

 方法1

[root@pks tmp]# bash -n sum1.sh 
[root@pks tmp]# bash  sum1.sh 
the sum=2500
[root@pks tmp]# cat sum1.sh 
#!/bin/bash
#
declare -i sum=0
declare -i i=0
until  [ $i -gt 100 ]; do
if [ $[${i}%2] -ne 0 ]; then
  let sum+=$i
        fi
let i++
done
echo "the sum=$sum"
[root@pks tmp]#

 方法2

root@pks tmp]# cat sum2.sh 
#!/bin/bash
#
declare -i sum=0
declare -i i=0
while  [ $i -le 100 ]; do
if [ $[${i}%2] -ne 0 ]; then
  let sum+=$i
        fi
let i++
done
echo "the sum=$sum"
[root@pks tmp]#


 方法3

[root@pks tmp]# bash  -n sum3.sh 
[root@pks tmp]# bash   sum3.sh 
the sum=2500
[root@pks tmp]# cat sum3.sh 
#!/bin/bash
#
declare -i sum=0
declare -i i=0
for (( i=1;i<=100;i++ ));do
if [ $[${i}%2] -ne 0 ]; then
  let sum+=$i
        fi
done
echo "the sum=$sum"
[root@pks tmp]#

八、寫一個腳本實現以下功能:

(1) 傳遞兩個文本文件路徑給腳本;

(2) 顯示兩個文件中空白行數較多的文件及其空白行的個數;

(3) 顯示兩個文件中總行數較多的文件及其總行數;

[root@pks tmp]# cat /etc/test
pks
[root@pks tmp]# cat /etc/test1
pks
[root@pks tmp]# bash -n test.sh 
[root@pks tmp]# bash test.sh 
enter two file/etc/test /etc/test1
the max space file is /etc/test : 6
the max file is /etc/test : 7
[root@pks tmp]# cat test.sh 
#!/bin/bash
#
read -p "enter two file"  -t 10 file1 file2
a=$(grep "^$" $file1 | wc -l )
b=$(grep "^$" $file2 | wc -l )
if [ $a -gt $b ];then
   echo "the max space file is $file1 : $a"
else
   echo "the max space file is $file2 : $b"
fi
c=$( wc -l $file1 | cut -d ' ' -f1 )
d=$( wc -l $file2 | cut -d ' ' -f1 )
if [ $c -gt $d ];then
   echo "the max file is $file1 : $c"
else
   echo "the max file is $file2 : $d"
fi
[root@pks tmp]#


九、寫一個腳本

(1) 提示用戶輸入一個字符串;

(2) 判斷:

若是輸入的是quit,則退出腳本;

不然,則顯示其輸入的字符串內容;

[root@pks tmp]# bash strip1.sh 
please enter string: quit
quit this strip
[root@pks tmp]# bash strip1.sh 
please enter string: pks
pks
[root@pks tmp]# cat strip1.sh 
#!/bin/bash
#
read -p "please enter string: " -t 10 str
if [ $str == 'quit' ];then
      echo "quit this strip"
      exit 0
else
      echo "$str"
fi
[root@pks tmp]#

十、寫一個腳本,打印2^n表;n等於一個用戶輸入的值;(很差意思,我調皮了)

[root@pks tmp]# bash strip2.sh 
please enter a number then print 2^number 3
2^1=2
2^2=4
2^3=8
[root@pks tmp]# bash strip2.sh 
please enter a number then print 2^number 5
2^1=2
2^2=4
2^3=8
2^4=16
2^5=32
[root@pks tmp]# cat strip2.sh 
read -p "please enter a number then print 2^number " -t 10 n
declare -i muil=1
for (( i=1;i<=n;i++));do
let muil*=2
echo "2^$i=$muil"
done
[root@pks tmp]#

十一、寫一個腳本,寫這麼幾個函數:函數一、實現給定的兩個數值的之和;函數二、取給定兩個數值的最大公約數;函數三、取給定兩個數值的最小公倍數;關於函數的選定、兩個數值的大小都將經過交互式輸入來提供。

    公約數特性:能同時整除若干個數字,最大公約數不會大過若干個數字中最小的數值

    公倍數特性:能被若干數數字同時整除,最小公倍數不會大過若干數字的乘積

  利用以上特性使用循環窮舉的方法

#!/bin/bash
#

sum(){
  read -p "enter two number for man yueshu : " a b
  sum=$[$a+$b]
  echo "sum=$sum"
}

max(){
  read -p "enter two number for man yueshu : " a b
  if [ $a -ge $b ]; then
   for ((i=1;i<=b;i++));do
      if [ $[$a%$i] -eq 0  -a  $[$b%$i] -eq 0 ];then
          echo "$i">/mnt/max.txt
      fi
   done
  fi
  if [ $a -lt $b ]; then
   for ((i=1;i<=a;i++));do
      if [ $[$a%$i] -eq 0  -a  $[$b%$i] -eq 0 ];then
          echo "$i">/mnt/max.txt
      fi
   done
  fi
  cat /mnt/max.txt
}

min(){
   read -p "enter two number for mingongbeishu : " a b
   c=$[$a*$b]
   for ((i=1;i<=c;i++ ));do
    if [ $[$i%$a] -eq 0 -a $[$i%$b] -eq 0  ]; then
        echo "$i"
        break
    fi
   done
}

read -p "enter sum | max | min :  "  string

case $string in
sum)
   sum
   ;;
max)
   max
   ;;
min)
   min
   ;;
*)                                      
   echo """enter sum | max | min :  "
   exit 1
esac
相關文章
相關標籤/搜索