设为首页 收藏本站
查看: 893|回复: 0

shell脚本应用及循环语句

[复制链接]

尚未签到

发表于 2018-8-18 11:18:40 | 显示全部楼层 |阅读模式
  Shell脚本应用及循环语句
  一.bash通配符:
  1. ?//任意的单个字符
  2.*//0个或多个字符
  3. []      //区间内的任意一个字符
  4.;       //分割命令,忽略前一个命令的执行结果,继续执行后面的命令
  5.&       //后台执行程序
  6.&&      //前面的命令执行成功,返回值是0,执行后面的命令。
  7.||      //前面的命令执行失败,返回值非0,执行后面的命令。
  8. |       //管道
  9.()      //开启子shell,执行里面的命令
  10.{}      //里面的命令在当前shell执行
  11.> >>    //输出重定向
  12.<   str2 检查str1是否大于str2
  -n   str1    检查str1的长度是否大于0
  -z   str1    检查str1的长度是否为0
  =~:判断左边的字符串是否能够被右边的模式所匹配,[[ &quot;$opt1&quot; =~ pattern ]],  注:判断字符串的时候,字符串要用&quot;&quot;包起来(更详细的man test)
  (2)整数的判断
  -eq  等于
  -ge  大于等于
  -gt   大于
  -le   小于等于
  -lt    小于
  -ne   不等于
  (3)文件的判断
  -b file   判断是否存在且为块文件
  -c file 判断是否存在且为字符文件
  -d file 判断是否存在且为目录文件
  -e file    判断文件是否存在
  -f file    判断文件是否存在且为一个普通文件
  -h file 判断是否存在且为符号链接
  -r file 判断是否存在且可读
  -s file 判断是否存在且不为空
  -w file 判断是否存在且可写
  -x file  判断是否存在且可执行
  -O file 判断是否存在且用户为当前用户
  -G file 判断是否存在且组为当前组
  (4)条件语句
  与 -a
  if [ 表达式1 -a 表达式2 ]
  或 -o
  if [ 表达式1 -o 表达式2 ]
  非 !
  if [ ! 表达式 ]
  注
  [[ 表达式1 && 表达式2 ]]  == -a
  [[ 表达式1 || 表达式2 ]]  == -o
  [[ ! 表达式 ]]      == !
  if 表达式
  then
  command
  elif 表达式
  then
  command
  elif 表达式
  then
  command
  ......
  else
  command
  fi
  (5)多重判断
  例1.
  注:read -p
  -t  超时时间    -t 3(秒)
  -s  输入没有回显,(密码)
  [root@tx1 ~]# vim tx1.sh
  #!/bin/bash
  #这是一个判断文件类型的脚本
  if [ -b /dev/hda1 ]
  then
  echo &quot;块设备&quot;
  fi
  if [ -d /etc ]
  then
  echo &quot;目录&quot;
  fi
  if [ -e /etc/passwd ]
  then
  echo &quot;普通文件&quot;
  fi
  [root@tx1 ~]# ./tx1.sh
  块设备
  目录
  普通文件
  例2
  #!/bin/bash
  #这是一个判断用户是否存在的脚本
  read -p &quot;请输入一个用户名:&quot; uname
  if [ &quot;$uname&quot; = &quot;&quot; ]
  then
  echo &quot;error&quot;
  else
  if grep &quot;^\&quot; /etc/passwd &> /dev/null
  then
  echo &quot;这个用户存在&quot;
  else
  echo &quot;这个用户不存在&quot;
  fi
  fi
  例3(if的嵌套)
  注:为终端加密
  [root@tx1 ~]# vim /etc/bashrc
  read -p &quot;请输入用户名:&quot; uname
  if [ &quot;$uname&quot; = &quot;root&quot; ]
  then
  read -p &quot;请输入密码:&quot; pass
  if [ &quot;$pass&quot; = &quot;123&quot; ]
  then
  echo &quot;welcome root&quot;
  else
  exit 1
  fi
  else
  exit 2
  fi
  打开一个新的终端后
  请输入用户名:root
  请输入密码:123
  welcome root
  [root@tx1 ~]#
  例4
  #!/bin/bash
  # 使用双重条件判断用户名和密码
  read -t 5 -p &quot;username: &quot; uname
  read -s -p &quot;password: &quot; pass
  echo
  if [ &quot;$uname&quot; = &quot;root&quot; -a &quot;$pass&quot; = &quot;123&quot; ]
  then
  echo &quot;welcome root&quot;
  else
  echo &quot;go out&quot;
  fi
  例5
  #!/bin/bash
  #判断一个用户的类型
  read -p &quot;请输入一个用户名:&quot; uname
  id=`grep &quot;^\&quot; /etc/passwd | cut -d : -f 3 `
  if [ &quot;$uname&quot; = &quot;&quot; ]
  then
  echo &quot;error&quot;
  elif [ $id -ge 500 ]
  then
  echo &quot;$uname 是普通用户&quot;
  elif [ $id -lt 500 -a $id -ge 1 ]
  then
  echo &quot;$uname 是系统用户&quot;
  else
  echo &quot;$uname 是超级用户&quot;
  fi
  [root@tx1 ~]# ./tx5.sh
  请输入一个用户名:root
  root 是超级用户
  [root@tx1 ~]# ./tx5.sh
  请输入一个用户名:tx
  tx 是普通用户
  [root@tx1 ~]# ./tx5.sh
  请输入一个用户名:bin
  bin 是系统用户
  [root@tx1 ~]# ./tx5.sh
  请输入一个用户名:
  error
  四.case 多分支判断
  语法:
  case $变量 in
  value1)
  commands
  ;;
  value2)
  commands
  ;;
  ......
  *)
  commands
  ;;
  esac
  例1.
  注:例如人的年龄: 0 婴儿 ,1-9 幼儿,1-19 少年,20-29 青年,30-39 中年,40-老年
  #!/bin/bash
  # 判断人的年龄段
  read -p &quot;请输入一个人的年龄:&quot; age
  case $age in
  0)
  echo &quot;婴儿&quot;
  ;;
  [1-9])
  echo &quot;幼儿&quot;
  ;;
  1[0-9])
  echo &quot;少年&quot;
  ;;
  2[0-9])
  echo &quot;青年&quot;
  ;;
  3[0-9])
  echo &quot;中年&quot;
  ;;
  *)
  echo &quot;老年&quot;
  ;;
  esac
  [root@tx1 ~]# ./t1.sh
  请输入一个人的年龄:3
  幼儿
  [root@tx1 ~]# ./t1.sh
  请输入一个人的年龄:67
  老年
  [root@tx1 ~]# ./t1.sh
  请输入一个人的年龄:20
  青年
  五.For循环
  注:for循环    事先提供一个元素列表,而后,使用变量去遍历此元素列表,每访问一个元素,就执行一次循环体,直到元素访问完毕
  1.语法:
  for 变量 in 变量列表
  do
  commands
  done
  例1. 用for循环输出三个数
  #!/bin/bash
  for i in &quot;$*&quot;
  do
  echo $i
  done
  for j in &quot;$@&quot;
  do
  echo $j
  done
  [root@tx1 ~]# ./t3.sh 1 2 3
  1 2 3
  1
  2
  3
  例2.把 /usr/share/doc/ 下所有的index.html复制到 /tmp/index/目录下,/tmp/index/  不确定是否存在。
  #!/bin/bash
  if [ -d /tmp/index ]
  then
  num=1
  for i in $(find /usr/share/doc -name index.html)
  do
  /bin/cp $i /tmp/index/index.html.$num
  num=$(($num+1))
  done
  else
  mkdir /tmp/index
  num=1
  for i in $(find /usr/share/doc -name index.html)
  do
  /bin/cp $i /tmp/index/index.html.$num
  num=$(($num+1))
  done
  fi
  六.While语句
  语法:
  while [ 表达式 ]
  do
  commands
  更新表达式
  done
  表达式的作用:判定循环是否执行。
  表达式的返回值为真, $?=0,执行命令
  表达式的返回值是假的,结束循环。
  : ;true  返回值永远是真的。
  例1.批量创建5个用户
  [root@tx1 ~]# cat t1.txt
  tx1
  tx2
  tx3
  tx4
  tx5
  [root@tx1 ~]# cat  t6.sh
  #!/bin/bash
  while read line
  do
  uname=`echo $line`
  useradd $uname
  echo $uname | passwd $uname --stdin
  done < /root/t1.txt
  例2.信号捕捉
  注:信号捕捉,trap,1 2 9 15 ,信号9是捕捉不了的。
  #!/bin/bash
  #信号捕捉
  trap &quot;echo 进程还在继续&quot; 1
  while :
  do
  echo &quot;hello&quot;
  sleep 3
  done
  [root@tx1 ~]# ps -a
  PID TTY          TIME CMD
  18810 pts/2    00:00:00 bash
  18813 pts/2    00:00:00 sleep
  18814 pts/1    00:00:00 ps
  [root@tx1 ~]# kill -1 18810
  [root@tx1 ~]# ./t7.sh
  hello
  hello
  hello
  hello
  进程还在继续
  hello
  七.Until语句
  语法:
  until [ 表达式 ]
  do
  commands
  更新表达式
  done
  表达式的作用:判定循环是否执行。
  表达式的返回值为假的时候,执行循环,$? 非0
  表达式的返回值为真的时候,结束循环
  例1.
  #!/bin/sh
  a=10;
  until [[ $a -lt 0 ]];do
  echo $a;
  ((a--));
  done;
  [root@tx1 ~]# ./t8.sh
  10
  9
  8
  7
  6
  5
  4
  3
  2
  1
  0
  八.shift,break和continue
  例1
  #!/bin/bash
  until [ $# -eq 0 ]
  do
  echo $*
  shift
  done
  [root@tx1 ~]# ./tt1.sh 1 2 3 4 5 6
  1 2 3 4 5 6
  2 3 4 5 6
  3 4 5 6
  4 5 6
  5 6
  6
  例2.break跳出循环
  #!/bin/bash
  for i in `seq 1 2`
  do
  for j in `seq 1 3`
  do
  echo $j
  if [ $j -eq 2 ]
  then
  break   #跳出当前循环
  fi
  done
  done
  [root@tx1 ~]# ./tt2.sh
  1
  2
  1
  2
  例3
  #!/bin/bash
  for i in `seq 1 10`
  do
  for j in `seq 1 3`
  do
  echo $j
  if [ $j -eq 2 ]
  then
  break 2  #跳出第2层循环
  fi
  done
  done
  [root@tx1 ~]# ./tt3.sh
  1
  2
  例4 (continue结束本次循环)
  #!/bin/bash
  for i in `seq 1 3`
  do
  for j in `seq 1 3`
  do
  if [ $j -eq 2 ]
  then
  continue    #结束本次循环
  fi
  echo $j
  done
  done
  [root@tx1 ~]# ./tt4.sh
  1
  3
  1
  3
  1
  3
  例5
  #!/bin/bash
  for i in `seq 1 3`
  do
  for j in `seq 1 3`
  do
  if [ $j -eq 2 ]
  then
  continue 2  #结束第2层的本次循环
  fi
  echo $j
  done
  done
  [root@tx1 ~]# ./tt5.sh
  1
  1
  1


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-553388-1-1.html 上篇帖子: 一次shell的过滤IP 下篇帖子: shell脚本for读取文件列表同时读取两个变量
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表