简体   繁体   中英

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

name abbreviation

price quantity 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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