Parse comma separated string of numbers into variables (scripting) bash [duplicate]

99封情书 提交于 2020-01-03 03:00:52

问题


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

I'd like to read these 4 numbers into helpfully named separate variable names - there will never fewer or more than 4 numbers.

Thanks for any help!


回答1:


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


来源:https://stackoverflow.com/questions/51807953/parse-comma-separated-string-of-numbers-into-variables-scripting-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!