将逗号分隔的数字字符串解析为变量(脚本)bash [英] Parse comma separated string of numbers into variables (scripting) bash
问题描述
上面标记为重复的帖子是相似的,但是不足以用于用例.以下答案显示了对read命令的最低限度使用,它将已知长度的定界符分隔值的解析后的输入放入有用命名的变量中.例如,如果我将所有四个变量读入$ STATEMENTS,$ BRANCHES,$ FUNCTIONS,$ LINES-循环是不理想的,因为它增加了最小的循环索引意识或增加了4行以将每个数组变量放入一个有用的变量中.
The post marked as duplicate above is similar, however is not sufficient for the use case. The below answers show a minimalist use of the read command to put parsed input for a known length of delimiter separated values into helpfully-named variables. For instance, if I read all four vars into $STATEMENTS,$BRANCHES,$FUNCTIONS,$LINES - a loop is not ideal as it adds a minimal of loop index awareness or 4 more lines to put each array var into a helpfully named var.
我在文件中有一个用逗号分隔的数字列表:
I have a list of comma separated numbers in a file:
26.16,6.89,23.82,26.17
26.16,6.89,23.82,26.17
我想将这4个数字读作有用命名的单独变量名-永远不会少于或少于4个数字.
I'd like to read these 4 numbers into helpfully named separate variable names - there will never fewer or more than 4 numbers.
感谢您的帮助!
推荐答案
您将需要内置 read
.输入流和要读取的变量可能会根据您的个人喜好而有所不同.例如,
You'll need read
builtin. The input stream, and variables to read can vary, based on your personal preferences. For instance,
IFS=,
LIST=1,2,3,4
read a b c d <<<$LIST
echo $a ; echo $b ; echo $c ; echo $d
这篇关于将逗号分隔的数字字符串解析为变量(脚本)bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!