马哥门徒-N26-邢岩
相信自己、勤奋努力、不断总结!shell脚本练习需要重复、重复、再重复!那么,我就继续来写、写、写。
1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;
#****************************************************
# Author: jeque
# Last modified: 2017-04-02 23:04
# Filename: user10.sh
# Description:
# Version: 1.1
#****************************************************
declare user_login=0
declare user_nologin=0
for i in $(cat /etc/passwd | cut -d: -f7); do
if [ $i == “/sbin/nologin” ];then
let user_nologin++
else
let user_login++
fi
done
echo “login user number:$user_login”
echo “nologin user number:$user_nologin”
2、写一个脚本
(1) 获取当前主机的主机名,保存于hostname变量中;
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
(3) 否则,则显示当前主机名;
#!/bin/bash
#****************************************************
# Email: 2372911967@qq.com
# Author: jeque
# Time: 2017-04-02 23:31
# Filename: localhost1.sh
# Version: 1.0
# Description: A test script.
#****************************************************
HostName=$(/bin/hostname)
if [ $UID -eq 0 ];then
if [ $HostName == “localhost.localdomain” ];then
hostname www.magedu.com
echo “hostname changed”
else
hostname
fi
else
echo”You must be root to change the hostname.”
exit 1
fi
3、写一个脚本,完成如下功能
(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
#!/bin/bash
#****************************************************
# Email: 2372911967@qq.com
# Author: jeque
# Time: 2017-04-03 18:03
# Filename: fdev.sh
# Version: 1.0
# Description: A test script.
#****************************************************
if [ $# -ge 1 ];then
for dev in $*;do
if [ -b $dev ];then
fdisk -l $dev
else
nodev=$nodev” “$dev
fi
done
[[ $(echo $nodev | wc -c) -gt 1 ]] && echo -e “\nThe device is not exist,plesae try again.\n$nodev”
else
echo “please put in an exist device.”
fi
4、写一个脚本,完成如下功能
脚本能够接受一个参数;
(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
(2) 如果参数1为yes,则显示继续执行脚本;
(3) 否则,参数1为其它任意值,均执行非正常退出;
#!/bin/bash
#****************************************************
# Email: 2372911967@qq.com
# Author: jeque
# Time: 2017-04-03 18:39
# Filename: parameter.sh
# Version: 1.0
# Description: A test script.
#****************************************************
menu () {
cat <<END
1.quit
2.yes
3.other
please input the parameter you want:
END
}
menu
read -p “Your choice: ” option
case $option in
quit)
echo “Exit the script.”
exit
;;
yes)
echo “Script continue.”
;;
*)
exit 2
esac
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
#****************************************************
# Email: 2372911967@qq.com
# Author: jeque
# Time: 2017-04-03 21:08
# Filename: parameter2.sh
# Version: 1.0
# Description: A test script.
#****************************************************
menu () {
cat <<END
1.gzip
2.bzip2
3.xz
please input the parameter listed above:
END
}
myPath=”/backups”
if [ ! -x “$myPath” ];then
mkdir -p “$myPath” &> /dev/null
fi
menu
read -p “Your choice:” option
case $option in
gzip)
tar -zcf /backups/etc-$(date +%Y%m%d).tar.gz /etc &> /dev/null
echo “The new filename is etc-$(date +%Y%m%d).tar.gz”
;;
bzip2)
tar -jcf /backups/etc-$(date +%Y%m%d).tar.bz2 /etci &> /dev/null
echo “The new filename is etc-$(date +%Y%m%d).tar.bz2”
;;
xz)
tar -Jcf /backups/etc-$(date +%Y%m%d).tar.xz /etc &> /dev/null
echo “The new filename is etc-$(date +%Y%m%d).tar.xz”
;;
*)
echo “Error compression tools.”
exit 2
esac
6、写一个脚本,接受一个路径参数:
(1) 如果为普通文件,则说明其可被正常访问;
(2) 如果是目录文件,则说明可对其使用cd命令;
(3) 如果为符号链接文件,则说明是个访问路径;
(4) 其它为无法判断;
#!/bin/bash
#****************************************************
# Email: 2372911967@qq.com
# Author: jeque
# Time: 2017-04-03 22:17
# Filename: files11.sh
# Version: 1.0
# Description: A test script.
#****************************************************
if [ $# -lt 1 ]; then
echo “At least one path.”
exit 1
elif ! [ -e $1 ]; then
echo “No such file!”
exit 2
elif [ -f $1 ]; then
echo “Common file.”
echo ” It can be normal access.”
elif [ -d $1 ]; then
echo “Directory file.”
echo “You can use command ‘cd’ to the file.”
elif [ -L $1 ]; then
echo “Symbolic link file.”
echo “An access path! “
else
echo “Unkown!”
echo “Can’t judge!”
fi
7、写一个脚本,取得当前主机的主机名,判断
(1) 如果主机名为空或为localhost,或为””(none)””,则将其命名为mail.magedu.com;
(2) 否则,显示现有的主机名即可;
#!/bin/bash
#****************************************************
# Email: 2372911967@qq.com
# Author: jeque
# Time: 2017-04-03 22:39
# Filename: localhost2.sh
# Version: 1.0
# Description: A test script.
#****************************************************
HostName=$(/bin/hostname)
if [ $UID -eq 0 ];then
if [ $HostName == “localhost” ];then
hostname mail.magedu.com
echo “hostname changed”
elif [ $HostName == ” ” ]; then
hostname mail.magedu.com
elif [ $HostName == “(none)” ];then
hostname mail.magedu.com
else
hostname
fi
else
echo”You must be root to change the hostname.”
exit 1
fi
OK!shell脚本暂时先练到这里,该休息了!Bye!
原创文章,作者:jeque,如若转载,请注明出处:http://www.178linux.com/72408
评论列表(2条)
总结的很好,脚本的思路清晰,加油~~~~
Cool! That’s a clever way of loonkig at it!