检测 本机当前用户是否超级管理员
#!/bin bash
if [ $USER == “root” ];then
yum -y install vsftpd
else
echo “NO”
fi
~
~
~
8 提示用户输入密码 脚本自动创建相应账户及配置密码 如果未输入 体会用户输入密码 采用密码123456
#!/bin/bash
read -p "please input your user name :" user
if [ -z $user ] ; then
echo "qinhshuru"
exit 2
fi
stty -echo
read -p "qingshurumima:" pass
stty echo
useradd $user
pass=${pass:-123456}
echo "$pass" | passwd --stdin "$user"
9 创建用户
#!/bin/bash
read -p "please input your user name :" user
if [ -z $user ] ; then
echo "qinhshuru"
exit 2
fi
stty -echo
read -p "qingshurumima:" pass
stty echo
useradd $user
pass=${pass:-123456}
echo "$pass" | passwd --stdin "$user"
10 一次提示用户输入
#!/bin/bash
read -p "please inputp your number1:" num1
read -p "please inputp your number2:" num2
read -p "please inputp your number3:" num3
now=0
if [ $num1 -ge $num2 ];then
now=$num1
num1=$num2
num2=$now
fi
if [ $num1 -ge $num3 ];then
now=$num1
num1=$num3
num3=$now
fi
if [ $num2 -ge $num3 ];then
now=$num2
num2=$num3
num3=$now
fi
echo "$num1 $num2 $num3"
11 编写脚本,实现人机<石头,剪刀,布>游戏
#!/bin/bash
# ligang
# 20.03.2019
#
#===========================
for (( i = 0; i < 3; i++ ))
do
game=(石头 剪刀 布)
num=$[RANDOM%3]
computer=${game[$num]}
echo "请出拳"
echo "1 -石头"
echo "2 -剪刀"
echo "3 -布"
read -p "please input 1-3:" person
case $person in
1)
if [ $num -eq 0 ]; then
echo "pingju"
elif [ $num -eq 1 ];then
echo "yonghuying"
else
echo "diannaoying"
fi
2)
if [ $num -eq 0 ];then
echo "diannaoying"
elif [ $num -eq 0 ];then
echo "pingju"
else
echo "yonghuying"
fi
;;
3)
if [ $num -eq 0 ];then
echo "yonghuying"
elif [ $num -eq 0 ];then
echo "diannaiying"
else
echo "pingju"
fi
;;
esac
done
;;
12 编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机
状态(for 版本)
#!/bin/bash
#
#
#
#
myping ()
{
ping -c 2 -i 0.3 -w1 $1 &> /dev/null
if [ $? -eq 0 ]; then
echo "$1 is up"
else
echo "$1 is nown"
fi
}
for i in {1..245}
do
myping 192.168.1.$i &
done
13 编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机
状态(while 版本)
#!/bin/bash
i=1
while [ $i ‐le 254 ]
do
ping ‐c2 ‐i0.3 ‐W1 192.168.4.$i &>/dev/null
if [ $? –eq 0 ];then
echo "192.168.4.$i is up"
else
echo "192.168.4.$i is down"
fi
let i++
done
点名脚本
先创建一个name.txt 文件 在里面有人名
#!/bin/bash
while :
do
line=`cat name.txt | wc -l`
num=$[ RANDOM%line + 1 ]
sed -n "${num}p" name.txt
sleep 0.2
clear
done
~