Read in a list of files and loop over them in bash

Needed to do this and after a bit of looking around (Bash scripting) came up with this:


#!/bin/bash

FILE_LIST_SOURCE=listoffiles.txt
DEFAULT_FILE="some.default.file"

[ -f $FILE_LIST_SOURCE ] && LIST_OF_FILES=$(cat $FILE_LIST_SOURCE)

LIST_OF_FILES=${LIST_OF_FILES:-$DEFAULT_FILE}

#echo -e "\\nLIST_OF_FILES:\\n$LIST_OF_FILES"

echo "List of files:"
for i in $LIST_OF_FILES; do
    echo $i
done


Share