简体   繁体   English

逐行读取文件并将值分配给以逗号分隔的变量

[英]read a file line by line and assign the values to variable as comma separated

I have the following a.txt file:我有以下 a.txt 文件:

abc,
def,
ghi

I want to read it line-by-line, and store in a varibale as comma seperated values我想逐行阅读它,并将其作为逗号分隔值存储在变量中

var1=abc,def,ghi

i am new to shell script pls help我是 shell 脚本的新手请帮忙

My try:我的尝试:

name="file.txt"
while IFS=read -r line
do
    names=`echo $line`
done < "name"

it is displaying only value ghi to varibale它只显示 varibale 的值 ghi

You're not concatenating, you're replacing the names variable each time through the loop.您不是在串联,而是在每次循环中都替换names变量。

There's no need to use echo when assigning the variable.分配变量时无需使用echo

name="file.txt"
names=
while IFS=read -r line
do
    names="$names$line"
done < "name"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM