xinghe0 发表于 2018-8-27 12:07:57

[]和[[]]的示例_shell脚本

  工作环境:Red Hat Enterprise Linux Server>  [[]]
  #!/bin/bash
  #
  until [[ $choice == "quit" ]]
  do
  read -p "input a character:" ch
  if [ ${#ch} -eq 1 ];then
  if [[ $ch == ]];then
  echo "num"
  elif [[ $ch == ]];then
  echo "character"
  else
  echo "special character"
  fi
  elif [ ${#ch} -eq 0 ];then
  echo "it's none"
  else
  echo "the system will select the first character"
  ch=`echo $ch | cut -c 1`
  if [[ $ch == ]];then
  echo "num"
  elif [[ $ch == ]];then
  echo "character"
  else
  echo "special character"
  fi
  fi
  read -p "if you want to out,just input quit:" choice
  done
  exit
  # bash 49.sh
  input a character:a
  character
  if you want to out,just input quit:n
  input a character:4
  num
  if you want to out,just input quit:quit
  []
  #!/bin/bash
  #
  until [[ $choice == "quit" ]]
  do
  read -p "input a character:" ch
  if [ ${#ch} -eq 1 ];then
  if [ $ch == ];then
  echo "num"
  elif [ $ch == ];then
  echo "character"
  else
  echo "special character"
  fi
  elif [ ${#ch} -eq 0 ];then
  echo "it's none"
  else
  echo "the system will select the first character"
  ch=`echo $ch | cut -c 1`
  if [ $ch == ];then
  echo "num"
  elif [ $ch == ];then
  echo "character"
  else
  echo "special character"
  fi
  fi
  read -p "if you want to out,just input quit:" choice
  done
  exit
  # bash 49.sh
  input a character:a
  special character
  if you want to out,just input quit:a
  input a character:4
  special character
  if you want to out,just input quit:quit
  # vim 49.sh
  优化:对于有多个字符的情况,可以使用如下判断
  [[ `echo "$ch"|cut -c1` == ]]

页: [1]
查看完整版本: []和[[]]的示例_shell脚本