第九周作業

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 09:57
下一篇 2017-03-05 11:35

相關推薦

  • 基于iptables mangle的lvs && lvs的持久連接

    一、lvs-dr類型:也稱direct routing,簡稱為g(gatewaying);配置lvs-dr基于iptables mangle的實現方式  實驗圖:  地址規劃:    VIP: 172.16.2.100    DIP: 172.16.2.13    RIP1:17…

    Linux干貨 2015-06-30
  • CentOS7的啟動及Systemd的管理?

    CentOS7的啟動流程     POST–>Boot Sequence–>Bootloader–>kernel+initramfs(initrd)–>rootfs–>/sbin/init    &n…

    Linux干貨 2016-09-21
  • 基于ssl的mysql的主從復制

    實驗環境: system:CentOS Linux release 7.2.1511 (Core) mariadb server:mariadb-server-5.5.44-2.el7.centos.x86_64 master server:10.1.51.20/16 slave serv…

    2016-11-22
  • 輸出重定向 輸入重定向 管道簡單介紹 -20160729

    輸出重定向 輸入重定向 管道簡單介紹 標準輸入和輸出 我們先來了解下輸入和輸出的概念: 在計算機中我們了解到計算機的組成部分:其中有輸入 輸出設備。       輸出重定向 對于程序來說: 程序 :指令 + 數據 對于數據來說,數據可以由我們通過鍵盤輸入,或者程序直接使用存儲設備上的數據,我們稱為讀入 數據,程序處理數據后需要返…

    Linux干貨 2016-08-04
  • bash中的變量

        對任何一門編程語言來說,最基礎的部分就是變量。那什么事變量呢?變量就是把一個已知的可以變動的值,賦給一個固定名字的,用固定的名字代表這個可變動的值。在bash中也不例外,跟大多數編程語言一樣,它也有一些自己的語法和規則 bash變量:     1.規則設置:變量和變量的值中間用=連接,=的兩…

    Linux干貨 2016-08-15
  • 文件系統的創建和管理

    作業:文件系統的創建和管理 思路:管理通常就是增刪改查[*代表某一數值,測試機centos6使用的就是ext4,不要問我為什么了,馬哥告訴我的,你問他吧!];   事情準備檢查系統支持和使用的文件系統格式:         #lsmod  &nbsp…

    Linux干貨 2016-01-19

評論列表(1條)

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

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

欧美性久久久久