Shell Bash 脚本以升序打印数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27112190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:52:24  来源:igfitidea点击:

Shell Bash script to print numbers in ascending order

linuxbashshell

提问by CLearner

I am really new to shell Bash scripting. I need to print numbers in ascending order on a line for a given arbitrary number that is input by the user.

我对 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

#
# Print the sorted number
# 
echo -e "\nSorted Numbers in Ascending Order:"
for (( i=0; i <= 4; i++ )) 
do
  echo ${nos[$i]}
done

回答by anubhava

You can use this script:

你可以使用这个脚本:

#!/bin/bash
IFS=' ' read -ra arr -p "Enter numbers: "
Enter numbers: 4 -1 2 66 10

sort -n <(printf "%s\n" "${arr[@]}")
-1
2
4
10
66
  • IFS=' 'to make readall number delimited by space
  • 'read -ra` to read all numbers in an array
  • sort -nto sort numbers numerically
  • printf "%s\n" "${arr[@]}"to print each element of array in separate line
  • <(printf "%s\n" "${arr[@]}")is process substitution that make it printfcommand behave like a file for sort -ncommand.
  • IFS=' '使read所有数字以空格分隔
  • 'read -ra` 读取数组中的所有数字
  • sort -n按数字对数字进行排序
  • printf "%s\n" "${arr[@]}"在单独的行中打印数组的每个元素
  • <(printf "%s\n" "${arr[@]}")是进程替换,使其printf命令的行为类似于命令的文件sort -n

回答by Riad

Ask user to give input by comma and parse it and then populate the nosarray..

要求用户通过逗号输入并解析它,然后填充nos数组..

echo "Please enter numbers separated by comma: "
read string_var
IFS=',' read -a nos <<< "$string_var" 

Or by space it is more easy:

或者按空间更容易:

echo "Please enter numbers separated by space: "
read string_var
nos=($string_var)   //now you can access it like array...

// now rest of the code ....

回答by F. Hauri

Bashisms, using bash

bashisms,使用 bash

If you

如果你

  • want to use bashonly (no fork and no external binaries)
  • use small interger numbers (smaller then 2^60 aka: < 2305843009213693952)
  • have no duplicated numbers
  • 只想使用bash(没有 fork 和外部二进制文件)
  • 使用小的整数(小于 2^60 又名:< 2305843009213693952)
  • 没有重复的数字

Under 64 bit bash, you could use array index to store and sort your integer:

在 64 位 bash 下,您可以使用数组索引来存储和排序您的整数:

read -a array <<<'4 -1 2 66 10'
for i in ${array[@]};do
    sorted[i+(2<<60)]=''
  done
for i in ${!sorted[@]};do
    echo $[i-(2<<60)]
  done
-1
2
4
10
66

For dealing with duplicate:

处理重复:

read -a arr <<<'4 -1 2 66 -12 -10 10 2 24 -10'

for i in ${arr[@]};do
    ((srtd[i+(2<<60)]++))
  done

for i in ${!srtd[@]};do
    for ((l=0;l<${srtd[i]};l++));do
        echo $[i-(2<<60)]
      done
  done

-12
-10
-10
-1
2
2
4
10
24
66