1. 程式人生 > 實用技巧 >第四章面試題

第四章面試題

第四章-面試練習題參考答案.md

第四章 面試練習題

解答論述題

1. Linux 系統中標準輸入,標準輸出,標準錯誤輸出的檔案描述符是什麼?
通常標準輸入的檔案描述符為 0,標準輸出為 1, 標準錯誤輸出為 2,如下:
[root@localhost ~]<span class="hljs-comment"># </span>
[root@localhost ~]<span class="hljs-comment"># ls -al /dev/std*</span>
lrwxrwxrwx. 1 root root 15 Jan  3 18:45 /dev/stderr -&gt; /proc/self/fd/2
lrwxrwxrwx. 1 root root 15 Jan  3 18:45 /dev/stdin -&gt; /proc/self/fd/0
lrwxrwxrwx. 1 root root 15 Jan  3 18:45 /dev/stdout -&gt; /proc/self/fd/1
[root@localhost ~]<span class="hljs-comment">#</span>
  1. Linux 系統中重定向符號 > 和 >> 的區別是什麼?

    > 表示覆蓋重定向,若被輸入的檔案存在切包含內容,則會直接重新覆蓋。
    >> 表示追加重定向,若被輸入的檔案存在切包含內容,則會保留原始檔內容,在該檔案
    尾部追加新的內容,如下:

    [root@localhost ~]#
    [root@localhost ~]# ls
    file
    [root@localhost ~]#
    [root@localhost ~]# cat file
    111
    [root@localhost ~]#
    [root@localhost ~]# echo 222 > file
    [root@localhost ~]#
    [root@localhost ~]# cat file
    222
    [root@localhost ~]#
    [root@localhost ~]# echo 333 >> file
    [root@localhost ~]#
    [root@localhost ~]# cat file
    222
    333
    [root@localhost ~]#
    [root@localhost ~]#

  2. 請說出以下幾個重定向符號的含義
    >
    2>
    &>
    2>&1
    1>&2

    符號 > 表示標準輸出重定向, 符號 2> 表示標準錯誤重定向,下午我們演示一下:
    [root@localhost ~]#
    [root@localhost ~]# ls -al / > /root/file
    [root@localhost ~]#
    [root@localhost ~]# cat /root/file
    total 108
    dr-xr-xr-x. 24 root root 4096 Jan 3 19:51 .
    dr-xr-xr-x. 24 root root 4096 Jan 3 19:51 ..
    -rw-r--r--. 1 root root 0 Jan 3 18:45 .autofsck
    dr-xr-xr-x. 2 root root 4096 Dec 21 19:50 bin
    dr-xr-xr-x. 5 root root 4096 Dec 21 19:51 boot
    drwxr-xr-x. 3 root root 4096 Jan 3 19:46 data
    drwxr-xr-x. 19 root root 3740 Jan 3 18:45 dev
    drwxr-xr-x. 63 root root 4096 Jan 3 22:34 etc
    drwxr-xr-x. 3 root root 4096 Jan 3 22:34 home
    dr-xr-xr-x. 8 root root 4096 Dec 21 19:50 lib
    dr-xr-xr-x. 9 root root 12288 Dec 21 19:50 lib64
    drwx------. 2 root root 16384 Dec 21 19:49 lost+found
    drwxr-xr-x. 2 root root 4096 Sep 23 2011 media
    drwxr-xr-x. 2 root root 4096 Sep 23 2011 mnt
    drwxr-xr-x. 2 root root 4096 Sep 23 2011 opt
    dr-xr-xr-x. 94 root root 0 Jan 3 18:45 proc
    dr-xr-x---. 2 root root 4096 Jan 3 22:35 root
    dr-xr-xr-x. 2 root root 12288 Dec 21 19:50 sbin
    drwxr-xr-x. 7 root root 0 Jan 3 18:45 selinux
    drwxr-xr-x. 2 root root 4096 Sep 23 2011 srv
    drwxr-xr-x 13 root root 0 Jan 3 18:45 sys
    drwxr-xr-x. 4 root root 4096 Jan 3 19:51 testdir
    drwxrwxrwt. 3 root root 4096 Jan 3 20:26 tmp
    drwxr-xr-x. 13 root root 4096 Dec 21 19:49 usr
    drwxr-xr-x. 17 root root 4096 Jan 3 19:35 var
    [root@localhost ~]#
    [root@localhost ~]# ls -al /uuu
    ls: cannot access /uuu: No such file or directory
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# ls -al /uuu > /root/file
    ls: cannot access /uuu: No such file or directory
    [root@localhost ~]#
    [root@localhost ~]# cat /root/file
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# ls -al /uuu 2> /root/file
    [root@localhost ~]#
    [root@localhost ~]# cat /root/file
    ls: cannot access /uuu: No such file or directory
    [root@localhost ~]#
    [root@localhost ~]#

    &> 符號表示將標準錯誤輸出和標準輸出同時輸出指定檔案中,如下:
    [root@localhost ~]#
    [root@localhost ~]# ls /uuu &> file
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# cat file
    ls: cannot access /uuu: No such file or directory
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# ls -ald / &> file
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# cat file
    dr-xr-xr-x. 24 root root 4096 Jan 3 19:51 /
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]#

    可以同時指定一個程式的標出輸出檔案和標準錯誤輸出檔案,如下:
    [root@localhost ~]#
    [root@localhost ~]# ls -ald / > file 2> error
    [root@localhost ~]#
    [root@localhost ~]# ll
    total 4
    -rw-r--r--. 1 root root 0 Jan 3 23:19 error
    -rw-r--r--. 1 root root 45 Jan 3 23:19 file
    [root@localhost ~]#
    [root@localhost ~]# cat file
    dr-xr-xr-x. 24 root root 4096 Jan 3 19:51 /
    [root@localhost ~]#
    [root@localhost ~]# cat error
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# ls -ald /uuu > file 2> error
    [root@localhost ~]#
    [root@localhost ~]# cat file
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# cat error
    ls: cannot access /uuu: No such file or directory
    [root@localhost ~]#

    在同時指定檔案的標準輸出和標準錯誤輸出時,可以使用 & 符號進行合併同一,如下:
    [root@localhost ~]#
    [root@localhost ~]# ls -ald / > file 2>&1
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# cat file
    dr-xr-xr-x. 24 root root 4096 Jan 3 19:51 /
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# ls -ald /uuu > file 2>&1
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# cat file
    ls: cannot access /uuu: No such file or directory
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# ls -ald / 2> error 1>&2
    [root@localhost ~]#
    [root@localhost ~]# cat error
    dr-xr-xr-x. 24 root root 4096 Jan 3 19:51 /
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# ls -ald /uuu 2> error 1>&2
    [root@localhost ~]#
    [root@localhost ~]# cat error
    ls: cannot access /uuu: No such file or directory
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]#

  3. Linux 系統中管道的作用是什麼?
    將前一個命令的標準輸出當做後一個命令的標準輸入。

  4. 將/etc/issue檔案中的內容轉換為大寫後儲存至/tmp/issue.out檔案中

    使用 tr 命令進行字元大小寫替換即可完成該題目:
    [root@localhost ~]#
    [root@localhost ~]# cat /etc/issue
    CentOS release 6.10 (Final)
    Kernel \r on an \m

    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# cat /etc/issue | tr 'a-z' 'A-Z' > /tmp/issue.out
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# cat /tmp/issue.out
    CENTOS RELEASE 6.10 (FINAL)
    KERNEL \R ON AN \M

    [root@localhost ~]#
    [root@localhost ~]#

  5. 將當前系統登入使用者的資訊轉換為大寫後儲存至/tmp/who.out檔案中

    本題和上一題類似,我們依舊使用 tr 命令完成轉換,下面我們使用另外一種字元大小寫的表示形式完成本題:
    [root@localhost ~]#
    [root@localhost ~]# who
    root pts/0 2020-01-03 22:17 (192.168.7.1)
    root pts/1 2020-01-03 22:21 (192.168.7.1)
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# who | tr '[:lower:]' '[:upper:]' > /tmp/who.out
    [root@localhost ~]#
    [root@localhost ~]# cat /tmp/who.out
    ROOT PTS/0 2020-01-03 22:17 (192.168.7.1)
    ROOT PTS/1 2020-01-03 22:21 (192.168.7.1)
    [root@localhost ~]#
    [root@localhost ~]#

  6. 一個 Linux 使用者給 root 發郵件,要求郵件標題為 hello ,郵件正文格式模板如下:
    Hello, I am 使用者名稱,The system version is here,please help me to check it ,thanks!
    作業系統版本資訊

    本題我們可以使用 ( ) 順序執行幾個簡單命令,然後將執行的結果作為輸入傳給 mail 命令完成此題:
    [root@localhost ~]#
    [root@localhost ~]# (echo "Hello, I am whoami.";echo "The system version is here, please help me to check it, thanks!";echo cat /etc/redhat-release) | mail -s hello root
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# mail
    Heirloom Mail version 12.4 7/29/08. Type ? for help.
    "/var/spool/mail/root": 1 message 1 new
    >N 1 root Sat Jan 4 00:27 24/953 "hello"
    &
    Message 1:
    From [email protected] Sat Jan 4 00:27:59 2020
    Return-Path: <[email protected]>
    X-Original-To: [email protected]
    Delivered-To: [email protected]
    From: root <[email protected]>
    Date: Sat, 04 Jan 2020 00:27:58 +0800
    To: [email protected]
    Subject: hello
    User-Agent: Heirloom mailx 12.4 7/29/08
    Content-Type: text/plain; charset=us-ascii
    Status: R

     Hello, I am root.
     The system version is here, please <span class="hljs-built_in">help</span> me to check it, thanks\!
     CentOS release 6.10 (Final)
    
     &amp; q
     Held 1 message <span class="hljs-keyword">in</span> /var/spool/mail/root
     [root@localhost ~]<span class="hljs-comment"># </span>
     [root@localhost ~]<span class="hljs-comment">#</span>
    
  7. 將 / 下檔案列表,顯示成一行,檔名之間使用一個空格隔開

    使用 tr 命令進行替換
    [root@localhost ~]#
    [root@localhost ~]# ls -1 / | tr "\n" " "
    bin boot dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]#

    或者

    [root@localhost ~]#
    [root@localhost ~]# ls / | tr "\n" " "
    bin boot dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]#

  8. 請寫出使用 bc 計算器計算 1 到 100 中所有數字的總和是多少?

    使用 { } 生成序列,然後使用 tr 替換字元,最後使用 bc 運算
    [root@localhost ~]# echo {1..100} | tr " " "+" | bc
    5050
    [root@localhost ~]#

    也可以使用 seq 命令直接生成字元序列,然後交由 bc 處理
    [root@localhost ~]#
    [root@localhost ~]# seq -s "+" 100 | bc
    5050
    [root@localhost ~]#
    [root@localhost ~]#

  9. 在 window 系統中建立的文字檔案中,換行符顯示為 ^M, 請問如何將其刪除?

    windows 建立的檔案比 Linux 系統建立的檔案多一個 \r, 我們使用 tr 將他刪除即可:
    [root@localhost ~]#
    [root@localhost ~]# ll
    total 4
    -rw-r--r--. 1 root root 20 Jan 4 01:04 test.txt
    [root@localhost ~]#
    [root@localhost ~]# cat -A test.txt
    hello^M$
    gordon^M$
    linux[root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# hexdump -c test.txt
    0000000 h e l l o \r \n g o r d o n \r \n l
    0000010 i n u x
    0000014
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# cat testLinux.txt
    hello
    gordon
    linux
    [root@localhost ~]#
    [root@localhost ~]# cat -A testLinux.txt
    hello$
    gordon$
    linux$
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# hexdump -c testLinux.txt
    0000000 h e l l o \n g o r d o n \n l i n
    0000010 u x \n
    0000013
    [root@localhost ~]#

    操作如下:
    [root@localhost ~]# hexdump -c test.txt
    0000000 h e l l o \r \n g o r d o n \r \n l
    0000010 i n u x
    0000014
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# cat test.txt | tr -d "\r" | hexdump -c
    0000000 h e l l o \n g o r d o n \n l i n
    0000010 u x
    0000012
    [root@localhost ~]#
    [root@localhost ~]#

  10. 處理字串 "xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4",只保留其中的數字和空格

    使用 tr 命令刪除 數字和空格意外的字元即可,使用 -c 表示取 set1 的補集:
    [root@localhost ~]#
    [root@localhost ~]# echo "xt.,l 1 jr#-cmn 2 c*/fe 3 uz 4" | tr -dc '[:digit:][:space:]'
    1 2 3 4
    [root@localhost ~]#
    [root@localhost ~]#

    還可以使用 tr 直接刪除標點符號和字母實現:
    [root@localhost ~]#
    [root@localhost ~]# echo "xt.,l 1 jr#-cmn 2 c*/fe 3 uz 4" | tr -d '[:punct:][:alpha:]'
    1 2 3 4
    [root@localhost ~]#
    [root@localhost ~]#

  11. 將PATH變數每個目錄顯示在獨立的一行

    此題依舊使用 tr 命令對 PATH 值中的 : 進行替換即可完成:

    [root@localhost ~]#
    [root@localhost ~]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# echo $PATH | tr ":" "\n"
    /usr/local/sbin
    /usr/local/bin
    /sbin
    /bin
    /usr/sbin
    /usr/bin
    /root/bin
    [root@localhost ~]#
    [root@localhost ~]#

  12. 將文字 test.txt 中的字元按照對映關係轉換,0 到 9 一共十個數字分別對於轉換為 a 到 j

    [root@localhost ~]# cat test.txt
    hello 0123456789 world

    13579

    02468
    [root@localhost ~]#
    [root@localhost ~]#

    [root@localhost ~]#
    [root@localhost ~]# cat test.txt | tr "0-9" "a-j"
    hello abcdefghij world

    bdfhj

    acegi
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]#