0

I have one txt file that has bunch of class names.

I would like to read those class names line by line in bash script and assign that value to a variable to use it globally in another command in the script.

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
Poshi
  • 5,332
  • 3
  • 15
  • 32
dicle
  • 1,122
  • 1
  • 12
  • 40

3 Answers3

1

I'm assuming files.txt is a list of the class files, one class per line?

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

If you have more than one file of classes, use an array.
Don't make it all caps.

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
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • Hey thank you for the answer so when i add additional command with the need of $class attribute variable i should just use it in de "do" part right? I cant use after done? – dicle Apr 04 '19 at 07:57
  • The `$class` variable's value will not persist past the loop, but you could set `foo=$class` inside the loop and `$foo` would still remain afterwards. – Paul Hodges Apr 04 '19 at 13:59
0

Assuming each line in your files contain only a class name, this should do it :

for f in $FILES
do
  echo "Processing $f file..."
  while read class
  do 
    <something with "$class" >
  done < $f
done
nullPointer
  • 4,419
  • 1
  • 15
  • 27
0

Example 1

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

Example 2, as commented by Paul Hodges

FILES="files.txt"
while read class
do
   echo $class  # Do something with $class
done < $FILES
Adam vonNieda
  • 1,635
  • 2
  • 14
  • 22
  • I have one file that contains several File Paths.. I think this does not work for me @PaulHodges – dicle Apr 04 '19 at 12:30