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

原文地址:https://www.cnblogs.com/2567xl/p/11447427.html

时间: 2024-08-26 10:04:22

shell 脚本 数字大小排序的相关文章

将一个整数数组先按照因子数量排序,再按照数字大小排序,输出第k个数

同小米OJ比赛题:现在有 n 个数,需要用因子个数的多少进行排序,因子个数多的排在后面,因子个数少的排在前面,如果因子个数相同那么就比较这个数的大小,数大的放在后面,数小的放在前面.现在让你说出排序之后第 KK 个位置的数字是多少. 原文地址:https://www.cnblogs.com/graybird/p/10797991.html

shell脚本学习(2)比较两个数字大小

注意:shell中对比字符串只能使用==.<.>.!=.-z.-n.对比字符串时,末尾一定要加上x(或者a.b等)一个字符,因为if [ $1x == "ab"x ]时如果没有了x ,并且$1是"",这个语句会翻译成if [  == "ab" ],左边相当于没有东西了,会报语法错误.或者使用[[  ]],就不需要x了.使用<或者>时,如果是用[  ],需要用转义符"\",如\>. 对比数字使用既能

shell脚本-比较两个整数大小

开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小.用条件表达式(禁止if)进行判断并以屏幕输出的方式提醒用户比较结果.注意:一共是开发2个脚本.当用脚本传参以及read读入的方式需要对变量是否为数字.并且传参个数不对给予提示. read读入方式 #!/bin/bash read -p "Pls input two num:" a b  #no1 [ -z "$a" ] || [ -z "$b" ] && 

shell脚本编程(合并排序)

#!/bin/bash #shell脚本排序之合并排序 a=(1 3 5 7 9) b=(2 4 6 8 10 12 14) c= n=5 m=7 i=0 j=0 k=0 while [ $i -lt $n -a $j -lt $m ];do if test ${a[$i]} -gt ${b[$j]};then c[$k]=${b[$j]} j=$(($j+1)) k=$(($k+1)) else c[$k]=${a[$i]} i=$(($i+1)) k=$(($k+1)) fi done #e

学以致用十七----shell脚本之比较数字和字符串及if else

非常需要注意的是shell脚本对空格要求非常严格, 如: 比较字符串   (不能用于比较字符串) 以上这种写法会报错 因此比较字符串不用 单中括号 [ ] 而是用双中括号[ [ ] ] ,比较两字符串中间用等号,用eq,lt ,gt 不起作用 ,        (正确格式) 且注意中括号和字母之间要有空格,否则会报错    (错误格式) 比较数字则单双中括号都可以用,-eq -lt  -gt  > <  =没有限制,但中括号和字母之间必须要有空格 if else 也有严格的要求 有if 后者是

16.数字大小升降排序编写

数字大小升降排序编写输入描述: 输入为: 1. 排序方式:DESC(降序) ASCE(升序),分别表示按降序和升序排列,大小写不敏感 2. 一组十进制整数字符串,以","符号作为分隔符 例子: DESC 789,123,456 ASCE 234,567,89 排序方式DESC ASCE请忽略大小写 输出描述: 按指定排序方式排序后得到的十进制整数字符串: 输出的数字字符串数组,以","字符作为分隔符. 示例1 输入:ASCE 789,123,456 输出:123,4

shell脚本编程设计——根据输入的数输出菱形、三角形或者数字金字塔(带闪烁颜色)

shell脚本编程设计--根据输入的数输出菱形.三角形或者数字金字塔(带闪烁颜色) shell脚本程序和解释如下 #!/bin/bash #创建死循环,当输入出错或者输入"quit"字符串退出脚本程序 while true do #等待用户输入 read -p "请输入一个不大于20的正整数(输入"quit"则退出):" n #先判断字符串是否是quit if [ "$n" == "quit" ] then

shell脚本编程排序算法之快速排序

#!/bin/bash #shell脚本编程之快速排序的实现(以最右边为元点的思想) a=(8 5 10 3 2 93 4 1 2 3 40 9 61 8 6 29) temp= buff= #交换函数 swap() { buff=${a[$1]} a[$1]=${a[$2]} a[$2]=$buff } fun() { i=$(($1-1)) j=$1 temp=${a[$2]} if test $1 -ge $2 ;then return 2 fi while [ $j -le $2 ];d

key值为数字字符的对象,按数字大小顺序排序|自定义obj.sort函数|为对象排序

var helper = { sort:function(data){ //{“20141216”:{},“20141217”:{}}按大小排序, var arr1 = [],arr2=[],self = this; for(var key in data){ arr1.push(key); } arr1.sort(); for(var i=0;i<arr1.length;i++){ var date = arr1[i]; var o = self._clone(data[date]); o.d