馬哥教育網絡班21期-第九周課程練習

第九周作業

1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現;

#!/bin/bash
#
declare -i nologin=0
declare -i other=0

    for i in $(cut -d: -f7 /etc/passwd);do
        if [ $i == /sbin/nologin ]; then
            let nologin++
        else
            let other++
        fi
    done

echo "nologin user numer:$nologin"
echo "other user number:$other"
~  

測試:
[root@qq scripts]# bash -x 9-1.sh 
+ declare -i nologin=0
+ declare -i other=0
++ cut -d: -f7 /etc/passwd
+ for i in '$(cut -d: -f7 /etc/passwd)'
+ '[' /bin/bash == /sbin/nologin ']'
+ let other++
+ for i in '$(cut -d: -f7 /etc/passwd)'
+ '[' /sbin/nologin == /sbin/nologin ']'
+ let nologin++
+ for i in '$(cut -d: -f7 /etc/passwd)'
+ '[' /sbin/nologin == /sbin/nologin ']'
+ let nologin++
……
+ echo 'nologin user numer:26'
nologin user numer:26
+ echo 'other user number:10'
other user number:10

2、寫一個腳本

  • (1) 獲取當前主機的主機名,保存于hostname變量中;

  • (2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com;

  • (3) 否則,則顯示當前主機名;

    #!/bin/bash
    
    hostname=$(hostname)
    
    if [ $hostname == 'localhost' ];then
        hostname www.magedu.com && echo "hostname change:'www.magedu.com'"
    else
        echo 'hostname is:' $hostname
    fi
    
    測試:
    [root@qq scripts]# bash -x 9-2.sh 
    ++ hostname
    + hostname=qq.localdomain
    + '[' qq.localdomain == localhost ']'
    + echo 'hostname is:' qq.localdomain
    hostname is: qq.localdomain
    [root@qq scripts]#

3、寫一個腳本,完成如下功能

  • (1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在;

  • (2) 如果存在,則顯示此設備上的所有分區信息;

    #!/bin/bash
    #
    
    read -p "enter dev path:" dev
    
    if [ -e $dev ];then
        parted $dev print
    else
        echo "This dev is not exist"
    fi
    
    測試:
    [root@qq scripts]# bash -x 9-3.sh 
    + read -p 'enter dev path:' dev
    enter dev path:/dev/sda
    + '[' -e /dev/sda ']'
    + parted /dev/sda print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sda: 268GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    
    Number  Start   End     Size    Type      File system     Flags
     1      1049kB  211MB   210MB   primary   ext4            boot
     2      211MB   52.6GB  52.4GB  primary   ext4
     3      52.6GB  94.6GB  41.9GB  primary   ext4
     4      94.6GB  268GB   174GB   extended
     5      94.6GB  131GB   36.7GB  logical   ext4
     6      131GB   152GB   21.0GB  logical   ext4
     7      152GB   168GB   15.7GB  logical   ext4
     8      168GB   184GB   15.7GB  logical   ext4
     9      184GB   194GB   10.5GB  logical   ext4
    10      194GB   198GB   4295MB  logical   linux-swap(v1)
    
    [root@qq scripts]#

4、寫一個腳本,完成如下功能

  • 腳本能夠接受一個參數;

  • (1) 如果參數1為quit,則顯示退出腳本,并執行正常退出;

  • (2) 如果參數1為yes,則顯示繼續執行腳本;

  • (3) 否則,參數1為其它任意值,均執行非正常退出;

    #!/bin/bash
    #
    
    if [ $# -le 0 ];then
        echo "usage:bash 9-4.sh quit|yes|other"
        exit 2
    else
        case $1 in
        quit)
        echo "quiting"
        exit 0;;
        yes)
        echo "conutine";;
        *)
        echo "error..."
        exit 1
        esac
    fi
    測試:
    [root@qq scripts]# bash 9-4.sh 
    usage:bash 9-4.sh quit|yes|other
    [root@qq scripts]# 
    [root@qq scripts]# 
    [root@qq scripts]# bash 9-4.sh quit
    quiting
    [root@qq scripts]# bash 9-4.sh yes
    conutine
    [root@qq scripts]# bash 9-4.sh d
    error...

5、寫一個腳本,完成如下功能

  • 傳遞一個參數給腳本,此參數為gzip、bzip2或者xz三者之一;

  • (1) 如果參數1的值為gzip,則使用tar和gzip歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.gz;

  • (2) 如果參數1的值為bzip2,則使用tar和bzip2歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.bz2;

  • (3) 如果參數1的值為xz,則使用tar和xz歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.xz;

  • (4) 其它任意值,則顯示錯誤壓縮工具,并執行非正常退出;

    #!/bin/bash
    #
    if [ $# -le 0 ]; then
        echo "usage:bash 9-5.sh gzip|bzip2|xz|..."
        exit 2
    else
        case $1 in
        gzip)
        tar czvf /backups/etc-20160613.tar.gz /etc;;
        bzip2)
        tar cjvf /backups/etc-20160613.tar.bz2 /etc;;
        xz)
        tar cJvf /backups/etc-20160613.tar.xz /etc;;
        *)
        echo "error..."
        exit 1;;
        esac
    fi
    測試:
    [root@qq scripts]# bash  9-5.sh xz
    [root@qq scripts]# ls /backups/
    etc-20160613.tar.xz
    [root@qq scripts]#

6、寫一個腳本,接受一個路徑參數:

  • (1) 如果為普通文件,則說明其可被正常訪問;

  • (2) 如果是目錄文件,則說明可對其使用cd命令;

  • (3) 如果為符號鏈接文件,則說明是個訪問路徑;

  • (4) 其它為無法判斷;

    #!/bin/bash
    #
    if [ $# -le 0 ];then
        echo "Usage:bash 9-6.sh /PATH/FILE."
        exit 2
    fi
    
    if [ -h $1 ]; then
        echo "This is accessable path."
    elif [ -d $1 ]; then
        echo "This is directory command 'cd' can be use."
    elif [ -f $1 ]; then
        echo "This file can be access."
    else
        echo "Unknow File."
    fi
    
    測試:
    [root@qq scripts]# bash 9-6.sh /tmp/2.txt 
    This is accessable path.
    [root@qq scripts]# bash 9-6.sh /tmp/1.txt 
    This file can be access.
    [root@qq scripts]# bash 9-6.sh /tmp/
    This is directory command 'cd' can be use.

7、寫一個腳本,取得當前主機的主機名,判斷

  • (1) 如果主機名為空或為localhost,或為"(none)",則將其命名為mail.magedu.com;

  • (2) 否則,顯示現有的主機名即可;

    #!/bin/bash
    #
    HostName=`hostname`
    if [ -z "HostNmae" -o "$HostName" == "localhost" -o "$HostName" == "(none)" ]; then
        hostname mail.magedu.com
    else
        echo "$HostName"
    fi
    測試:
    [root@qq scripts]# bash 9-7.sh 
    qq.localdomain

8、寫一腳本,接受一個用戶名為參數;

  • (1) 如果用戶的id號為0,則顯示其為管理員;

  • (2) 如果用戶的id號大于0且小于500, 則顯示其為系統用戶;

  • (3) 否則,則顯示其為普通用戶;

    #!/bin/bash
    #
    
    read -p "Please enter one user:" user
    uid=`id -u $user`
    
    if [ $uid -eq 0 ]; then
        echo "$user is admin."
    elif [ $uid -gt 0 -a $uid -lt 500 ]; then
        echo "$user is system user."
    else
        echo "$user is common user."
    fi
    測試:
    [root@qq scripts]# bash 9-8.sh 
    Please enter one user:root
    root is admin.
    [root@qq scripts]# bash 9-8.sh
    Please enter one user:mysql
    mysql is system user.
    [root@qq scripts]# bash 9-8.sh
    Please enter one user:centos
    centos is common user.

10、寫一個腳本,傳遞一個用戶名參數給腳本;

  • (1) 如果用戶的id號大于等于500,且其默認shell為以sh結尾的字符串,則顯示“a user can log system.”類的字符串;

  • (2) 否則,則顯示無法登錄系統;

    #!/bin/bash
    #
    if [ $# -le 0 ]; then
        echo "Usage:bash 9-10.sh username"
        exit 2
    fi
    
    if id $1 &> /dev/null; then
        userid=`grep ^$1 /etc/passwd | grep sh$ | cut -d: -f3`
        if [ $userid -ge 500 ]; then
        echo "a user can login system."
        else
        echo "a user can not login system."
        fi
    fi
    測試:
    [root@qq scripts]# bash 9-10.sh root
    a user can not login system.
    [root@qq scripts]# bash 9-10.sh gg
    a user can login system.
    [root@qq scripts]# cat /etc/passwd | grep gg
    gg:x:603:603::/home/gg:/bin/sh

11、寫一個腳本,完成如下任務 :

  • (1) 按順序分別復制/var/log目錄下的每個直接文件或子目錄至/tmp/test1-testn目錄中;

  • (2) 復制目錄時,才使用cp -r命令;

  • (3) 復制文件時使用cp命令;

  • (4) 復制鏈接文件時使用cp -d命令;

  • (5) 余下的所有類型,使用cp -a命令;

    #!/bin/bash
    dir="/var/log/"
    des="/tmp/test1-testn"
    mkdir $des
    for i in `ls $dir`; do
        if [ -c $i ]; then
            cp -r $dir$i $des/
        elif [ -f $i ]; then
            cp $dir$i $des/
        elif [ -h $i ]; then
            cp -d $dir$i $des/
        else
            cp -a $dir$i $des/
        fi
    done

原創文章,作者:Net21_仲樂,如若轉載,請注明出處:http://www.www58058.com/45878

(2)
Net21_仲樂Net21_仲樂
上一篇 2016-09-19 13:48
下一篇 2016-09-19 13:48

相關推薦

  • Linux下的SSH端口轉發

    通常情況下兩個不同的網絡之間總會開放某一些特定的端口用于通訊使用,而SSH所使用的22端口通常就在開放之列。基于SSH的端口轉發就是利用SSH作為中間的代理,達到繞過兩個網絡之間的限制,順利的進行任意的端口的訪問。端口轉發可以分為三種,正向端口轉發,反向端口轉發和動態端口轉發。為了演示這三種端口轉發方式的用法我們先假設存在有2個網域Office和Prod,在…

    Linux干貨 2015-02-09
  • python練習實例

    #依次輸出五位數的每一位(由低位到高位)i=12345for a in range(5):j=i%10i=i//10print(j) #依次輸出五位數的每一位(由高位到低位)i=12345for a in range(5,0,-1):j=i//10**(a-1)i=i%10**(a-1)print(j) #打印菱形for i in range(-3,4):j…

    Linux干貨 2018-03-25
  • 文本處理工具-習題

    1 、找出ifconfig 命令結果中本機的所有IPv4地址 [root@centos7 ~]# ifconfig |head -2 |tail-1 |cut -dn -f2 |cut -d" " -f2 2 、查出分區空間使用率的最大百分比值 [root@centos7 ~]# df |cut -c44-46 |sort -n|tail…

    Linux干貨 2016-08-15
  • Linux基礎之RAID

    一.RAID介紹 RAID剛開始出現的時候叫做廉價磁盤冗余陣列(Redundant Array of Inexpensive Disks),但在當時磁盤并非像現在這么便宜,反而這種組合方式使得代價非常昂貴,所以后來改名為獨立磁盤冗余陣列(Redundant Array of Independent Disks),基本的思想就是組合組合多個便宜的,性能相對較低…

    Linux干貨 2016-11-08
  • lvs集群學習筆記之原理

    lvs集群學習筆記之原理 lvs集群學習筆記之原理 集群 負載均衡 lvs 原理 lvs集群學習筆記之原理 什么是集群 什么是負載均衡 負載均衡解決方案 lvs簡介 lvs內核空間模型 lvs特點 lvs實現方式 lvs實現方式之nat模型 lvs實現方式之dr lvs實現方式之tun lvs實現方式之FULLNAT lvs之算法 靜態算法   &…

    Linux干貨 2017-01-03
  • shell三劍客之grep

    正則表達式是對字符串操作的一種邏輯公式,就是用事先定義好的一些特定字符、及這些特定字符的組合,組成一個“規則字符串”,這個“規則字符串”用來表達對字符串的一種過濾邏輯。 給定一個正則表達式和另一個字符串,我們可以達到如下的目的: 1. 給定的字符串是否符合正則表達式的過濾邏輯(稱作“匹配”); 2. 可以通過正則表達式,從字符串中獲取我們想要的特定部分。 正…

    Linux干貨 2016-08-08

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-09-19 18:14

    寫腳本的時候,不要用系統的函數名作為變量名

欧美性久久久久