1、if语句练习
1、编写脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
#!/bin/bash
read -p "please input a username: " username
if id $username &>/dev/null;then
echo "the $username is exist"
exit 10
else
useradd $username &>/dev/null&& echo "$username is created"
id $username
fi
2、编写脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
#!/bin/bash
read -p "please input yes or no: " ans
case $ans in
[Yy][eE][Ss]|[Yy])
echo "yes"
;;
[Nn][oO]|[nN])
echo "no"
;;
*)
echo "other"
esac
3、编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
#!/bin/bash
read -p "请输入文件路径: " file
if [ -f $file ];then
echo "这是一个普通文件"
elif [ -d $file ];then
echo "这是一个目录文件"
elif [ -h $file ];then
echo "这是一个连接文件"
else
echo "这是一个其他文件类型"
fi
4、编写脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数
#!/bin/bash
read -p "please input a integer: " num
if [[ "$num" =~ ^[0-9]+$ ]];then
echo "the $num is positive integer"
else
echo "echo the $num is not positive integer"
fi
2、for循环练习
1、判断/var/目录下所有文件的类型
#!/bin/bash
for type in /var/*;do
if [ -f $type ];then
echo "$type是一个普通文件"
elif [ -d $type ];then
echo "$type是一个目录文件"
elif [ -L $type ];then
echo "$type是一个链接文件"
else
echo "$type是一个其他文件"
fi
done
2、添加10个用户user1-user10,密码为8位随机字符
#!/bin/bash
>/app/user.log
for i in {1..10};do
if id user$i &>/dev/null;then
echo "the user$i is exist"
exit 1
fi
useradd user$i && echo "the user$i is created"
passwd=`tr -dc '[[:alnum:]]'< /dev/urandom|head -c 8`
echo user$i:$passwd>>/app/user.log
echo $passwd |passwd --stdin user$i &>/dev/null
passwd -e user$i &>/dev/null
done
3、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start
#!/bin/bash
for rc in /etc/rc.d/rc3.d/*;do
if [[ $rc =~ /.*/S.*$ ]];then
echo "`basename $rc` start"
elif [[ $rc =~ /.*/K.*$ ]];then
echo "`basename $rc` stop"
fi
done
4、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和
#!/bin/bash
read -p "please input positive integer: " num
if [[ ! "$num" =~ ^[0-9]+$ ]];then
echo "you must input positive integer"
exit 10
else
echo `seq $num|tr "\n" "+"|sed -r 's/.*\+$/&0/'`|bc #可以简写为 echo `seq -s + $num|bc`
fi
1 #!/bin/bash
2 read -p "请输入正整数:" n
3 sum=0
4 for n in `eval echo {1..$n}`;do
5 let sum+=n
6 done
7 echo sum is $sum
5、计算100以内所有能被3整除的整数之和
[root@redhat7 app]#echo {0..100..3}|tr ' ' '+'|bc
1683
!#/bin/bash
2 sum=0
3 for i in {1..100};do
4 if [ $[i%3] -eq 0 ];then
5 let sum+=i
6 fi
7 done
8 echo sum is $sum
6、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态
read -p "please input the ip: " ip
if ping -c1 -w1 $ip &>/dev/null;then #-c指明ping的次数,-w指明一次ping多长时间,如果超过规定时间还是ping不通,就不ping了
echo "the $ip is throuh"
else
echo "the $ip is not throuh"
fi
7、打印九九乘法表
#!/bin/bash
for i in `seq 9`;do
for j in `seq $i`;do
let z=i*j
echo -e "$i*$j=$z\t\c"
done
echo
done
8、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html
#!/bin/bash
for n in {1..10};do
alpha=`tr -dc "[[:alpha:]]"</dev/urandom|head -c 8`
# alpha也可以定义为下面的方式
# alpha=`openssl rand -base64 8|tr -dc "[[:alpha:]]"|head -c 8`
touch /app/$n$alpha.html
done
9、打印等腰三角形
#!/bin/bash
read -p "请输入等腰三角形的行数: " num
if [[ ! "$num" =~ ^[0-9]+$ ]];then
echo "你输入的不是整数"
exit 10
fi
for i in `seq $num`;do
let word=i*2-1
let space=num-i
if [ "$space" -eq 0 ];then
printf %${word}s " "|tr " " "*"
echo
else
printf %${space}s " "
printf %${word}s " "|tr " " "*"
printf %${space}s " "
echo
fi
done
#!/bin/bash
read -p "请输入等腰三角形的行数:" line
if [[ ! $line =~ ^[1-9]|[0-9]{2,} ]];then
echo "你输入的不是整数"
exit 100
fi
for i in `seq $line`;do
let word=i*2-1 ---计算出每行需要打印的*的个数
let space=line-i ---计算处每行需要打印的空格的个数
for k in `seq $space`;do ----打印空格的
echo -n " "
done
for j in `seq $word`;do ----打印*的次数
echo -n "*"
done
echo
done