shell-03

2/13/2017来源:经验技巧人气:1600

for循环的语法 格式1: for 变量 in 值1 值2 值3... do 命令 done   1 #!/bin/bash   2    3 for i in  `ls /var/log`   4 do   5   if [ -s /var/log/$i  ]&& [ -f /var/log/$i ];then   6         tar -zcf  /data/log/$i.tar.gz  /var/log/$i &>/dev/null   7    fi   8 done ~          

seq 9    表示1~9

seq  3  9   表示3~9

seq  3  3   100  表示3,6,9,12........100  (值为3的等差数列)

  

使用循环ping测试网络中所有主机 #!/bin/bash net=172.40.56 for i in `seq 100 254`  #反引号 do ping -c 2 -i 0.2 -w 0.2 $net.$i &>/dev/null if [ $? -eq 0 ];then     echo "该主机$net.$i,ping通" else echo "该主机$net.$i,无法ping通" fi done 批量创建账户的脚本 1素材:user.txt zhansan jerry lisi 2.test.sh #!/bin/bash for i in  `cat user.txt` #反引号 do useradd $i done +++++++++++++++++++++++++++ for循环的格式2: for ((i=1;i<=5;i++)) do 命令 done 制作9*9乘法表 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 ... ... ... #!/bin/bash for ((i=1;i<=9;i++)) do      for ((j=1;j<=$i;j++))      do         echo -n "$i*$j=$((i*j))"      done echo done #!/bin/bash for i in `seq 9` do      for j in `seq $i`      do         echo -n "$i*$j=$((i*j))"      done echo done ++++++++++++++++++++++++++++++++

字体色:30—–37      echo -e “\033[30m 黑色字 \033[0m”      echo -e “\033[31m 红色字 \033[0m”      echo -e “\033[32m 绿色字 \033[0m”      echo -e “\033[33m 黄色字 \033[0m”      echo -e “\033[34m 蓝色字 \033[0m”      echo -e “\033[35m 紫色字 \033[0m”      echo -e “\033[36m 天蓝字 \033[0m”      echo -e “\033[37m 白色字 \033[0m”       背景色:40—–47      echo -e “\033[40;37m 黑底白字 \033[0m”      echo -e “\033[41;37m 红底白字 \033[0m”      echo -e “\033[42;37m 绿底白字 \033[0m”      echo -e “\033[43;37m 黄底白字 \033[0m”      echo -e “\033[44;37m 蓝底白字 \033[0m”      echo -e “\033[45;37m 紫底白字 \033[0m”      echo -e “\033[46;37m 天蓝底白字 \033[0m”      echo -e “\033[47;30m 白底黑字 \033[0m” 

+++++++++++++++++++++++++++++++++

while循环格式

while 判断 do     命令 done i=15, while [ $i -le 20 ] do    echo hello done +++++++++++++++++++++++++++++ #!/bin/bash i=1 while [ $i -le 5 ] do         echo $i let i++ done +++++++++++++++++++++++++++ while : 死循环 do     命令 done +++++++++++++++++++++++++++++ 写个脚本,屏幕提示如下 1.查看ip 2.查看内存 3.查看CPU信息 4.查看硬盘 5.退出 请输入您的选项[1-5]: abc ++++++++++++++++++++++++++++++ #!/bin/bash while : do clear echo -e "\033[34m    菜单\033[0m" echo "1.查看IP 2.查看CPU 3.查看磁盘 4.查看内存 5.退出" read -p "请输入选项[1-5]:" xz if [ $xz -eq 1 ];then ifconfig eth0 |grep "inet add" echo -e "\033[34m按回车键继续...\033[0m" read  elif [ $xz -eq 2 ];then cat /PRoc/cpuinfo |grep vendor cat /proc/cpuinfo |grep MH cat /proc/cpuinfo |grep cache read -p "按回车键继续..." elif [ $xz -eq 3 ];then df -h read -p "按回车键继续..." elif [ $xz -eq 4 ];then free read -p "按回车键继续..." elif [ $xz -eq 5 ];then exit else read -p "您需要输入[1-5]:" xz fi done ++++++++++++++++++++++++++++ case语句 case 变量  in stop)    命令;; start)    命令;; *)    命令 esac 1.编写脚本,当用户输入参数redhat,则返回fefora,当用户输入参数fefora,返回redaht,其他返回Usage:脚本名 (redhat|fedora) #!/bin/bash case $1 in redhat)     echo "XXX" echo "fedora";; fedora) echo "redhat";; *) echo "用法" esac ++++++++++++++++++++++++++++ 编写Apache启动脚本: #yum -y install httpd #vim test.sh #!/bin/bash case $1 in start) /usr/sbin/httpd echo "我已经启动了";; stop) kill `cat /var/run/httpd/httpd.pid`;;   #反引号 restart) kill `cat /var/run/httpd/httpd.pid`     sleep 1 /usr/sbin/httpd;; status) if [ -f /var/run/httpd/httpd.pid ];then echo "httpd is 在运行..." else echo "没有running" fi;; *) echo "用法是$0 {start|stop|restart|status}" esac ++++++++++++++++++++++++++++++ 给Apache启动脚本添加开机启动功能: #vim test.sh #!/bin/bash #chkconfig: - 88 90 case $1 in start) /usr/sbin/httpd echo "我已经启动了";; stop) kill `cat /var/run/httpd/httpd.pid`;;   #反引号 restart) kill `cat /var/run/httpd/httpd.pid`     sleep 1 /usr/sbin/httpd;; status) if [ -f /var/run/httpd/httpd.pid ];then echo "httpd is 在运行..." else echo "没有running" fi;; *) echo "用法是$0 {start|stop|restart|status}" esac ++++++++++++++++++++++++++++++ #vim myhttp给启动脚本添加一行注释 #chkconfig: - 88 90

#chkconfig --add myhttpd #chkconfig --list myhttpd #chkconfig myhttpd on

#chkconfig myhttpd off

+++++++++++++++++++++++++ 判断用户输入的是数字、字母、标点符号 #!/bin/bash read -p "输入字符:" key case $key in [0-9]) echo 数字;; [a-Z]) echo "字母";; *) echo "其他" esac ++++++++++++++++++++++++++++++++ 函数:避免代码重复 先声明函数 再调用函数 声明函数的格式: 函数名称(){ 命令 } 示例1: #vim test.sh echo2(){   echo -e "\033[32mOK\033[0m" } echo2 echo2 示例2: #vim test.sh echo2(){   echo -e "\033[$1m$2\033[0m" } echo2 32 OK echo2 34 Error echo2 33 nihao

echo2 36 hello

示例3:

cecho(){

echo -e “\033[$1m $2 \033[0m”   

}

a=31

cecho  $a  OK           #显示红色字

+++++++++++++++++++++++++++++++++

Shell版本fork炸弹

递归死循环,可迅速消耗系统资源

#vim test.sh

.(){ .|.& } . #chmod +x test.sh #./test.sh +++++++++++++++++++++++++++++++++