1. Write a script to determine whether the shell of all users on the current system is a login shell (that is, the user's shell is not/sbin/nologin), the number of these two types of users, and the string comparison;
[Email protected] ~]# vim usershell.sh#!/bin/bash#declare-i nologin_num=0declare-i login_num=0for i in $ (cut-d:-f7/e TC/PASSWD);d o if ["$i" = = "/sbin/nologin"];then let nologin_num++ else let login_num++ fidoneecho "The total number of the user shell," can ' t login is: $nologin _num "echo" The all number of the user shell that can login is : $login _num "
Script Test
[[email protected] ~]# Bash usershell.sh The total number of the user shell, the can ' t login is:33the total number of user sh ell that can login Is:5[[email protected] ~]# Grep-o/sbin/nologin/etc/passwd | Wc-l33[[email protected] ~]# grep-v/sbin/nologin/etc/passwd | Wc-l5
2. Write a script
(1) Obtain the host name of the current host and save it in the hostname variable;
(2) Determine if the value of this variable is localhost, and if so, modify the current hostname to www.magedu.com;
(3) Otherwise, the current host name is displayed;
Vim hostnametest.sh#!/bin/bash#hostname=$ (hostname) If ["$hostname" = = "localhost"];then hostname www.magedu.com EC Ho "Hostname have changed to www.magedu.com" Else echo "current Hostname is $hostname" fi
Script Test
[[email protected] ~]# hostnamelocalhost[[email protected] ~]# bash hostnametest.sh Hostname have changed to www.magedu.com [Email protected] ~]# hostnamewww.magedu.com[[email protected] ~]# bash hostnametest.sh current hostname is WWW.MAGEDU.C Om
3, write a script to complete the following functions
(1) Pass a disk device file path to the script to determine whether the device exists;
(2) If present, displays all the partition information on this device;
Vim devicetest.sh#!/bin/bash#read-p "Please input a device path:" Devicepathif [-Z $devicepath];then echo "USAGE:PL Ease input a device path "Exit 1fiif [-B $devicepath];then fdisk-l $devicepathelse echo" No such device "fi
Script Test
[[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/sdbno such device[[ email protected] ~]# bash devicetest.sh please input a device path:/dev/sdadisk /dev/sda: 21.5 gb, 21474836480 bytes, 41943040 Sectorsunits = sectors of 1 * 512 = 512 bytessector size (logical/physical): 512 bytes / 512 bytesi/o size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk 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, write a script to complete the following functions
The script can accept a parameter;
(1) If the parameter 1 is quit, the exit script is displayed, and the normal exit is performed;
(2) If the parameter 1 is yes, the execution script is displayed;
(3) Otherwise, the parameter 1 is any other value, performs an abnormal exit;
Vim parametertest.sh#!/bin/bash#read-p "Please input a word (Quit|yes):" Parameterwhile true;docase $parameter inquit) 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;; Esacdone
Script Test
[[email protected] ~]# bash parametertest.shplease input a word (Quit|yes): Quitexit the Script[[email protected] ~]# bash p Arametertest.shplease input a Word (Quit|yes): Yescontinue to excute the Scriptplease input a word (Quit|yes): Quitexit the SC Ript[[email protected] ~]# bash parametertest.shplease input a word (Quit|yes): Error exit[[email protected] ~]# bash Parame Tertest.shplease input a Word (Quit|yes): Yescontinue to excute the Scriptplease input a word (Quit|yes): Error exit
5, write a script to complete the following functions
Pass a parameter to the script, this parameter is one of gzip, bzip2 or XZ;
(1) If the value of parameter 1 is gzip, use the TAR and gzip archive to compress/etc directories into the/backups directory and name/backups/etc-20160613.tar.gz;
(2) If the value of parameter 1 is bzip2, use tar and bzip2 archive to compress/etc directory to/backups directory, and named/backups/etc-20160613.tar.bz2;
(3) If the value of parameter 1 is XZ, then the tar and XZ archives are used to compress/etc directories into the/backups directory and named/backups/etc-20160613.tar.xz;
(4) Any other value, the error compression tool is displayed, and an abnormal exit is performed;
Vim compresstest.sh#!/bin/bash#if [!-e/backups];thenmkdir/backupsfiread-p "Please choose a format of compression (Gzi P|BZIP2|XZ): "Zipcase $zip ingzip) 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
Script Test
[[email protected] ~]# ls/backupsls:cannot access/backups:no such file or directory[[email protected] ~]# bash compres Stest.shplease choose a format of compression (GZIP|BZIP2|XZ): Gzip[[email protected] ~]# bash compresstest.shplease Choose a format of compression (GZIP|BZIP2|XZ): Bzip2[[email protected] ~]# bash compresstest.shplease Choose a format of CO Mpression (GZIP|BZIP2|XZ): Xz[[email protected] ~]# bash compresstest.shplease 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. Write a script that accepts a path parameter:
(1) If it is an ordinary document, it can be accessed normally;
(2) If it is a catalog file, then the CD command is available for it;
(3) If the file is a symbolic link, the description is a access path;
(4) Other is impossible to judge;
Vim pathtest.sh#!/bin/bash#read-p "Please input a path:" Pathif [-F $path];then echo "${path} can be visited" cat $pathelif [-D $path];then echo "${path} can use ' CD ' command" Elif [-H $path];then echo "${path} is a access pat H "Ls-l $pathelse echo" Unknown file "Exit 1fi
Script Test
[[email protected] ~]# bash pathtest.shplease 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 1UUID=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.shplease input a path:/dev /dev can use ' CD ' command[[email protected] ~]# bash pathtest.shplease input a path:/dev/cdrom/dev/cdrom is a access pathlrwxrwxrwx. 1 root root 3 aug 3 2016 /dev/cdrom -> sr0[[email protected] ~]# bash pathtest.shPlease Input a path:/hellounknown file
7, write a script, get the host name of the current host, judge
(1) If the host name is empty or localhost, or "(none)", it is named mail.magedu.com;
(2) Otherwise, display the existing host name can be;
Vim hostnametest2.sh#!/bin/bash#hostname=$ (hostname) If [-Z $hostname-o $hostname = = "localhost"-o $hostname = "None"] ; Thenhostname "mail.magedu.com" && Hostnameelseecho "current hostname is $hostname" fi
Script Test
[Email protected] ~]# hostnamemail.magedu.com[[email protected] ~]# Bash hostnametest2.shcurrent hostname is Mail.magedu.com[[email protected] ~]# hostname localhost[[email protected] ~]# bash hostnametest2.shmail.magedu.com[[ Email protected] ~]# hostname none[[email protected] ~]# bash hostnametest2.shmail.magedu.com
8, write a script, accept a user name as a parameter;
(1) If the user's ID number is 0, it is displayed as an administrator;
(2) If the user's ID number is greater than 0 and less than 500, it is displayed as a system user;
(3) Otherwise, it is displayed as a normal user;
Vim usernametest.sh#!/bin/bash#read-p "Please input a username:" Usernameif [-Z $username];then echo "Usage:please Input a username "Exit 1fiif! ID $username &>/dev/null;thenecho "user doesn ' t exist" elseuserid=$ (id-u $username) if [$userid-eq 0];thenecho "$ Username is a administrator "elif [$userid-gt 0-a $userid-lt];thenecho" $username is a system user "Elseecho" $user Name is a normal user "Fifi
Script Test
[[email protected] ~]# bash usernametest.shplease input a username:Usage:Please input a username[[email protected] ~]# BA SH usernametest.shplease input a username:jackuser doesn ' t exist[[email protected] ~]# bash usernametest.shplease input A Username:rootroot is a administrator[[email protected] ~]# bash usernametest.shplease input a username:postfixpostfix is a System User[[email protected] ~]# useradd yuki[[email protected] ~]# bash usernametest.shplease input a Username:yukiyuki is a normal user
Linux System Management Seventh week job "Linux Micro Jobs"