Logical OR in my shell script
I think you can avoid a loop entirely here:
cp *TH[xy]*.ats /home/milenko/procmt
There's no need to loop through the results and then do a separate comparison; a single glob will expand to the list of files that you want.
There were a couple of problems with your original approach:
- Firstly, you were trying to test for exact matches, so the condition would never be true.
- Also, take care with spaces:
]]
is a keyword in the compound command[[
, so it needs to be a separate word (i.e. surrounded by spaces).
What about using extglob for extended globbing? This way you can use the for
itself to get the required extensions:
shopt -s extglobfor file in *TH?(x|y)*.ats; do # do things with "$file" ...done
*TH?(x|y)*.ats
expands to those files containing <something> + TH + either x or y + <something> + .ats
Your script fails because you have a typo in it:
if [[ ("${file}" = THx) || ("${file}" = THy)]]# ^# missing space
This is fine:
$ d="hi"$ [[ ($d == hi) || ($d == ha) ]] && echo "yes"yes
Although the parentheses are superfluous:
$ [[ $d == hi || $d == ha ]] && echo "yes"yes