SHELL脚本编程生产案例

Shell脚本编程

基础

编写脚本 backup.sh,可实现每日将/etc/目录备份/backup/etcYYYY-mm-dd中

#!/bin/bash
cp -av /etc/ /data/etc\`date +%F`                

编写脚本 hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”

#!/bin/bash
ping -w3 -c3 $1  &>/dev/null && echo "该IP地址可访问"  ||  echo "该IP地址不可访问" 

编写脚本 checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满

#!/bin/bash
CHECKDISK=`( df ; df -i )|egrep "^/dev/sda"|tr -s ' ' %|cut -d'%' -f5|sort -nr |head -1`
[ $CHECKDISK -gt 80 ] &>/dev/null && echo "磁盘空间或节点编号空间已达到80%" |mail -s "空间已满" root 

编写脚本 per.sh,判断当前用户对指定参数文件,是否不可读并且不可写

#!/bin/bash
[ ! -r  $1 -a ! -w $1 ]&>/dev/null && echo "该文件不可读写" || echo " 该文件可读或写"

让所有用户的PATH环境变量的值多出一个路径,例如/usr/local/apache/bin

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
echo 'PATH=/data/sh:$PATH' >> /etc/profile
source /etc/profile
echo $PATH
/data/sh:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”

echo -e "\033[1;31m Hi,dangerous!\033[0m" >>/etc/motd 

if,case

编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息

#!/bin/bash
if  id $1  &>/dev/null; then
  echo "id $1 existed"
else 
  useradd $1
  getent passwd $1
fi

编写脚本 yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息

#!/bin/bash
read -p "Do you like me :" YN
q=`echo $q | tr [A-Z] [a-z]`
case  $YN in 
y|yes)
   echo "YES"
    ;;
n|no)
  echo "NO"
    ;;
*)
  echo "输错误"
  esac 

For,while

打印99乘法表

#!/bin/bash
for i in {0..9} ;do                        
   for j in `seq $i` ;do                
   echo -e "${i}x${j}=$[i*j]\t\c"          
   done                                    
    echo                               
done                                       

判断/var下所有文件类型

#!/bin/bash
FF=`find /var/*`
for FILE in $FF ;do
  if [ -h $FILE  ] ;then
      echo "$FILE 是链接文件"
  elif [  -h $FILE   ];then
      echo  "$FILE 是文件夹"
  elif [  -f $FILE ];then
      echo "$FILE 是普通文件"                                                           
  elif [  -b $FILE ] ;then
     echo " $FILE 是块设备文件"
  elif [ -c $FILE ] ;then
     echo " $FILE 是字符设备文件"
  elif [  -p $FILE ];then
     echo "$FILE 是管道文件"
  else
     echo "$FILE 是其他类型文件"
   fi
 done

添加10个用户user1-10,密码为10位随机数

#!/bin/bash
user=`echo user{0..10}|xargs -n1`                                                        for NAME in $user ;do
   useradd $NAME
   mima=` cat /dev/random |tr -dc '[[:alnum:]]'|head -c10`
   echo "$mima" |passwd --stdin $NAME >/dev/null
   echo "$NAME:$mima" >>passwd.txt
 done

为etc/rc.d/rc3.d下的以k开头的文件加上stop,以s开头的文件加上start

file=`ls /etc/rc.d/rc3.d/`
for f in $file ;do
   cd /etc/rc.d/rc3.d
   if [[ $f == k*  ]] ;then
    mv $f $f-stop
  elif [[ $f == s*  ]] ;then
    mv $f $f-strat
fi
done

将/data/下所有文件修改为.bak后缀

#!/bin/bash
cd /data/test
for FI in * ;do
 pwe=`echo $FI |sed -nr 's/(.\*)\.([^.]+)$/\1/p'`
   mv $FI $pwe.bak
done

提示输入正整数N的值,计算1+2+3…+N的值

#!/bin/bash
read -p "请输入正整数N:" N
t=0
for((i=1;i<=$N;i++));do
   let t+=i
done
  echo $t

判断ip地址是否能连通

#!/bin/bash
ipa="192.168.57"
for ((i=100;i<=200;i++)) ;do
ping -w1 -c1 $ipa.$i &>/dev/null && echo "$ipa.$i 可以连接" || echo "$ipa.$i 无法连接"
Done

计算100以内能被3整除的数的和

#!/bin/bash
y=0
for u in `seq 100` ;do
let  a=$u%3
   If [ $a = 0 ] ;then
let y+=u
fi
  done
  echo $y
1683

多参数循环,即脚本名后面跟多个参数shift会一一执行

while [ $1 ] ;do
   echo $1
   shift
done  

无限循环,直到磁盘空间已满80

kongjian=`df | awk -F"[[:space:]]+|%"  '/^\/dev\/sda/{print $5}'|sort -nr|head -1`
while true; do
   if [ $kongjian -ge 80  ] ;then
  echo "磁盘空间已满" |mail -s "报警" 18559002373@163.com
  break
  fi
  sleep 30
done

特殊用法,依次读取行处理

while read line ;do
  echo  $line | awk -F: '/\<sbin\>/{print $1,$3,$7}'                          
done < /etc/passwd

函数,数组

编写函数实现OS的版本判断

yyy(){
cat /etc/redhat-release | sed -nr 's/.*\ ([0-9]{1}).*/\1/p'
}
yyy

编写函数实现取出ip地址

ip(){
ifconfig | grep -o "\([0-9]\{1,3\}.\)\{3\}[0-9]\{1,3\}"|head -1
}
ip   

生成十个随机数字,找出最大值和最小值

declare -a quanbu
for ((i=0;i<10;i++));do
    quanbu[$i]=$RANDOM
  if [ $i -eq 0  ] ;then
   xiao=${quanbu[$i]} && da=${quanbu[i]} && continue
   elif [ ${quanbu[$i]} -gt  $da  ] ;then
   da=${quanbu[$i]}
   elif [ ${quanbu[$i]} -lt $xiao ] ;then
    xiao=${quanbu[$i]}
   fi
 done
     echo "${quanbu[*]}" 
    echo "最大值为$da"
    echo "最小值为$xiao"**
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值