zoukankan      html  css  js  c++  java
  • shell 脚本 数字大小排序

    shell 实现三个数大小排序

     #!/bin/bash
      
      tmp=0
      echo "input the three number"
      read -p "1:" a
      read -p "2:" b
      read -p "3:" c
      if [ $a -gt $b ]
      then
              tmp=$a
              a=$b
              b=$tmp
      fi
      if [ $a -gt $c ]
      then
              tmp=$a
              a=$c
              c=$tmp
      fi
      if [ $b -gt $c ]
      then
              tmp=$b
              b=$c
              c=$tmp
      fi
      echo "the sorted number is : $a $b $c"
    
    自测:
    Loong:/home/yee# sh -x compare.sh 
    + tmp=0
    + echo 'input the three number'
    input the three number
    + read -p 1: a
    1:23
    + read -p 2: b
    2:43
    + read -p 3: c
    3:35
    + '[' 23 -gt 43 ']'
    + '[' 23 -gt 35 ']'
    + '[' 43 -gt 35 ']'
    + tmp=43
    + b=35
    + c=43
    + echo 'the sorted number is : 23 35 43'
    the sorted number is : 23 35 43
    Loong:/home/yee# sh -x compare.sh 
    + tmp=0
    + echo 'input the three number'
    input the three number
    + read -p 1: a
    1:12
    + read -p 2: b
    2:13
    + read -p 3: c
    3:16
    + '[' 12 -gt 13 ']'
    + '[' 12 -gt 16 ']'
    + '[' 13 -gt 16 ']'
    + echo 'the sorted number is : 12 13 16'
    the sorted number is : 12 13 16
    Loong:/home/yee# sh -x compare.sh 
    + tmp=0
    + echo 'input the three number'
    input the three number
    + read -p 1: a
    1:54
    + read -p 2: b
    2:43
    + read -p 3: c
    3:32
    + '[' 54 -gt 43 ']'
    + tmp=54
    + a=43
    + b=54
    + '[' 43 -gt 32 ']'
    + tmp=43
    + a=32
    + c=43
    + '[' 54 -gt 43 ']'
    + tmp=54
    + b=43
    + c=54
    + echo 'the sorted number is : 32 43 54'
    the sorted number is : 32 43 54
  • 相关阅读:
    第11组 Alpha冲刺(4/6)
    第11组 Alpha冲刺(3/6)
    第11组 Alpha冲刺(2/6)
    第11组 Alpha冲刺(1/6)
    团队Git现场编程实战
    第11组 团队项目-需求分析报告
    团队项目-选题报告
    第10组 Alpha冲刺(2/6)
    第10组 Alpha冲刺(1/6)
    2019 SDN上机第2次作业
  • 原文地址:https://www.cnblogs.com/2567xl/p/11447427.html
Copyright © 2011-2022 走看看