如何在Shell脚本中逐行读取文件并将每一行存储在变量中 [英] how to read file line by line in shell script and store each line in a variable
问题描述
我是Unix的新手.我必须编写一个shell脚本,逐行读取文件并将其存储在单独的变量中.我的文本文件包含多个源路径和一个目标路径. 像这样:
i am new in unix. i have to write a shell script which read a file line by line and store it in a separate variable. my text file contents multiple source path and one destination path. something like this::
source_path=abc/xyz
source_path=pqr/abc
desination_path=abcd/mlk
源路径的数量可以变化. 我在Unix方面没有太多经验.谁能帮助我实现这一目标.这将非常有帮助.
number of source path can vary. I dont have much hands on experience in Unix. Can anyone help me to achieve this one. It will be very helpfull.
预先感谢
推荐答案
sourcePaths=( $(grep source_path myfile |cut -d= -f2) )
destPath=`grep destination_path myfile |cut -d= -f2`
$sourcePaths
是source_path
s的数组.您无需将每个source_path
存储在单独的变量中.
$sourcePaths
is an array of source_path
s. You don't need to store each source_path
in a separate variable.
您可以遍历数组,并对每个source_path
执行所需的操作.例如:
You can loop over the array and do what you want with each source_path
. For example:
for i in "${sourcePaths[@]}"
do
echo $i
done
这篇关于如何在Shell脚本中逐行读取文件并将每一行存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!