Home > Mobile >  Two conditions with negation bash for filename
Two conditions with negation bash for filename

Time:12-01

I want to echo files that does not contain a substring "cds" or "rna" in their filenames.

I use the following code:

for genome in *
do
if [ ! "$genome" == *"cds"* ] || [ ! "$genome" == *"rna"* ]
then
    echo $genome
fi
done

The code does not return any error but it keeps printing files that have the substrings indicated in the file name. How can I correct this? Thank you!

CodePudding user response:

There's two separate mistakes:

  • When using * in Bash comparisons, you need to use two sets of brackets, so [ ... ] should be [[ ... ]].
  • I think you really mean "files that does not contain a substring "cds" and also not "rna" in their filenames. That is, the 'or' (||) should be an 'and' (&&).
for genome in *
do
if [[ ! "$genome" == *"cds"* ]] && [[ ! "$genome" == *"rna"* ]]
then
    echo $genome
fi
done

CodePudding user response:

The condition should be written as

[[ ! $genome =~ cds|rna ]] && echo $genome
  • Related