linux 按数字大小排序,shell 实现三个数大小排序

阅读: 评论:0

linux 按数字大小排序,shell 实现三个数大小排序

linux 按数字大小排序,shell 实现三个数大小排序

想到shell跟C 有那么多不同,之前想到用shell 怎么实现C里面三个数的大小排序呢?

今天想了下,实练了下,还是不困难,虽然算法那么普通;不过有想法然后能根据自己学的东西把想法实现,还是蛮有意思的。

1 #!/bin/bash

2

3 tmp=0

4 echo "input the three number"

5 read -p "1:" a

6 read -p "2:" b

7 read -p "3:" c

8 if [ $a -gt $b ]

9 then

10 tmp=$a

11 a=$b

12 b=$tmp

13 fi

14 if [ $a -gt $c ]

15 then

16 tmp=$a

17 a=$c

18 c=$tmp

19 fi

20 if [ $b -gt $c ]

21 then

22 tmp=$b

23 b=$c

24 c=$tmp

25 fi

26 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

本文发布于:2024-01-31 09:03:40,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170666302527405.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:大小   个数   数字   linux   shell
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23