简体   繁体   English

BASH:试图读入文件但它只得到第一行

[英]BASH: trying to read in file but it only gets the first line

Im trying to read in a file in bash and store the variables to be used at some later point, the format for the files is as follows我正在尝试读取 bash 中的文件并存储稍后要使用的变量,文件格式如下

name abbreviation姓名缩写

price quantity maxQuantitiy价格 数量 maxQuantitiy

itemDescription商品描述

but when i try to actually read in the file it seems ot only store the first line in every variable and was wondering where it is that its storing the variables wrong但是当我尝试实际读取文件时,它似乎只在每个变量中存储了第一行,并且想知道它存储变量的位置是错误的

if [ -r data/$fileName.file ]; then
    read name abbreviation < data/$fileName.file
    read price quantity maxQuantity < data/$fileName.file
    read itemDescription < data/$fileName.file
fi

and when i try to echo the price or quantity it echos the name and the abbreviation.当我尝试回应价格或数量时,它回应了名称和缩写。

read reads the first line. read读取第一行。 When you do another redirection, it's not tied to the previous one, so it again reads the first line.当您进行另一个重定向时,它不会与前一个重定向相关联,因此它会再次读取第一行。 You need to use one redirection for all the reads:您需要对所有读取使用一个重定向:

if [ -r data/$fileName.file ]; then
    {
        read name abbreviation
        read price quantity maxQuantity
        read itemDescription
    } < data/$fileName.file
fi

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

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