magedu pro 第七周作业
1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;
脚本内容如下:
#!/usr/bin/env bash
loginshell=$(cat /etc/passwd|cut -d: -f7);
loginct=0;
nologinct=0;
for i in $loginshell;do
if [[ "$i" == "/sbin/nologin" ]]; then
let nologinct+=1;
else
let loginct+=1;
fi
done
echo "可登录的用户数为:$loginct";
echo "不可登录的用户数为:$nologinct";
执行结果如下:
[hzz@magedu ~]$ bash test.sh
可登录的用户数为:5
不可登录的用户数为:20
[hzz@magedu ~]$
2、写一个脚本
(1) 获取当前主机的主机名,保存于hostname变量中;
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
(3) 否则,则显示当前主机名;
脚本内容如下:
#!/usr/bin/env bash
set -e;
hostname=$(hostname);
if [[ "$hostname" == "localhost" ]]; then
hostname www.magedu.com;
# 永久更改,需重启
#echo "www.magedu.com" > /etc/hostname
else
echo $hostname;
fi
执行结果如下:
[root@localhost ~]# hostname
localhost
[root@localhost ~]# bash /home/hzz/test.sh
[root@localhost ~]# hostname
www.magedu.com
[root@localhost ~]# bash /home/hzz/test.sh
www.magedu.com
[root@localhost ~]#
3、写一个脚本,完成如下功能
(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
脚本内容如下:
#!/usr/bin/env bash
set -e;
diskdir=$1;
fdisk -l $1 2>/dev/null || echo "此设备不存在!";exit 1;
执行结果如下:
[root@magedu ~]# bash /home/hzz/test.sh /dev/sdd
此设备不存在!
[root@magedu ~]# bash /home/hzz/test.sh /dev/sda
磁盘 /dev/sda:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00086948
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 20971519 9436160 8e Linux LVM
[root@magedu ~]#
4、写一个脚本,完成如下功能
脚本能够接受一个参数;
(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
(2) 如果参数1为yes,则显示继续执行脚本;
(3) 否则,参数1为其它任意值,均执行非正常退出;
脚本内容如下:
#!/usr/bin/env bash
set -e;
read -p "Please input a command (yes/quit): " input_command;
case $input_command in
yes )
echo "Continue...";
;;
quit )
echo "Goodbye!";exit 1;
;;
* )
echo "Wrong command!";exit 2;
;;
esac
执行结果如下:
[hzz@magedu ~]$ bash test.sh
Please input a command (yes/quit): yes
Continue...
[hzz@magedu ~]$ echo $?
0
[hzz@magedu ~]$ bash test.sh
Please input a command (yes/quit): quit
Goodbye!
[hzz@magedu ~]$ echo $?
1
[hzz@magedu ~]$ bash test.sh
Please input a command (yes/quit): test
Wrong command!
[hzz@magedu ~]$ echo $?
2
[hzz@magedu ~]$
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) 其它任意值,则显示错误压缩工具,并执行非正常退出;
脚本内容如下:
#!/usr/bin/env bash
set -e;
mkdir -p /backups;
read -p "Please select a compress type (gzip/bzip2/xz): " input_command;
case $input_command in
gzip )
tar zcf /backups/etc-20160613.tar.gz /etc;
echo "Compress complete!"
;;
bzip2 )
tar jcf /backups/etc-20160613.tar.bz2 /etc;
echo "Compress complete!"
;;
xz )
tar Jcf /backups/etc-20160613.tar.xz /etc;
echo "Compress complete!"
;;
* )
echo "Wrong type!";exit 1;
;;
esac
执行结果如下:
[root@magedu ~]# bash /home/hzz/test.sh
Please select a compress type (gzip/bzip2/xz): gzip
tar: 从成员名中删除开头的“/”
Compress complete!
[root@magedu ~]# ls /backups/
etc-20160613.tar.gz
[root@magedu ~]# bash /home/hzz/test.sh
Please select a compress type (gzip/bzip2/xz): bzip2
tar: 从成员名中删除开头的“/”
Compress complete!
[root@magedu ~]# ls /backups/
etc-20160613.tar.bz2 etc-20160613.tar.gz
[root@magedu ~]# bash /home/hzz/test.sh
Please select a compress type (gzip/bzip2/xz): xz
tar: 从成员名中删除开头的“/”
Compress complete!
[root@magedu ~]# ls /backups/
etc-20160613.tar.bz2 etc-20160613.tar.gz etc-20160613.tar.xz
[root@magedu ~]# bash /home/hzz/test.sh
Please select a compress type (gzip/bzip2/xz): jzip
Wrong type!
[root@magedu ~]# echo $?
1
[root@magedu ~]#
6、写一个脚本,接受一个路径参数:
(1) 如果为普通文件,则说明其可被正常访问;
(2) 如果是目录文件,则说明可对其使用cd命令;
(3) 如果为符号链接文件,则说明是个访问路径;
(4) 其它为无法判断;
脚本内容如下:
#!/usr/bin/env bash
set -e;
while read -p "Please file path (input quit to exit): " input_command;do
if [[ "$input_command" == "quit" ]]; then
exit;
elif [[ -L $input_command ]]; then
echo "$input_command is a soft link, you can follow it! "
elif [[ -f $input_command ]]; then
echo "$input_command is a nomal file, you can read it! "
elif [[ -d $input_command ]]; then
echo "$input_command is a directory, use 'cd' to enter it! "
else
echo "Path is not exist or cat't recognize file! "
fi
done
执行结果为:
[hzz@magedu ~]$ mkdir test_dir
[hzz@magedu ~]$ touch test_file
[hzz@magedu ~]$ ln -s test_file test_link
[hzz@magedu ~]$ bash test.sh
Please file path (input quit to exit): /home/hzz/test_file
/home/hzz/test_file is a nomal file, you can read it!
Please file path (input quit to exit): /home/hzz/test_dir
/home/hzz/test_dir is a directory, use 'cd' to enter it!
Please file path (input quit to exit): /home/hzz/test_link
/home/hzz/test_link is a soft link, you can follow it!
Please file path (input quit to exit): /dev/tty
Path is not exist or cat't recognize file!
Please file path (input quit to exit): /dev/123
Path is not exist or cat't recognize file!
Please file path (input quit to exit): quit
[hzz@magedu ~]$
7、写一个脚本,取得当前主机的主机名,判断
(1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;
(2) 否则,显示现有的主机名即可;
脚本内容如下:
#!/usr/bin/env bash
set -e;
hostname=$(hostname);
if [[ -z "$hostname" || "$hostname" == "(none)" ]]; then
hostname mail.magedu.com;
# 永久更改,需重启
#echo "mail.magedu.com" > /etc/hostname;
else
echo "$hostname";
fi
执行结果如下:
[root@magedu ~]# bash /home/hzz/test.sh
magedu
[root@magedu ~]#
8、写一脚本,接受一个用户名为参数;
(1) 如果用户的id号为0,则显示其为管理员;
(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
(3) 否则,则显示其为普通用户;
脚本内容如下:
#!/usr/bin/env bash
while read -p "Please input username (input quit to exit): " input_command;do
[[ "$input_command" == "quit" ]] && exit;
username=$input_command
userid=$(id -u $username 2> /dev/null)
if [ $userid -eq 0 ]; then
echo "$username is a administrator! "
elif [ $userid -gt 0 ] && [ $userid -lt 500 ]; then
echo "$username is a system user! "
elif [[ "$userid" == "" ]]; then
echo "$username is not exist! "
else
echo "$username is a nomal user! "
fi
done
执行结果为:
[hzz@magedu ~]$ bash test.sh
Please input username (input quit to exit): root
root is a administrator!
Please input username (input quit to exit): mail
mail is a system user!
Please input username (input quit to exit): hzz
hzz is a nomal user!
Please input username (input quit to exit): quit
[hzz@magedu ~]$