1. 写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;
“`
#!/bin/bash
#
declare -i loguser=0
declare -i nologuser=0
while read userinfo; do
usershell=`echo $userinfo | cut -d’:’ -f7`
if [ $usershell == ‘/sbin/nologin’ ]; then
let nologuser+=1
else
let loguser+=1
fi
done < /etc/passwd
echo “Login user: $loguser.”
echo “Nologin user: $nologuser.”
“`
2. 写一个脚本
(1) 获取当前主机的主机名,保存于hostname变量中;
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
(3) 否则,显示当前主机名;
“`
#!/bin/bash
#
hostname=`hostname`
if [ $hostname == ‘localhost’ ]; then
hostname ‘www.magedu.com’
fi
echo “Hostname for this host is $hostname.”
“`
3. 写一个脚本,完成如下功能
(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
“`
#!/bin/bash
#
read -p “Please enter a disk device in the following format(/dev/sda): ” disk
if fdisk -l | grep “^Disk $disk”; then
fdisk -l $disk
else
echo “The disk you entered does not exist.”
exit 1
fi
“`
4. 写一个脚本,完成如下功能
脚本能够接受一个参数
(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
(2) 如果参数1为yes,则显示继续执行脚本;
(3) 否则参数1为其他任意值,均质性非正常退出;
“`
#!/bin/bash
#
read -p “Please enter a parameter: ” PARM
while true; do
case $PARM in
quit)
echo “Quiting the script…”
exit 0
;;
yes)
read -p “Please reenter a parameter: ” PARM
;;
*)
echo “Abnormal quiting.”
exit 1
;;
esac
done
“`
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
#
read -p “Please choose a compress method {gzip|bzip2|xz} : ” COMP_MTHD
ls /backups/ &> /dev/null || mkdir /backups
case $COMP_MTHD in
gzip)
tar -zcf /backups/etc-`date +%Y%m%d`.tar.gz /etc/* &> /dev/null && echo “Gzip archive finished.”
;;
bzip2)
tar -jcf /backups/etc-`date +%Y%m%d`.tar.bz2 /etc/* &> /dev/null && echo “Bzip2 archive finished.”
;;
xz)
tar -Jcf /backups/etc-`date +%Y%m%d`.tar.xz /etc/* &> /dev/null && echo “XZ archive finished.”
;;
*)
echo “Wrong compress tool.”
exit 1
;;
esac
“`
6. 写一个脚本,接受一个路径参数:
(1) 如果为普通文件,则说明其可被正常访问;
(2) 如果是目录文件,则说明可对其使用cd命令;
(3) 如果为符号链接文件,则说明是个访问路径;
(4) 其他为无法判断
“`
#!/bin/bash
#
read -p “Please eneter a filepath: ” filepath
until `ls $filepath &> /dev/null`; do
echo “$filepath is not a valid filepath.”
read -p “Please eneter a filepath: ” filepath
done
filetype=`file -b $filepath | cut -d’,’ -f1`
if echo $filetype | grep “symbolic link” &> /dev/null; then
echo “This is a access path.”
elif [ “$filetype” == ‘directory’ ]; then
echo “You can use cd command to enter this directory.”
elif [ “$filetype” == ‘ASCII text’ ]; then
echo “This file can be access normally.”
else
echo “Unknow file type.”
fi
“`
7. 写一个脚本,取得当前主机的主机名,判断
如果主机名为空或为localhost、或为“(none)”,则将其命名为mail.magedu.com;
“`
#!/bin/bash
#
hostName=$(hostname)
[ -z “$hostName” -o “$hostName”==”localhost” -o $hostName”==”none” ] && hostname mail.magedu.com
“`
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/89524
评论列表(1条)
脚本没有问题。赞