练习脚本总结

一、case运用

#!/bin/bash
#第1段程序,用case语句实现一个选择语句#
#read -p "请输入一个名字:" i
#case $i in
#redhat)
#   echo "fedora";;
#fedora)
#   echo "redhat";;
#*)
#   echo "Error";;
#esac


#第2段程序,用case语句实现一个命令的多个选项功能,比if语句简单#
#case $1 in
#-n|--new)
#  touch $2;;
#-e|--edit)
#  vim $2;;
#-c|--cat)
#  cat $2;;
#-r|--remove)
#  rm -rf $2;;
#*)
#  echo "Usage:$0 (-n|-e|-c|-r) 文件名";;
#esac

二、for运用

#!/bin/bash
#for i in 1 aa c 88
#do
#   echo $i
#done

for i in `cat /opt/user.txt`
do
   useradd user$i 2>/opt/userfalse.log
done

三、while运用

#!/bin/bash
i=1
while [ $i -le 5 ]
do
  echo "请等待..."
#  let i++
   i=i++
done

四、猜数字

#!/bin/bash

#  “下面的这段代码有BUG,假如没有输入数值,会报错”
#x=$[RANDOM%10]
#read -p "10 以内数字请猜一猜:" cai
#
#   if [ $cai -gt $x ];then
#       echo "对不起,你猜大了,正确答案是:$x"
#   elif [ $cai -lt $x ];then
#       echo "对不起,你猜小了,正确答案是:$x"
#   else
#       echo "恭喜你,猜对了,正确答案就是:$x"
#   fi



#  "不猜到正确不停止,显示猜的次数"
#x=$[RANDOM%10]
#i=0
#while :
#do
#  read -p "10 以内的数字猜一猜:" cai
#  let i++  
#    if [ $cai -gt $x ];then
#        echo "对不起,你猜大了"
#    elif [ $cai -lt $x ];then
#        echo "对不起,你猜小了"
#    else
#        echo "恭喜你,猜对了,正确答案就是:$x ,你一共猜了$i 次"
#        exit
#    fi
#done


#  "下面再来一段升级代码解决BUG"
x=$[RANDOM%10]
i=0
while :
do
  read -p "10 以内的数字猜一猜:" cai
  let i++  
    if [ $cai -gt $x ];then
        echo "对不起,你猜大了"
    elif [ $cai -lt $x ];then
        echo "对不起,你猜小了"
    else
        echo "恭喜你,猜对了,正确答案就是:$x ,你一共猜了$i 次"
        exit
    fi
done

五、端口状态

#!/bin/bash
#     rpm -q httpd  &> /dev/null
#
#    if [ ! $? -eq 0 ];then
#         yum -y install httpd   &> /dev/null
#    fi
#
#        # systemctl status httpd  &> /dev/null
#          netstat -antpu |grep 80
#    if [ ! $? -eq 0 ];then
#          systemctl restart httpd  
#    fi

     rpm -q httpd  &> /dev/null
    if [ ! $? -eq 0 ];then
     yum -y install httpd   &> /dev/null
    fi
        # systemctl status httpd  &> /dev/null
         netstat -antpu |grep 80
    if [ ! $? -eq 0 ];then
           systemctl restart httpd
          if [ $? == 0 ];then
           echo "重启成功"
          fi
    fi

六、非交互发邮件

#!/bin/bash
mail -s "hello" root << EOF
first
second
third
...
tenth
EOF

七、函数

#!/bin/bash
cecho(){
   echo -e "33[$1m$233[0m"
}

cecho 32 OK
cecho 33 OK
cecho 34 Error
cecho 35 Hehe
八、录入IP

#!/bin/bash
i=1
while :
do
  read -p "请输入over结束或者输入你要录入的IP:" ip
  [ $ip == over ] && break
  x[$i]=$ip
  echo $ip >> /opt/temp/ip.log
  let i++
done
#echo ${x[@]}
echo ${x[*]}

九、录数统计

#!/bin/bash
sum=0
while :
do
  read -p "请输入数字:" num
  [ $num -eq 0 ] && break
#  let sum +=`$num`
  sum=$[sum+num]
done
echo "你输入的数字总和为:$sum"

十、排序

#!/bin/bash
read -p "请输入第一个整数:" num1
read -p "请输入第二个整数:" num2
read -p "请输入第三个整数:" num3

#第一种方法:
#先定出num3为最大值,再定出num1、num2的大小
#if [ $num1 -gt $num3 ];then
#     t=$num3
#     num3=$num1
#     num1=$t
#fi
#if [ $num2 -gt $num3 ];then
#     t=$num3
#     num3=$num2
#     num2=$t
#fi
#if [ $num1 -gt $num2 ];then
#     t=$num2
#     num2=$num1
#     num1=$t
#fi
#echo "$num1,$num2,$num3"



#第二种方法:
#老师的方法,先定num1为最小值,然后比较num2、num3的大小
#if [ $num1 -gt $num2 ];then
#tmp=$num1
#num1=$num2
#num2=$tmp
#fi
#if [ $num1 -gt $num3 ];then
#tmp=$num1
#num1=$num3
#num3=$tmp
#fi
#if [ $num2 -gt $num3 ];then
#tmp=$num2
#num2=$num3
#num3=$tmp
#fi
#echo "排序为:$num1,$num2,$num3"




#第三种方法:
#自己的方法,先3、2比较:大数下沉小数上浮;然后2、1比较:大数下沉小数上浮;最后3、2比较:大数下沉小数上浮,最终确定1、2、3三个数的顺序
#if [ $num3 -lt $num2 ];then
#     t=$num2
#     num2=$num3
#     num3=$t
#fi
#if [ $num2 -lt $num1 ];then
#     t=$num1
#     num1=$num2
#     num2=$t
#fi
#if [ $num3 -lt $num2 ];then
#     t=$num2
#     num2=$num3
#     num3=$t
#fi
#echo "排序为:$num1,$num2,$num3"




#第四种方法:
#和上面的第三种思想一样
#if [ $num2 -lt $num1 ];then
#   t=$num1
#   num1=$num2
#   num2=$t
#fi
#if [ $num3 -lt $num2 ];then
#   t=$num2
#   num2=$num3
#   num3=$t
#fi
#if [ $num2 -lt $num1 ];then
#   t=$num1
#   num1=$num2
#   num2=$t
#fi
#echo "排序为:$num1,$num2,$num3

十一、批量更改文件名

#!/bin/bash
touch /opt/temp/{a,b,c,d,e,f}.txt
for i in `ls /opt/temp/*.txt`
do
  mv $i ${i%.*}.doc  #这里两个变量i前面不能用绝对路径,为什么?
done

十二、ping命令

#!/bin/bash
read -p "请输入ip地址:" ip
ping -c2 -i0.1 -W1 $ip   &> /dev/null
#ping -c2    ip  表示ping2次这个ip,c和2之间可以有空格
#ping -i0.1  ip  表示ping这个ip的间隔时间0.1秒
#ping -W1    ip  表示ping这个ip的超时时间1秒
if [ $? == 0 ];then
  echo "$ip 成功ping通"
else
  echo "$ip 未能ping通"
fi

十三、取6为随机密码

#!/bin/bash
X=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
pass=''
for i in {1..6}
do
  n=$[RANDOM%62]    #26小写字母+26大写字母+10个数字=62
  tmp=${X:$n:1}
  pass=${pass}$tmp
done
echo $pass

十四、数列求和

#!/bin/bash
read -p "请输入一个正整数:" n
#n=${n:-100}
#sum=0
#for i in `seq $n`
#do
#  # let sum=($sum+$i)
#  #  sum=$[$sum+$i]
#   sum=$[sum+i]
#done
#echo "从1到$n 的总和是:"$sum

n=${n:-100}
i=1;sum=0
while [ $i -le $n ]
do
   let sum+=i
   let i++
done
echo "从1到$n 的总和是:"$sum

十五、添加用户

#!/bin/bash
#第一段是一般添加用户
#read -p "请输入用户名:" user
#[ -z $user ] && echo "请输入正确的用户名" && exit
#read -p "请输入密码:" pass
#[ -z $pass ] && echo "请输入正确的密码" && exit
#useradd $user
#echo $pass |passwd --stdin $user


#第二段是添加用户时没有输入密码。会默认给个默密码
#read -p "请输入用户名:" user
#read -p "请输入密码:" pass
#[ -z $user ] && exit
#pass=${pass:-123456}

#useradd $user
#echo $pass |passwd --stdin $user
十六、超用户报警

#!/bin/bash
x=`who | wc -l`
[ x > 2 ] && echo "有人入侵电脑啦!!!"|mail -s 'overuser' root

#crontab -e -u root /opt/jiaoben/useralert 可以用这个周期计划任务监控

十七、远程非交互

#!/bin/bash
rm -rf /root/.ssh/known_hosts     #所有已经连接过的主机存储为这个位置,删掉后会>提示未连接过的主机,要不要继续远程登录
expect << EOF
spawn ssh 172.25.0.10    
expect "continue" {send "yes "}
expect "password" {send "redhat "}
expect "#"      {send "touch /a.txt "}
expect "#"      {send "exit "}
EOF

#spawn表示将交互式的转化为非交互,expect表示期待出现一个值后,发送什么命令

十八、中断

#!/bin/bash
for i in {1..5}
do
   [ $i -eq 3 ] && continue
   echo $i
done
echo OK


#for i in {1..5}
#do
#   [ $i -eq 3 ] && break
#   echo $i
#done
#echo OK


#for i in {1..5}
#do
#   [ $i -eq 3 ] && exit
#   echo $i
#done
#echo OK

二十、自动划分分区(待完善)

#!/bin/bash
read -p "请输入你要划分的分区个数(默认每个分区500M):" i
for $i in `seq $[i-1]`
do
    fdisk /dev/vdb << EOF
n
p
$[i+1]

+500M
w
EOF
done

####需要完善!!!!!#########

原文地址:https://www.cnblogs.com/baichuanhuihai/p/8134100.html