How do I make an array of files in shell. Is it just
files = (file1.txt file2.txt file3.txt)
or is it
files = ("file1.txt" "file2.txt" "file3.txt)
If later on in the program I want to refer to this array and pick a specific file to read, how would I do that. I am asking because I would like to have a conditional that would read that file and compare it to an output file.
Would I just do: ( I know I can't do this)
if grep -q ${outputs[i]} output; then
#etc.
But what should I do instead then?
No spaces around =
in assignments, arrays are no excepetion.
files=( file1.txt "file2.txt" )
Quotes around file names are needed if file names contain special characters, e.g. whitespace, parentheses, etc.
You can use a for
loop to walk the array:
for file in "${array[@]}" ; do
if grep -q "$file" output ; then
Are you sure you want to use $file as the regular expression? Then you need to handle regex characters specially, i.e. dot, caret, dollar sign, etc.
If you want to compare contents of the two files, use diff
, not grep
.
if diff -q "$file" output > /dev/null ; then
echo Same
else
echo Different
fi