1:检查当前用户是否是超级用户,如果是超级用户,则安装dhcp服务;
如果不是超级用户,则输出“当前用户权限不足”
[ `id -u`="0" ] && {
yum install dhcp.x86_64 -y
systemctl start dhcpd
systemctl status dhcpd
}||{
echo "当前用户不是超级用户"
}
2:脚本编程要求如下:
a) :运行 scr ipt1.sh dog 输出结果为 cat
b) :运行 scr ipt1.sh cat 输出结果为 dog 除此以外都输出结果为 error
[ "$1" = "dog" ] && {
echo cat
}|| {
[ "$1" = "cat" ] && {
echo dog
}||{
echo error
}
}
3:建立用户脚本要求如下:
a) :如果系统存在用户 luck,并且使用/bin/bash,则输出 user ok
b) :如果系统村存在用户 luck,则输出 user exist
c):如果系统不存在用户 luck,则运行 user add luck 命令来创建
d) :当在系统上运行命令 usercmd 时,将输出指令“echo hello;ls-l” 的结果,并
在重启后也生效。
useradd luck &> /dev/null || {
echo "user exist"
[ `grep "^luck" /etc/passwd | cut -d ":" -f 7`="/bin/bash" ] &&{
echo user is ok
}
}
[ "`grep "usercmd" /etc/bashrc`" = "alias usercmd='echo hello;ls -l'" ] ||{
echo "alias usercmd='echo hello;ls -l'" >> /etc/bashrc
source /etc/bashrc
}
4:输出时间倒计时:
A:固定时间倒计时:
for time in {4..1}
do
echo -n "TIME $time 秒 "
echo -ne "\r"
sleep 1
done
read -p "请输入倒计时分秒数:" m s
for ((A=60*m+s;A>0;A--))
do
M=$[A/60]
S=$[A%60]
echo -n "TIME $M 分 $S 秒 "
echo -ne "\r"
sleep 1
done
5:检测可以pin通的主机,如果可以ping通则将ip地址存放在file文件中(if和case的基本用法):
if [ -e "/mnt/file" ]
then
echo "/mnt/file was exist!!!"
echo "[Overwrite] [Backup] [skip]"
read -p "what are you want to do:" action
case $action in
s|S)
;;
b|B)
mv /mnt/file /mnt/file_back
;;
o|O)
rm -rf /mnt/file
esac
fi
for ip in {1..10}
do
ping -c1 -w1 172.25.254.$ip &>/dev/null &&{
echo 172.25.254.$ip >> /mnt/file
}
6:判断文件类型:
需要注意的是,在判定一个文件是普通文件还是软链接时,因为if语句是顺序执行的,所有需要将判定软链接写在判定普通文件之前:
read -p " Plesae input the filename of what you want to detection: " filename
if [ ! -e "$filename" ]
then
echo "The file is not's exist!!! Please detection your input:"
elif [ -L "$filename" ]
then
echo "$filename is a software~"
elif [ -d "$filename" ]
then
echo "$filename is a common directory~"
elif [ -f "$filename" ]
then
echo "$filename is a common file~"
fi
7:使用脚本创建/删除/备份文件
需要注意的是,在创建/删除/备份文件之前,首先要进行判断,-a/-d/-p参数以及filename参数是否输入;其次判断filename文件是否存在;
Check_File()
{
if [ -e "$1" ]
then
$2
$3
fi
}
if [ "$#" -lt "2" ]
then
echo "Error:Please input option [-a|-d|-p] and following filename $0"
exit 1
fi
if [ "$1" = "-a" ]
then
Check_File $2 "echo $2 is exit!!" exit
touch $2
elif [ "$1" = "-d" ]
then
Check_File $2 "rm -rf $2" exit
echo "$2 is not exist"
elif [ "$1" = "-p" ]
then
Check_File $2 "cp -rp $2 /mnt" exit
echo "$2 is not exist"
else
echo "Please input -a|-d|-p following $0!!"
fi
8:交互式使用脚本远程获取信息文件(expect基本用法):
如:在可以连接的情况下,获取1~34号主机的/etc/passwd/文件的后三行文件信息(当然不止这么简单,类比的可以使用脚本批量远程在对方主机添加用户/安装程序等)
Connect(){
/usr/bin/expect <
echo “ i p + y e s " e c h o " ‘ C o n n e c t 172.25.254. ip + yes" echo "`Connect 172.25.254. ip+yes"echo"‘Connect172.25.254.ip tail -n 3 /etc/passwd >>/mnt/passwd`”
}||{
echo no
}
done
9:用户创建脚本:
if [ $# -lt 2 ]
then
echo "Please input userlist and passlist after scrept!!!"
elif [ ! -e "$1" -o ! -e "$2" ]
then
echo "userlist or passlist is not exist ,please inspect!!"
fi
userline=`awk 'EGIN{n=0}{n++}END{print n}' $1`
#echo $userline
passline=`awk 'BEGIN{N=0}{N++}END{print N}' $2`
if [ $userline != $passline ]
then
echo "Please keep two files same lines to continue!!$0"
fi
for num in `seq 1 $userline`
do
#echo $num
username=`sed -n ${num}p $1`
password=`sed -n ${num}p $2`
#echo $username
#echo $password
useradd $username &>/dev/null &&{
echo $password | passwd --stdin $username &>/dev/null
}||{
echo "$username is exist!!!"
}
done
if [ "$#" -lt 1 ]
then
echo "please input userlist and passlist after $0"
exit 1
elif [ ! -e "$1" ]
then
echo "用户文件为空,请检查文件!!"
fi
userline=`awk 'BEGIN{n=0}{n++}END{print n}' $1`
for NUM in `seq 1 $userline`
#for NUM in 1 $userline
do
username=`sed -n ${NUM}p $1`
id $username &> /dev/null &&{
userdel -r $username &>/dev/null
}
done
11:简单计算器:
read -p "Please input first num~:" anum
read -p "Please input what you want to do~:" choice
read -p "Please input second num~:" bnum
[ -z "$anum" -o -z "$choice" -o -z "$bnum" ] &&{
echo "Please input right message"
exit 1
}
bc <
12:9*9乘法表:
for ((i=1;i<=9;i++))
do
# echo $i
for ((c=1;c<=i;c++))
do
num=$(($i*$c))
echo -ne "$i*$c=$num \t"
done
echo -e "\n"
done