歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> 實用的Linux SHELL面試問題及答案

實用的Linux SHELL面試問題及答案

日期:2017/3/1 9:42:38   编辑:SHELL編程

著之前有關面試的系列文章,讀者的反應比較強烈,所以我決定出一篇有關Linux Shell相關的面試文章,如果對本文有什麼意見或意見的話,歡迎反饋到我的郵箱裡。

如果想要閱讀已發表在Tecmint.com的文章,可以點擊鏈接,鏈接到訪談系列,在這裡我們已經介紹很多題目即文章。,FTP,MySQL和Apache的,腳本,Linux命令等。

Linux Shell腳本的10個有用的“面試問題和解答” http://www.linuxidc.com/Linux/2014-05/102374.htm

Linux Shell參數替換 http://www.linuxidc.com/Linux/2013-06/85356.htm

Shell for參數 http://www.linuxidc.com/Linux/2013-07/87335.htm

Linux/Unix Shell 參數傳遞到SQL腳本 http://www.linuxidc.com/Linux/2013-03/80568.htm

Shell腳本中參數傳遞方法介紹 http://www.linuxidc.com/Linux/2012-08/69155.htm

Shell腳本傳遞命令行參數 http://www.linuxidc.com/Linux/2012-01/52192.htm

實用的shell腳本面試問題

這邊提到的5個面試問題,延續之前的有關Linux面試問題和答案。如果你是Tecmint.com的讀者,你的支持我非常感謝。

1. 寫一個shell腳本來得到當前的日期,時間,用戶名和當前工作目錄。

答案 : 輸出用戶名,當前日期和時間,以及當前工作目錄的命令就是logname,date,who i am和pwd。

現在,創建一個名為userstats.sh文件,將下面的代碼添加到它。

  1. #!/bin/bash
  2. echo "Hello, $LOGNAME"
  3. echo "Current date is `date`"
  4. echo "User is `who i am`"
  5. echo "Current directory `pwd`"

給它添加執行權限,並且執行他。

  1. # chmod 755 userstats.sh
  2. # ./userstats.sh

樣例輸出

  1. Hello, avi
  2. Current date isSatJun713:05:29 IST 2014
  3. Useris avi pts/02014-06-0711:59(:0)
  4. Current directory /home/avi/Desktop

2.寫一個shell腳本,進行兩個數字的相加,如果沒有輸入參數就輸出錯誤信息和一行使用說明

答案 : 下面是簡單的shell腳本以及描述,如果沒有命令行參數,它會拋出錯誤與如何使用腳本的說明。

再創建一個名為twonumbers.sh文件和下面的內容添加到文件裡。

  1. #!/bin/bash
  2. # The Shebang
  3. if[ $# -ne 2 ]
  4. # If two Inputs are not received from Standard Input
  5. then
  6. # then execute the below statements
  7. echo "Usage - $0 x y"
  8. # print on standard output, how-to use the script (Usage - ./1.sh x y )
  9. echo " Where x and y are two nos for which I will print sum"
  10. # print on standard output, “Where x and y are two nos for which I will print sum ”
  11. exit1
  12. # Leave shell in Error Stage and before the task was successfully carried out.
  13. fi
  14. # End of the if Statement.
  15. echo "Sum of $1 and $2 is `expr $1 + $2`"
  16. # If the above condition was false and user Entered two numbers as a command Line Argument,
  17. it will show the sum of the entered numbers.

給他添加可執行權限,並且執行。

  1. # chmod 755 two-numbers.sh

情形一: 未輸入兩個數字作為命令行參數運行腳本,你將得到下面的輸出。

樣例輸出

  1. # ./two-numbers.sh
  2. Usage-./two-numbers.sh x y
  3. Where x and y are two nos for which I will print sum

情形二: 當數字存在時,你會得到如圖所示的結果。

  1. $ ./two-numbers.sh 45
  2. Sum of 4and5is9

因此,上述shell腳本滿足了問題的要求。

3.你需要打印一個給定的數字的反序,如輸入10572,輸出27501,如果沒有輸入數據,應該拋出錯誤和使用腳本說明。在此之前,告訴我你需要在這裡使用的算法。

算法

  1. 輸入的數字為n
  2. 賦值 rev=0, sd=0 (反向和單個數字設置為0)
  3. n % 10, 將得到最左邊的數字
  4. 反向數字可以用這個方法生成 rev * 10 + sd
  5. 對輸入數字進行右位移操作(除以10)
  6. 如果n > 0, 進入第三步,否則進行第七步
  7. 輸出rev

現在,創建一個名為`numbers.sh`文件,並添加以下代碼。

  1. #!/bin/bash
  2. if[ $# -ne 1 ]
  3. then
  4. echo "Usage: $0 number"
  5. echo " I will find reverse of given number"
  6. echo " For eg. $0 0123, I will print 3210"
  7. exit1
  8. fi
  9. n=$1
  10. rev=0
  11. sd=0
  12. while[ $n -gt 0]
  13. do
  14. sd=`expr $n % 10`
  15. rev=`expr $rev \* 10 + $sd`
  16. n=`expr $n / 10`
  17. done
  18. echo "Reverse number is $rev"

授予對文件的執行權限,並運行如下所示的腳本。

  1. # chmod 755 numbers.h

情形一: 當輸入不包含命令行參數,你將得到下面的輸出。

樣例輸出

  1. ./numbers.sh
  2. Usage:./numbers.sh number
  3. I will find reverse of given number
  4. For eg../2.sh123, I will print321

情形二: 正常輸入

  1. $ ./numbers.sh 10572
  2. Reverse number is27501

上面的腳本非常完美,輸出正是我們需要的。

4. 你應該直接用終端,而不是依靠任何shell腳本來進行實數計算。你會怎麼做(比如實數7.56+2.453)?

答案 : 我們需要用如下所述的特殊方式使用bc命令。將7.56+2.453作為輸入通過管道進入bc中。

  1. $ echo 7.56+2.453| bc
  2. 10.013

5. 你需要給出圓周率的值,精度為小數點後100位,什麼是最簡單的方法。

答案 : 找圓周率的值最簡單的方法,我們只是需要發出以下命令。

  1. # pi 100
  2. 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067

很明顯!安裝我們必須有包pi。只用一個aptyum命令,就能獲得所需的軟件包,同時用最簡單方法來實現這個需求。

就是這樣。我會很快在Tecmint.com發表另一個有趣的文章。至此敬請關注。別忘了向我們提供您在的評論和反饋。

Copyright © Linux教程網 All Rights Reserved