I have a bunch of output files in a directory, such as: a.out, b.out c.out, etc.
I want to search through the output files and if the output file name contains a certain string (such as "a"), then it will print the corresponding output file "a.out" to screen.
After I cd-ed into the output files directory, here's my code:
OUT_FILE="*.out"
OT=$OUT_FILE
STRING="a"
for file in "$OT";do
if [[$file == *"$STRING"*]];then
echo $file
fi
done
The error I received is [[*.out: command not found. It looks like $file is interpreted as $OT, not as individual files that matches $OT.
But when I removed the if statement and just did a for-loop to echo each $file, the output gave me all the files that ended with .out.
Would love some help to understand what I did wrong. Thanks in advance.
*.out) aren't expanded inside double-quotes or on the right side of an assignment, so yours never gets converted to a list of files (until youechoit without double-quotes). Also, you need spaces between[[, the elements of the comparison, and]]. Spaces are critical delimiters in the shell; do not leave them out.