根据 Bash 中的输入生成逗号分隔的数字序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17056187/
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 05:40:08  来源:igfitidea点击:

Generating a comma-separated sequence of numbers based on input in Bash

bashshellscripting

提问by Looking2learned

I've found more than few things on here to help me as I'm learning to code in Bash and they all come close but not quite.

当我正在学习在 Bash 中编码时,我在这里发现了很多东西可以帮助我,它们都很接近但不完全。

I need to take an input of a positive integer and print out on a single line down to one, all separated by commas, without a comma at the end of the last variable.

我需要输入一个正整数并在一行上打印出来,所有行都用逗号分隔,最后一个变量的末尾没有逗号。

This is what I have so far:

这是我到目前为止:

#!/bin/bash
#countdown

read -p "Enter a Number great than 1: " counter

until ((counter < 1)); do
echo -n ",$counter"
((counter--))
done

It almost works out but I can't figure out how to prevent the comma in front and not have it behind the last variable.

它几乎可以解决,但我无法弄清楚如何防止前面的逗号而不是将它放在最后一个变量后面。

EDIT: You guys are AMAZING. Poured over this book and learned more in ten minutes here than I did with an hour there.

编辑:你们太棒了。翻阅这本书,在这里十分钟学到的东西比我在那里一个小时学到的还要多。

So is there some sort of command I could use to ensure it was only one number entered and ensure it had to be positive?

那么有没有我可以使用的某种命令来确保它只输入了一个数字并确保它必须是正数?

Some way to put an if statement on the read to ensure its <= 1 and only one character?

某种在读取上放置 if 语句以确保其 <= 1 且只有一个字符的方法?

I only have a background in some basic C coding, so I have the basics but translating them to BASH is harder than expected

我只有一些基本 C 编码的背景,所以我有基础知识,但将它们翻译成 BASH 比预期的要难

回答by choroba

Use seqwith the -soption:

seq-s选项一起使用:

seq -s, $counter -1 1 

回答by Yang

Probably simper way using brace expansion:

使用大括号扩展可能更简单:

#!/bin/bash
#countdown

read -p "Enter a Number great than 1: " counter

eval printf "%s" {${counter}..2}, 1

Test:

测试:

Enter a Number great than 1: 10
10,9,8,7,6,5,4,3,2,1

To validate the input, you can use regular expressions:

要验证输入,您可以使用正则表达式:

#!/bin/bash
#countdown

read -p "Enter a Number great than 1: " counter

if [[ ${counter} =~ ^[1-9][0-9]*$ ]]
then
  eval printf "%s" {${counter}..2}, 1
fi

回答by jim mcnamara

One way

单程

read -p "Enter a Number great than 1: " counter
echo -n "$counter"
((counter--))
until ((counter < 1)); do
echo -n ",$counter"
((counter--))
done

回答by chepner

A slightly awkward construction using an array, the seqcommand, and a subshell to localize a change to the IFSparameter will work.

使用数组、seq命令和子shell 来本地化IFS参数更改的稍微笨拙的构造将起作用。

read -p "Enter a Number great than 1: " counter
range=( $(seq $counter -1 1) )
( IFS=,; echo "${range[*]}" )

回答by jaypal singh

Here is another way … kinda influenced by chepner'ssolution but not using seq:

这是另一种方式......有点受chepner的解决方案影响但不使用seq

Content of script.sh:

script.sh 的内容:

#!/bin/bash

read -p "Enter a Number great than 1: " counter
range=( $(eval echo {$counter..1}) )
( IFS=,; echo "${range[*]}" )

Test:

测试:

$ bash script.sh
Enter a Number great than 1: 5
5,4,3,2,1
$ bash script.sh
Enter a Number great than 1: 30
30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1