如何从文件名在 bash 中的列表中读取全局变量?

How to have a global variable that has been read from a list that has filenames in bash?

我有一个包含一堆 class 个名字的 txt 文件。

我想在 bash 脚本中逐行读取那些 class 名称,并将该值赋给一个变量,以便在脚本中的另一个命令中全局使用它。

FILES="files.txt"

for f in $FILES
do
  echo "Processing $f file..."
  # take action on each file. $f store current file name
  cat $f
done

假设您文件中的每一行仅包含一个 class 名称,应该这样做:

for f in $FILES
do
  echo "Processing $f file..."
  while read class
  do 
    <something with "$class" >
  done < $f
done

示例 1

FILES="files.txt" 
cat $FILES | while read class
do
   echo $class  # Do something with $class
done

示例 2,如 Paul Hodges 所评论

FILES="files.txt"
while read class
do
   echo $class  # Do something with $class
done < $FILES

我假设 files.txt 是 class 文件的列表,每行一个 class?

while read class
do : whatver you need with $class
done < files.txt

如果您有多个 classes 文件,请使用数组。
不要全部大写。

file_list=( files.txt other.txt ) # put several as needed
for f in "${file_list[@]}" # use proper quoting
do : processing $f # set -x for these to log to stderr
   while read class
   do : whatver you need with $class
   done < "$f"
done