Linux系統管理第七周作業【Linux微職位】
1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現;
[[email protected] ~]# vim usershell.sh #!/bin/bash # declare -i nologin_num=0 declare -i login_num=0 for i in $(cut -d: -f7 /etc/passwd);do if [ "$i" == "/sbin/nologin" ];then let nologin_num++ else let login_num++ fi done echo "The total number of user shell that can‘t login is :$nologin_num" echo "The total number of user shell that can login is :$login_num"
腳本測試
[[email protected] ~]# bash usershell.sh The total number of user shell that can‘t login is :33 The total number of user shell that can login is :5 [[email protected] ~]# grep -o /sbin/nologin /etc/passwd | wc -l 33 [[email protected] ~]# grep -v /sbin/nologin /etc/passwd | wc -l 5
2、寫一個腳本
(1) 獲取當前主機的主機名,保存於hostname變量中;
(2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com;
(3) 否則,則顯示當前主機名;
vim hostnametest.sh #!/bin/bash # hostname=$(hostname) if [ "$hostname" == "localhost" ];then hostname www.magedu.com echo "Hostname has changed to www.magedu.com" else echo "Current hostname is $hostname" fi
腳本測試
[[email protected] ~]# hostname localhost [[email protected] ~]# bash hostnametest.sh Hostname has changed to www.magedu.com [[email protected] ~]# hostname www.magedu.com [[email protected] ~]# bash hostnametest.sh Current hostname is www.magedu.com
3、寫一個腳本,完成如下功能
(1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在;
(2) 如果存在,則顯示此設備上的所有分區信息;
vim devicetest.sh #!/bin/bash # read -p "Please input a device path:" devicepath if [ -z $devicepath ];then echo "Usage: Please input a device path" exit 1 fi if [ -b $devicepath ];then fdisk -l $devicepath else echo "No such device" fi
腳本測試
[[email protected] ~]# bash devicetest.sh Please input a device path: Usage: Please input a device path [[email protected] ~]# bash devicetest.sh Please input a device path:/dev/sdb No such device [[email protected] ~]# bash devicetest.sh Please input a device path:/dev/sda Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000a0ae7 Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 41943039 20458496 8e Linux LVM
4、寫一個腳本,完成如下功能
腳本能夠接受一個參數;
(1) 如果參數1為quit,則顯示退出腳本,並執行正常退出;
(2) 如果參數1為yes,則顯示繼續執行腳本;
(3) 否則,參數1為其它任意值,均執行非正常退出;
vim parametertest.sh #!/bin/bash # read -p "Please input a word(quit|yes):" parameter while true;do case $parameter in quit) echo "exit the script" exit 0 ;; yes) echo "continue to excute the script" read -p "Please input a word(quit|yes):" parameter ;; *) echo "error exit" exit 1 ;; esac done
腳本測試
[[email protected] ~]# bash parametertest.sh Please input a word(quit|yes):quit exit the script [[email protected] ~]# bash parametertest.sh Please input a word(quit|yes):yes continue to excute the script Please input a word(quit|yes):quit exit the script [[email protected] ~]# bash parametertest.sh Please input a word(quit|yes): error exit [[email protected] ~]# bash parametertest.sh Please input a word(quit|yes):yes continue to excute the script Please input a word(quit|yes): 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) 其它任意值,則顯示錯誤壓縮工具,並執行非正常退出;
vim compresstest.sh #!/bin/bash # if [ ! -e /backups ];then mkdir /backups fi read -p "Please choose a format of compression(gzip|bzip2|xz):" zip case $zip 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 "error compression format" exit 1 ;; esac
腳本測試
[[email protected] ~]# ls /backups ls: cannot access /backups: No such file or directory [[email protected] ~]# bash compresstest.sh Please choose a format of compression(gzip|bzip2|xz):gzip [[email protected] ~]# bash compresstest.sh Please choose a format of compression(gzip|bzip2|xz):bzip2 [[email protected] ~]# bash compresstest.sh Please choose a format of compression(gzip|bzip2|xz):xz [[email protected] ~]# bash compresstest.sh Please choose a format of compression(gzip|bzip2|xz): error compression format [[email protected] ~]# ls /backups/ etc-20170624.tar.bz2 etc-20170624.tar.gz etc-20170624.tar.xz
6、寫一個腳本,接受一個路徑參數:
(1) 如果為普通文件,則說明其可被正常訪問;
(2) 如果是目錄文件,則說明可對其使用cd命令;
(3) 如果為符號鏈接文件,則說明是個訪問路徑;
(4) 其它為無法判斷;
vim pathtest.sh #!/bin/bash # read -p "Please input a path:" path if [ -f $path ];then echo "${path} can be visited" cat $path elif [ -d $path ];then echo "${path} can use ‘cd‘ command" elif [ -h $path ];then echo "${path} is a access path" ls -l $path else echo "unknown file" exit 1 fi
腳本測試
[[email protected] ~]# bash pathtest.sh Please input a path:/etc/fstab /etc/fstab can be visited # # /etc/fstab # Created by anaconda on Tue Aug 2 21:31:19 2016 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/rhel-root / xfs defaults 1 1 UUID=ce40e87b-854d-42dc-ac50-e1309101c43d /boot xfs defaults 1 2 /dev/mapper/rhel-swap swap swap defaults 0 0 /dev/cdrom/media/cdromiso9660defaults0 0 [[email protected] ~]# bash pathtest.sh Please input a path:/dev /dev can use ‘cd‘ command [[email protected] ~]# bash pathtest.sh Please input a path:/dev/cdrom /dev/cdrom is a access path lrwxrwxrwx. 1 root root 3 Aug 3 2016 /dev/cdrom -> sr0 [[email protected] ~]# bash pathtest.sh Please input a path:/hello unknown file
7、寫一個腳本,取得當前主機的主機名,判斷
(1) 如果主機名為空或為localhost,或為"(none)",則將其命名為mail.magedu.com;
(2) 否則,顯示現有的主機名即可;
vim hostnametest2.sh #!/bin/bash # hostname=$(hostname) if [ -z $hostname -o $hostname == "localhost" -o $hostname == "none" ];then hostname "mail.magedu.com" && hostname else echo "Current hostname is $hostname" fi
腳本測試
[[email protected] ~]# hostname mail.magedu.com [[email protected] ~]# bash hostnametest2.sh Current hostname is mail.magedu.com [[email protected] ~]# hostname localhost [[email protected] ~]# bash hostnametest2.sh mail.magedu.com [[email protected] ~]# hostname none [[email protected] ~]# bash hostnametest2.sh mail.magedu.com
8、寫一腳本,接受一個用戶名為參數;
(1) 如果用戶的id號為0,則顯示其為管理員;
(2) 如果用戶的id號大於0且小於500, 則顯示其為系統用戶;
(3) 否則,則顯示其為普通用戶;
vim usernametest.sh #!/bin/bash # read -p "Please input a username:" username if [ -z $username ];then echo "Usage: Please input a username" exit 1 fi if ! id $username &>/dev/null;then echo "user doesn‘t exist" else userid=$(id -u $username) if [ $userid -eq 0 ];then echo "$username is a administrator" elif [ $userid -gt 0 -a $userid -lt 500 ];then echo "$username is a system user" else echo "$username is a normal user" fi fi
腳本測試
[[email protected] ~]# bash usernametest.sh Please input a username: Usage: Please input a username [[email protected] ~]# bash usernametest.sh Please input a username:jack user doesn‘t exist [[email protected] ~]# bash usernametest.sh Please input a username:root root is a administrator [[email protected] ~]# bash usernametest.sh Please input a username:postfix postfix is a system user [[email protected] ~]# useradd yuki [[email protected] ~]# bash usernametest.sh Please input a username:yuki yuki is a normal user
Linux系統管理第七周作業【Linux微職位】