Greetings all
I am trying to remember some goofy/clever way to do something. Is there
another way to do this, or any alternate methods which you prefer?
I'm working on test conditional expressions via []. I was writing a
script where I needed to evaluate and take action for certain multiple
exit codes of some program.
Let us say;
TEST1=7
The first way to do this uses single [] brackets and I'll break it up
for readability;
if [ \
"$TEST1" == 2 \
-o "$TEST1" == 3 \
-o "$TEST1" == 4 \
-o "$TEST1" == 5 \
-o "$TEST1" == 6 \
-o "$TEST1" == 7 \
] ; then
echo YES
else
echo NO
fi
But this is obviously long and annoying if I have many things to match
against. I really just want to match against a regex.
Then, I remembered that you could use =~ to do something like that, but
I had to google up to remember that you can only do it in double [[]]
brackets (new style);
if [[ "$TEST1" =~ 2|3|4|5|6|7 ]] ; then
echo YES
else
echo NO
fi
However, I seem to remember having done this previously with single
brackets, in a clever manner that was neat and didn't need as many lines
or repetitive goop as the first method mentioned above. Anyone have a
clue WTF I'm talking about, because I sure don't.
Any other clever ways to do complex conditional tests, or neat tricks on
the subject?