Linux第七周微职位
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux第七周微职位相关的知识,希望对你有一定的参考价值。
1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;
1)创建文件 ]# vim usershells.sh
#!/bin/bash
# check current system all users is its login shells
#
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"
2)授予用户执行权限 #] chmod u+x usershells.sh
3)执行文件 ]# ./usershells.sh 如下提示:当然每个人是系统用户数都是不一样的
The total number of user shell that can‘t login is: 23
The total number of user shell that can login is: 5
4)执行]# grep -o /sbin/nologin /etc/passwd | wc -l 进行验证用户不可登录的shell
5)执行 ]# grep -v /sbin/nologin /etc/passwd | wc -l 进行验证用户可登录的shell
2、写一个脚本
(1) 获取当前主机的主机名,保存于hostname变量中;
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
(3) 否则,则显示当前主机名;
1)编辑文件 ]# vim hostname.sh
#!/bin/bash
# get current system hostname
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
2)授予hostname.sh文件执行权限 ]# chmod u+x hostname.sh
3)执行文件 ]# ./hostname.sh
Hostname has changed to www.magedu.com
3、写一个脚本,完成如下功能
(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
1)编辑文件 #] vim deviceinfo.sh
#!/bin/bash
# check device is it not exists, it is exist that list deviceinfo
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
2) 授予用户执行权限]# chmod u+x deviceinfo.sh
3) 执行文件 ]# ./deviceinfo.sh 如下:
Please input a device path:/dev/sda #在提示下输入/dev/sda设备路径
磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00049c92
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 41943039 20458496 8e Linux LVM
[[email protected] tmp]# ./deviceinfo.sh
Please input a device path:/dev/sdb
磁盘 /dev/sdb:85.9 GB, 85899345920 字节,167772160 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
没有输入任何然后直接回车
[[email protected] tmp]# ./deviceinfo.sh
Please input a device path:
Usage: Please input a device path
输入一个不存在的设备路径
[[email protected] tmp]# ./deviceinfo.sh
Please input a device path:/dev
No such device
4、写一个脚本,完成如下功能
脚本能够接受一个参数;
(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
(2) 如果参数1为yes,则显示继续执行脚本;
(3) 否则,参数1为其它任意值,均执行非正常退出;
1) 编辑文件
[[email protected] tmp]# cat 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
2)授予用户执行文件 ]# chmod u+x parametertest.sh
3)测试脚本文件
[[email protected] tmp]# ./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] tmp]# ./parametertest.sh
Please input a word(quit|yes): bye
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) 其它任意值,则显示错误压缩工具,并执行非正常退出;
1)创建脚本文件 vim compress.sh
[[email protected] tmp]# cat compress.sh
#!/bin/bash
# check input parameter is it gzip or bzip2 or xz
#
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
2)授予用户执行权限 ]# chmod u+x compress.sh
3)执行脚本
[[email protected] tmp]# ./compress.sh
Please choose a format of compression(gzip|bzip2|xz): gzip
tar: 从成员名中删除开头的“/”
[[email protected] tmp]# ./compress.sh
Please choose a format of compression(gzip|bzip2|xz): bzip2
tar: 从成员名中删除开头的“/”
[[email protected] tmp]# ./compress.sh
Please choose a format of compression(gzip|bzip2|xz): xz
tar: 从成员名中删除开头的“/”
[[email protected] tmp]# ./compress.sh
Please choose a format of compression(gzip|bzip2|xz): tar
error compression format
[[email protected] tmp]# ls -l /backups/
总用量 26328
-rw-r--r-- 1 root root 8577760 8月 26 16:38 etc-20170826.tar.bz2
-rw-r--r-- 1 root root 9794015 8月 26 16:35 etc-20170826.tar.gz
-rw-r--r-- 1 root root 8577760 8月 26 16:38 etc-20170826.tar.xz
6、写一个脚本,接受一个路径参数:
(1) 如果为普通文件,则说明其可被正常访问;
(2) 如果是目录文件,则说明可对其使用cd命令;
(3) 如果为符号链接文件,则说明是个访问路径;
(4) 其它为无法判断;
1)创建脚本文件vim pathtest.sh
[[email protected] tmp]# cat 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
2)授予用户执行权限chmod u+x pathtest.sh
3)测试文件
[[email protected] tmp]# ./pathtest.sh
Please input a path: /etc
/etc can use ‘cd ‘ command
[[email protected] tmp]# ./pathtest.sh
Please input a path: /etc/fa
unknown file
[[email protected] tmp]# ./pathtest.sh
Please input a path: /etc/fstab
/etc/fstab can be visited
#
# /etc/fstab
# Created by anaconda on Mon Jul 3 01:42:15 2017
#
# 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/centos-root / xfs defaults 0 0
UUID=ddb0b178-1278-498e-acee-c1436685cfbc /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[[email protected] tmp]# ./pathtest.sh
Please input a path: /dev/cdrom
/dev/cdrom is a access path
lrwxrwxrwx 1 root root 3 8月 26 15:20 /dev/cdrom -> sr0
7、写一个脚本,取得当前主机的主机名,判断
(1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;
(2) 否则,显示现有的主机名即可;
1)编辑脚本文件 vim hostnametest.sh
[[email protected] tmp]# cat hostnametest.sh
#!/bin/bash
# get current hostname
hostname=`hostname`
if [ -z $hostname -o $hostname == "localhost" -o $hostname == "none" ]; then
hostname mail.magedu.com
else
echo "Current hostname is $hostname"
fi
2)授予用户执行权限 ]# chmod u+x hostnametest.sh
3)测试脚本文件
[[email protected] tmp]# hostname
www.magedu.com
[[email protected] tmp]# ./hostnametest.sh
Current hostname is www.magedu.com
8、写一脚本,接受一个用户名为参数;
(1) 如果用户的id号为0,则显示其为管理员;
(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
(3) 否则,则显示其为普通用户;
1)创建脚本文件 vim userstest.sh
[[email protected] tmp]# cat userstest.sh
#!/bin/bash
# check userid
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
2)授予用户执行权限chmod u+x userstest.sh
3)测试
[[email protected] tmp]# ./userstest.sh
Please input a username: centos
centos is a normal user
[[email protected] tmp]# ./userstest.sh
Please input a username: root
root is a administrator
[[email protected] tmp]# ./userstest.sh
Please input a username: sshd
sshd is a system user
本文出自 “11822904” 博客,请务必保留此出处http://11832904.blog.51cto.com/11822904/1959502
以上是关于Linux第七周微职位的主要内容,如果未能解决你的问题,请参考以下文章