小编CLe*_*ner的帖子

Shell Bash脚本以升序打印数字

我真的是Shell Bash脚本的新手。对于用户输入的给定任意数字,我需要在一行上按升序打印数字。

#!/bin/bash

declare nos[5]=(4 -1 2 66 10)
# Prints the number befor sorting

echo "Original Numbers in array:"
for (( i = 0; i <= 4; i++ ))
    do
      echo ${nos[$i]}
    done

 #
 # Now do the Sorting of numbers  
 #

for (( i = 0; i <= 4 ; i++ ))
do
   for (( j = $i; j <= 4; j++ ))
   do
      if [ ${nos[$i]} -gt ${nos[$j]}  ]; then
       t=${nos[$i]}
       nos[$i]=${nos[$j]}
       nos[$j]=$t
      fi
   done
done

# …
Run Code Online (Sandbox Code Playgroud)

linux bash shell

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

bash ×1

linux ×1

shell ×1