[英]BASH: trying to read in file but it only gets the first line
我正在尝试读取 bash 中的文件并存储稍后要使用的变量,文件格式如下
姓名缩写
价格 数量 maxQuantitiy
商品描述
但是当我尝试实际读取文件时,它似乎只在每个变量中存储了第一行,并且想知道它存储变量的位置是错误的
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
当我尝试回应价格或数量时,它回应了名称和缩写。
read
读取第一行。 当您进行另一个重定向时,它不会与前一个重定向相关联,因此它会再次读取第一行。 您需要对所有读取使用一个重定向:
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.