1、编写脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
[ $# -lt 1 ] && echo "input username !" && exit
id $1 &> /dev/null
if [ $? -eq 1 ];then
useradd $1 ;echo -e "successful\n`id $1`"
else echo "user already exists"
fi
2、编写脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
read -p "yes or no " answer
if [[ $answer =~ ^[Yy]([Ee][Ss])?$ ]];then
echo "your anwser is yes"
elif [[ $answer =~ ^[Nn][Oo]?$ ]];then
echo "your anwer is no"
else
echo "other"
fi
3、编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
[ $# -ne 1 ] && echo "input file path"&&exit 1
if [ -d $1 ] ;then
echo dir
elif [ -h $1 ] ;then
echo link
elif [ -f $1 ] ;then
echo file
elif [ ! -e $1 ] ;then
echo not exist
else echo other
fi
4、编写脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数
read -p "input positive integer " num
if [[ $num =~ ^[1-9][0-9]*$ ]] ;then
echo right
else
echo "error"
fi
1、判断/var/目录下所有文件的类型
for i in `ls -A /var/` ; do
echo "$i -- `filetype15.sh /var/$i`"
done
2、添加10个用户user1-user10,密码为8位随机字符
> ./user.log
for i in {1..10} ;do
useradd user$i
mima=`openssl rand -base64 10|head -c8`
echo $mima|passwd --stdin user$i
echo "user$i $mima" >>./user.log
done
3、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start
for i in `ls /etc/rc.d/rc3.d` ;do
if [[ $i =~ ^K.*$ ]];then
echo "$i start"
elif [[ $i =~ ^S.*$ ]];then
echo "$i stop"
fi
done
4、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和
read -p "input a number " i
if [[ $i =~ [1-9][0-9]* ]];then
for j in `seq $i`;do
let sum+=j
done
echo $sum
else
echo error
fi
5、计算100以内所有能被3整除的整数之和
for i in `seq 100`;do
let sum=i%3
if [ $sum -eq 0 ];then
let add=add+$i
fi
done
echo $add
6、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态
read -p "input ipv4 address " ip
if [[ $ip =~ ^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]] ;then
nip=`echo $ip|cut -d. -f1-3`
for i in {1..254};do
ping -w1 -c1 $nip.$i &>/dev/null && echo "$nip.$i is up"|| echo "$nip.$i is down"&
done
wait
else
echo "error"
fi
7、打印九九乘法表
for i in `seq 9`;do
for j in `seq $i`;do
let k=$i*$j
echo -e "$j*$i=$k \c"
done
echo
done
8、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html
for i in `seq 10`;do
num=`cat /dev/urandom |tr -dc "[:alpha:]"|head -c8`
mkdir /app/$i$num.html
done
9、打印等腰三角形
read -p "input mun " t
if [[ $t =~ ^[1-9][0-9]*$ ]];then
for n in `seq $t`;do
let kmun=$t-$n
let smun=2*$n-1
for i in `seq $kmun`;do
echo -e " \c"
done
for i in `seq $smun`;do
echo -e "*\c"
done
echo
done
else
echo error
fi
10.国际象棋
for i in `seq 4`;do
for j in `seq 4`;do
for a in `seq 4`;do
echo -e "\033[46m \033[0m\c"
echo -e "\033[43m \033[0m\c"
done
echo
done
for k in `seq 4`;do
for b in `seq 4`;do
echo -e "\033[43m \033[0m\c"
echo -e "\033[46m \033[0m\c"
done
echo
done
done