北风留影 发表于 2018-8-26 09:09:54

shell逻辑控制语句之case-lyw

  case分支判断结构 
  语法:
  case 变量名称 in
  value1)
  statement
  statement
  ;;
  value2)
  statement
  statement
  ;;
  value3)
  statement
  statement
  ;;
  *)
  statement
  statement
  ;;
  esac
  编写脚本,判断用户输入的字符串
  #!/bin/bash
  #
  read -p "Enter string: " str
  case $str in
  linux|Linux)
  echo "windows."
  ;;
  windows|Windows)
  echo "linux."
  ;;
  *)
  echo "other."
  ;;
  esac
  特殊变量:
  位置变量
  $1, $2 ,$3 ,$4 ...... $9, ${10},
  $1: 命令的第1个参数 
  $0命令本身 
  $#命令参数的个数
  使用位置变量:
  #!/bin/bash
  #
  case $1 in
  linux)
  echo "windows."
  ;;
  windows)
  echo "linux"
  ;;
  *)
  echo "other"
  ;;
  esac
  #!/bin/bash
  #
  if [ -z $1 ]; then
  echo "用法:./11.sh {linux|windows|other}"
  exit 9
  fi
  case $1 in
  linux)
  echo "windows."
  ;;
  windows)
  echo "linux"
  ;;
  *)
  echo "other"
  ;;
  esac
  使用$#判断参数是否正确 
  #!/bin/bash
  #
  if [ $# -ne 1 ]; then
  echo "用法:./11.sh {linux|windows|other}"
  exit 9
  fi
  case $1 in
  linux)
  echo "windows."
  ;;
  windows)
  echo "linux"
  ;;
  *)
  echo "other"
  ;;
  esac
  # basename /etc/sysconfig/network-scripts/ifcfg-eth0 >>>获取文件名称
  ifcfg-eth0
  # dirname /etc/sysconfig/network-scripts/ifcfg-eth0 >>>获取文件所在的目录名称 
  /etc/sysconfig/network-scripts
  #

页: [1]
查看完整版本: shell逻辑控制语句之case-lyw