第九周作業

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

#!/bin/bash
declare -i sum_login=0
declare -i sum_nologin=0
while read line;do
    shell=$(echo $line|awk -F: '{print $NF}')
    if [ "$shell" == "/sbin/nologin" ];then
        let sum_nologin++
    else
        let sum_login++
    fi
done </etc/passwd
echo "Can login: $sum_login"
echo "Cannot login: $sum_nologin"
執行結果:
[root@centos6 script]# ./chkuser.sh   
Can login: 8
Cannot login: 17

2、寫一個腳本
    (1) 獲取當前主機的主機名,保存于hostname變量中;
    (2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com;

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

#!/bin/bash
hostname=$(hostname)
if [ "$hostname" == "localhost" ];then
    hostname www.magedu.com
else
    hostname
fi
[root@centos6 script]# ./chhn.sh 
www.magedu.com

3、寫一個腳本,完成如下功能
    (1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1"
    exit 1
fi

if [ -b $1 ];then
    fdisk -l $1
else
    echo "Invalid file!"
fi
執行結果:
[root@centos6 script]# ./devinfo.sh /dev/tty0
Invalid file!
[root@centos6 script]# ./devinfo.sh /dev/sda1

Disk /dev/sda1: 524 MB, 524288000 bytes
255 heads, 63 sectors/track, 63 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

4、寫一個腳本,完成如下功能
   腳本能夠接受一個參數;
   (1) 如果參數1為quit,則顯示退出腳本,并執行正常退出;
   (2) 如果參數1為yes,則顯示繼續執行腳本;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1"
    exit 1
fi

while true;do
    case $1 in
        quit)
            echo "Exit in 2 second..."
            sleep 2
            exit 0
            ;;
        yes)
            echo "Continue..."
            break
            ;;
        *)
            echo "Unknown error,exit!"
            exit 1
    esac
done
echo "Execute complete!"
執行結果:
[root@centos6 script]# ./argu.sh 
Usage ./argu.sh arg1
[root@centos6 script]# ./argu.sh quit
Exit in 2 second...
[root@centos6 script]# ./argu.sh yes
Continue...
Execute complete!
[root@centos6 script]# ./argu.sh haha
Unknown error,exit!

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 [ $# -ne 1 ];then
    echo "Usage $0 arg1."
    exit 1
fi

[ -d /backups ] || mkdir -p /backups
case $1 in
    gzip)
        tar -zcf /backups/etc-$(date +%Y%m%d).tar.gz /etc
        ;;
    bzip2)
        tar -jcf /backups/etc-$(date +%Y%m%d).tar.bz2 /etc
        ;;
    xz)
        tar Jcf /backups/etc-$(date +%Y%m%d).tar.xz /etc
        ;;
    *)
        echo "Invalid compress mode!"
        exit 1
esac
執行結果:
[root@centos6 script]# ll /backups
total 22904
-rw-r--r-- 1 root root 8395375 Feb 13 19:34 etc-20170213.tar.bz2
-rw-r--r-- 1 root root 9527374 Feb 13 19:32 etc-20170213.tar.gz
-rw-r--r-- 1 root root 5523792 Feb 13 19:34 etc-20170213.tar.xz

6、寫一個腳本,接受一個路徑參數:
   (1) 如果為普通文件,則說明其可被正常訪問;
   (2) 如果是目錄文件,則說明可對其使用cd命令;
   (3) 如果為符號鏈接文件,則說明是個訪問路徑;

   (4) 其它為無法判斷;

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1."
    exit 1
fi

if [ -L $1 ];then
    echo "$1 is symbolic file."
elif [ -f $1 ];then
    echo "$1 is regular file,you can read and write it."
elif [ -d $1 ];then
    echo "$1 is directory,you can use the command cd to enter it."
else
    echo "Unknown file type!"
fi
執行結果:
[root@www script]# ./chktype.sh /etc/hosts
/etc/hosts is regular file,you can read and write it.
[root@www script]# ./chktype.sh /etc/redhat-release 
/etc/redhat-release is symbolic file.
[root@www script]# ./chktype.sh /etc/
/etc/ is directory,you can use the command cd to enter it.
[root@www script]# ./chktype.sh ddd
Unknown file type!

7、寫一個腳本,取得當前主機的主機名,判斷
   (1) 如果主機名為空或為localhost,或為”(none)”,則將其命名為mail.magedu.com;

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

#!/bin/bash
if [ -z "$(hostname)" ] || [ "$(hostname)" == "localhost" ];then
    hostname mail.magedu.com
    echo "set 'mail.magedu.com' as new hostname."
else
    hostname
fi
執行結果:
[root@www script]# ./sethn.sh 
set 'mail.magedu.com' as new hostname.
[root@www script]# hostname
mail.magedu.com

8、寫一腳本,接受一個用戶名為參數;
   (1) 如果用戶的id號為0,則顯示其為管理員;
   (2) 如果用戶的id號大于0且小于500, 則顯示其為系統用戶;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1"
    exit 1
fi

if ! id $1 &>/dev/null;then
    echo "$1 not exists!"
    exit 1
fi

uid=$(id -u $1)
if [ $uid -ge 500 ];then
    echo "$1 is regular user."
elif [ $uid -gt 0 ];then
    echo "$1 is system user."
else
    echo "$1 is root."
fi
執行結果:
[root@www script]# ./chkuser2.sh magedu
magedu is regular user.
[root@www script]# ./chkuser2.sh mysql
mysql is system user.
[root@www script]# ./chkuser2.sh root
root is root.
[root@www script]# ./chkuser2.sh hahaha
hahaha not exists!

10、寫一個腳本,傳遞一個用戶名參數給腳本;
   (1) 如果用戶的id號大于等于500,且其默認shell為以sh結尾的字符串,則顯示“a user can log system.”類的字符串;

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 arg1."
    exit 1
fi

if ! id $1 &>/dev/null;then
    echo "$1 not exists!"
    exit 1
fi

uid=$(id -u $1)
if [ $uid -ge 500 ] && grep "^$1.*sh$" /etc/passwd &>/dev/null;then
    echo "$1 can login."
else
    echo "$1 cannot login."
fi
執行結果:
[root@www script]# ./chkuser3.sh magedu
magedu can login.
[root@www script]# ./chkuser3.sh nfc
nfc cannot login.

11、寫一個腳本,完成如下任務 :
   (1) 按順序分別復制/var/log目錄下的每個直接文件或子目錄至/tmp/test1-testn目錄中;
   (2) 復制目錄時,才使用cp -r命令;
   (3) 復制文件時使用cp命令;
   (4) 復制鏈接文件時使用cp -d命令;

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

#!/bin/bash
declare -i j=1
for i in /var/log/*;do
    [ ! -d /tmp/test$j ] && mkdir -p /tmp/test$j
    if [ -L $i ];then
        cp -d $i /tmp/test$j
    elif [ -f $i ];then
        cp $i /tmp/test$j
    elif [ -d $i ];then
        cp -r $i /tmp/test$j
    else
        cp -a $i /tmp/test$j
    fi
    let j++
done
執行結果:
[root@www tmp]# tree
.
├── test1
│   └── anaconda.ifcfg.log
├── test10
│   └── btmp
├── test11
│   └── ConsoleKit
│       └── history
├── test12
│   └── cron
├── test13
│   └── dmesg
├── test14
│   └── dmesg.old
├── test15
│   └── dracut.log
├── test16
│   └── lastlog
├── test17
│   └── maillog
├── test18
│   └── messages
├── test19
│   └── mysqld.log
├── test2
│   └── anaconda.log
├── test20
│   └── secure
├── test21
│   └── spooler
├── test22
│   └── tallylog
├── test23
│   └── wtmp
├── test24
│   └── yum.log
├── test3
│   └── anaconda.program.log
├── test4
│   └── anaconda.storage.log
├── test5
│   └── anaconda.syslog
├── test6
│   └── anaconda.xlog
├── test7
│   └── anaconda.yum.log
├── test8
│   └── audit
│       └── audit.log
└── test9
    └── boot.log

原創文章,作者:N26-西安-方老喵,如若轉載,請注明出處:http://www.www58058.com/70445

(0)
N26-西安-方老喵N26-西安-方老喵
上一篇 2017-03-05
下一篇 2017-03-05

相關推薦

  • LNMP

    1、源碼編譯安裝LNMP架構環境 OS版本:2.6.32-431.el6.x86_64 Nginx版本:nginx-1.6.1 mariadb版本:mariadb-10.0.13 php版本:php-5.4.26 1、安裝編譯安裝所需系統環境 ~]# yum groupinstall "Development Tools" "S…

    Linux干貨 2017-02-09
  • lvs

    1、什么是LVS LVS是Linux Virtual Server的簡寫,以為Linux虛擬服務器,是一個虛擬服務器集群,其具有很好的可伸縮性、可靠性、可管理性。LVS集群采用IP負載均衡技術和基于內容請求分發技術。調度器具有很好的吞吐率,將請求均衡地轉移到不同的服務器上執行,且調度器自動屏蔽掉服務器的故障,從而將一組服務器構成一個高性能的、高可用的虛擬服務…

    Linux干貨 2016-10-29
  • systemd

    systemd的新特性(centos 7) 系統引導時實現服務并行啟動 按需激活進程,在此之前可以讓進程處于半活動狀態 系統狀態快照:能夠自我保存當前系統上沒一個用戶空間的進程運行狀態快照,將來可以迅速恢復到某一種狀態,因此用戶空間有了時間機器。可以回滾到過去某一時刻的狀態 內在的基于依賴關系定義的服務控制邏輯 對與systemd來講,引入了一個新的核心概念…

    Linux干貨 2016-09-23
  • 查找幫助功能、Linux文件系統目錄標準LHS介紹

    查找幫助 命令自帶   COMMAND –help                      COMMAND -h 使用手冊 man COMMAND…

    Linux干貨 2016-10-30
  • LVS

        Linux虛擬服務器(LVS)是基于Linux內核的操作系統的負載平衡軟件。     LVS提供了良好的可擴展性,可靠性和可服務性。 LVS項目的主要工作是開發先進的IP 負載平衡軟件(IPVS),應用級負載平衡軟件(KTCPVS)和集群管理組件。 IPVS:在…

    Linux干貨 2017-08-21

評論列表(1條)

  • 馬哥教育
    馬哥教育 2017-03-07 11:52

    完成的很好,腳本的思路清晰,加油!

欧美性久久久久