1、编写脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之,并生成8位随机口令并存在一个文件中,初步提示改口令,显示添加的用户的id号等信息。
#!/bin/bash # ------------------------------------------ # Filename: cid.sh # Date: 2017-09-16 # Author: liuke # Description: 使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之# 生成8位随机口令并存在一个文件中,初步提示改口令,显示添加的用户的id号等信息。 # ------------------------------------------- read -p "please input a username:" uname id $uname &> /dev/null e=$? if [ $e -eq 0 ] then echo "$uname is exist" exit 2 else useradd $uname echo `cat /dev/urandom | tr -d -c '[:alnum:][:punct:]' | head -c 10` > /app/mima passwd $uname id $uname fi unset uanme e
2、编写脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
#!/bin/bash # ------------------------------------------ # Filename: yesno.sh # Revision: null # Date: 2017-09-10 # Author: liuke # Description: 提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息 # ------------------------------------------- read -p "Yue ma?yes or no:" word word=`echo $word|tr "A-Z" "a-z"` case $word in yes|y) echo "yue" ;; no|n) echo "buyue" ;; *) echo "error input" esac
3、编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
#!/bin/bash # ------------------------------------------ # Filename: filetype.sh # Date: 2017-09-11 # Author: liuke # Description: 判断用户输入文件路径,显示其文件类型 # ------------------------------------------- read -p "please input a file:" file if [ -f $file ];then echo "filetype is common file" elif [ -d $file ];then echo "filetype is directory" elif [ -h $file ];then echo "filetype is soft link" else echo "filetype is other" fi unset file
4、编写脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数
#!/bin/bash # ------------------------------------------ # Filename: checkint.sh # Date: 2017-09-11 # Author: liuke # Description: 判断用户输入的参数是否为正整数 # ------------------------------------------- read -p "please input a num:" num if [[ $num =~ ^[1-9][0-9]*$ ]];then echo "the num is a positive integer " else echo "wrong input" fi
5、判断/var/目录下所有文件的类型
#!/bin/bash # ------------------------------------------ # Filename: file-var.sh # Date: 2017-09-11 # Author: liuke # Description: 判断/var/目录下所有文件的类型 # ------------------------------------------ cd /var/;ls -1 | while read filename do if [ -f $filename ];then echo " $filename filetype is common file" elif [ -d $filename ];then echo "$filename filetype is directory" elif [ -h $filename ];then echo "$filename filetype is soft link" else echo "$filename filetype is other" fi done unset filename
6、添加10个用户user1-user10,密码为8位随机字符
#!/bin/bash # ------------------------------------------ # Filename: 10user.sh # Date: 2017-09-11 # Author: liuke # Description: 添加10个用户user1-user10,密码为8位随机字符 # ------------------------------------------- for ((i=1;i<= 10;i+=1)) do pw=`cat /dev/urandom |tr -d -c '[:alnum:][:punct:]'|head -c 8` useradd user$i && echo $pw|passwd --stdin user$i &> /dev/null echo -e "user:user$i;passwd:$pw" done # for i in {1..10};do userdel -r user$i;done 一次删除10个用户
7、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start
#!/bin/bash # ------------------------------------------ # Filename: rc3.sh # Date: 2017-09-16 # Author: liuke # Description: 分别读取/etc/rc.d/rc3.d/每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start # ------------------------------------------- for filename in `ls /etc/rc.d/rc3.d/`;do case $filename in K*) echo "$filename stop" ;; S*) echo "$filename start" esac done unset filename
8、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和
#!/bin/bash # ------------------------------------------ # Filename: 1-nhe.sh # Date: 2017-09-11 # Author: liuke # Description: 提示输入正整数n的值,计算1+2+…+n的总和 # ------------------------------------------- read -p "please input a num:" num if [[ $num =~ ^[1-9][0-9]*$ ]];then sum=0 for ((i=1;i<= $num;i+=1)) do sum=$(($sum+$i)) done echo "1+2+…+n的总和为:$sum" fi unset num sum
9、计算100以内所有能被3整除的整数之和
#!/bin/bash # ------------------------------------------ # Filename: zhengchu3.sh # Date: 2017-09-11 # Author: liuke # Description: 计算100以内所有能被3整除的整数之和 # ------------------------------------------- sum=0 for ((i=3;i<= 100;i+=3)) do sum=$(($sum+$i)) done echo "100以内所有能被3整除的整数之和为:$sum" unset sum
10、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态。
#!/bin/bash # ------------------------------------------ # Filename: scanip11-2.sh # Revision: null # Date: 2017-09-10 # Author: liuke # Description: 提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态。 # ------------------------------------------- > /app/ip.txt read -p "please input a ip:" ip echo $ip|egrep "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4]0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"&> /dev/null if [ $? -eq 0 ];then id=`echo $ip|egrep -o ".*\."` ping -c 1 -w 1 $ip &> /dev/null echo "$ip is up">>/app/ip.txt echo "$ip is down" fi
11、打印九九乘法表(for循环)
#!/bin/bash # ------------------------------------------ # Filename: 9*9.sh # Revision: null # Date: 2017-09-10 # Author: liuke # Description: 打印九九乘法表(for循环) # ------------------------------------------- for i in {1..9};do for j in `seq $i`;do echo -en "${i}X${j}=$[$ix$j]\t" done echo done
12、打印九九乘法表(while循环)
#!/bin/bash # ------------------------------------------ # Filename: 9*9while.sh # Date: 2017-09-11 # Author: liuke # Description: 打印九九乘法表(while) # ------------------------------------------- while ((i<10,j<9)) do echo -en "${i}X${j}=$[$i*$j|bc]\t" done echo
13、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html。
#!/bin/bash # ------------------------------------------ # Filename: 10user.sh # Date: 2017-09-11 # Author: liuke # Description: 添加10个用户user1-user10,密码为8位随机字符 # ------------------------------------------- for ((i=1;i<= 10;i+=1)) do pw=`cat /dev/urandom |tr -d -c '[:alnum:][:punct:]'|head -c 8` useradd user$i && echo $pw|passwd --stdin user$i &> /dev/null echo -e "user:user$i;passwd:$pw" done # for i in {1..10};do userdel -r user$i;done 一次删除10个用户
14、编写脚本,求100以内所有正奇数之和
#!/bin/bash # ------------------------------------------ # Filename: jishuhe.sh # Date: 2017-09-11 # Author: liuke # Description: 编写脚本,求100以内所有正奇数之和 # ------------------------------------------- sum=0;i=1 while ((i<100)) do sum=$[$sum + $i] let i=$[$i+2] done echo "100以内所有正奇数之和为:$sum"
15、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大值和最小值。
#!/bin/bash # ------------------------------------------ # Filename: random10-2.sh # Date: 2017-09-16 # Author: liuke # Description: 利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大值和最小值 # while # ------------------------------------------- i=1 while [ $i -lt 11 ];do sum=`echo $[RANDOM]` echo -e "$sum" "\c" if [ $i -le 1 ];then max=$sum min=$sum elif [[ $sum -gt $max ]];then max=$sum elif [[ $sum -lt $min ]];then min=$sum fi let i+=1 done echo echo "the number max is:$max;min number is:$min"
16、编写脚本,编辑菜单,用户输入菜单列表中的某个数字,显示相应价格。
#!/bin/bash # ------------------------------------------ # Filename: caidan.sh # Date: 2017-09-10 # Author: liuke # Description: 编辑菜单,用户输入菜单列表中的某个数字,显示相应价格。 # ------------------------------------------- echo "1=yangroutang 2=mifan 3=hulatang 4=jiaozi 5=lamian 6=huimian" read -p "please input your choose num:" num case $num in 1|4) echo "the price is 20¥" ;; 2|5) echo "the price is 12¥" ;; 3|6) echo "the price is 10¥" ;; *) echo "error choose" esac
17、编写脚本,实现打印国际象棋棋盘
#!/bin/bash # ------------------------------------------ # Filename: chess11.sh # Date: 2017-09-11 # Author: liuke # Description: 棋盘 # ------------------------------------------- for ((i=1;i<=8;i++));do for ((j=1;j<=8;j++));do total=$(($i+$j)) tmp=$(($total % 2)) if [ $tmp -eq 0 ] then echo -e -n "\033[43m \033[0m" else echo -e -n "\033[41m \033[0m" fi done echo done