美文网首页
shell入门9-字符串,整数,文件属性的判断与比较

shell入门9-字符串,整数,文件属性的判断与比较

作者: 万州客 | 来源:发表于2022-12-29 08:02 被阅读0次

    shell在这方面设计,感觉复杂了点,可能有一定历史渊源吧。

    一,字符串的判断与比较

    在 Shell 中进行条件测试时一定要注意空格问题。使用[]测试时,左方括号右边和右方
    括号左边都必须有空格。而且测试的比较符号两边也必须都有空格。

      867  echo $((2-4))
      868  echo $((2*4))
      869  echo $((2**4))
      870  echo $((10%3))
      871  x=2
      872  echo $((x+=2)
      873  )
      874  echo $((x+=2))
      875  echo $((x*=3))
      876  echo $((x%=2))
      877  x=2 ; y=3
      878  echo $((x*y))
      879  echo $x $y
      880  echo $((3>2&&5>3))
      881  echo $((3>2&&5>9))
      882  echo $((1>2||5>8))
      883  echo $((3>2||5>8))
      884  echo $[2+8]
      885  echo $[2**8]
      886  x=3; y=5
      887  echo $[x+y]
      888  echo $[x*y]
      889  echo $[1+2*3]
      890  echo $[(1+2)*3]
      891  echo $[x>y?2:3]
      892  echo $[y>x?2:3]
      893  echo $[y>x?2+2:3*5]
      894  echo $[x>y?2+2:3*5]
      895  let 1+2
      896  x=5
      897  let x++
      898  echo $x
      899  let x*=2; echo $x
      900  let i=1+2*3; echo $i
      901  let i=(1+2)*3; echo $i
      902  let 2.2 + 5.5
      903  x=1
      904  echo $[x+1]
      905  echo $[x++]
      906  echo $x
      907  x=6
      908  echo $[x--]
      909  echo $x
      910  bc
      911  echo "2+3; scale=2; 8/19" |bc
      912  echo {obase=2; 10"|bc
      913  "
      914  echo "obase=2; 10"|bc
      915  test a == a
      916  echo $?
      917  test a == b; echo $?
      918  test a == b ; echo $?
      919  test a != b ; echo $?
      920  test a != b; echo $?
      921  [ $USER == root ]; echo $?
      922  [ $USER == root ] && echo Y || echo N
      923  [ -z $TEST ] && echo Y || echo N
      924  TEST=123456
      925  [ -z $TEST ] && echo Y || echo N
      926  [test == beijing]
      927  [ test==root ]; echo $?
      928  [ 1==2 ]; echo $?
      929  [ -n $TEST ] && echo Y || echo N
      930  [ -n $Jacob ] && echo Y || echo N
      931  [ -n ] && echo Y || echo N
      932  [ -n "$Jacob" ] && echo Y || echo N
    

    二,整数的判断与比较

    image.png
    933  test 3 -eq 3 && echo Y || echo N
      934  test 3 -ne 3 && echo Y || echo N
      935  [ 6 -gt 4 ] && echo Y || echo N
      936  [ 6 -gt 9 ] && echo Y || echo N
      937  [ 6 -ge 4 ] && echo Y || echo N
      938  [ 6 -ge 6 ] && echo Y || echo N
      939  [ 6 -lt 9 ] && echo Y || echo N
      940  [ 6 -lt 3 ] && echo Y || echo N
      941  [ 6 -lt 6 ] && echo Y || echo N
      942  [ 6 -le 3 ] && echo Y || echo N
      943  [ 6 -le 6 ] && echo Y || echo N
      944  [ $UID -eq 0 ] && echo Y || echo N
      945  cat /proc/meminfo 
      946  grep Available /proc/meminfo |egrep -o "[0-9]"
      947  grep Available /proc/meminfo |egrep -o "[0-9]+"
      948  mem_free = $(grep Available /proc/meminfo |egrep -o "[0-9]+")
      949  mem_free=$(grep Available /proc/meminfo |egrep -o "[0-9]+")
      950  [ $mem_free -le 512000] && echo Y|| echo N
      951  [ $mem_free -le 512000 ] && echo Y|| echo N
      952  ps aux|wc -l
      953  procs=$(ps aux|wc -l)
      954  [ $procs -gt 100 ] &&echo Y ||echo N
      955  cat /etc/passwd|wc -l
      956  num=$(cat /etc/passwd|wc -l)
      957  [ $num -ge 30 ] &&echo Y ||echo N
    

    三,文件属性的判断与比较

    image.png
     958  touch ver1.txt
      959  touch ver2.txt
      960  mkdir test
      961  [ -e ver1.txt ] && echo Y || echo N
      962  [ -e test ] && echo Y || echo N
      963  [ ! -e ver1.txt ] && echo Y || echo N
      964  [ -f ver1.txt ] && echo Y || echo N
      965  [ ! -f ver1.txt ] && echo Y || echo N
      966  [ -f test/ ] && echo Y || echo N
      967  [ -d test/ ] && echo Y || echo N
      968  [ -d ver1.txt ] && echo Y || echo N
      969  [ -b /dev/sda ] && echo Y || echo N
      970  [ -b /etc/passwd ] && echo Y || echo N
      971  ls -s /etc/hosts /root/soft
      972  ln -s /etc/hosts /root/soft
      973  ln /etc/hosts /root/hard
      974  ls -l /root/soft 
      975  ls -l /root/hard 
      976  [ -L /root/soft ] && echo Y || echo N
      977  [ -L /root/hard ] && echo Y || echo N
      978  ls -l ver1.txt 
      979  [ -r ver1.txt] && echo Y || echo N
      980  [ -r ver1.txt ] && echo Y || echo N
      981  chmod -r ver1.txt 
      982  [ -r ver1.txt ] && echo Y || echo N
      983  chmod -w ver1.txt 
      984  [ -w ver1.txt ] && echo Y || echo N
      985  ls -l ver1.txt 
      986  [ -x ver1.txt ] && echo Y || echo N
      987  chmod +x ver1.txt 
      988  [ -x ver1.txt ] && echo Y || echo N
      989  [ -s ver1.txt ] && echo Y || echo N
      990  echo "hello" > ver1.txt 
      991  [ -s ver1.txt ] && echo Y || echo N
      992  ls -l ver*
      993  [ ver1.txt -nt ver2.txt ] && echo Y || echo N
      994  [ ver1.txt -ot ver2.txt ] && echo Y || echo N
    

    相关文章

      网友评论

          本文标题:shell入门9-字符串,整数,文件属性的判断与比较

          本文链接:https://www.haomeiwen.com/subject/urlaqdtx.html