I am dealing with simple for loop, which loop the filles and execute on it some commands. I need to set up the filter with IF statement to consider only file started with the keyword "AllBoxes":

  for pdb in "${storage}"/"${experiment}"/*.pdb ; do
    pdb_name=$(basename "$pdb" .pdb)
    # if file starts with pharase AllBoxes do something on the file
    if [[ ${pdb_name} =='AllBoxes*' ]] ; then
    echo Vizualisation of ${pdb_name} is being processed!

This gives me an error in the IF statement.

Also would it be enought to skipp all of the rest filles (that does not start with "AllBoxes" keyworld? Need I introduce something like Else or Elif Break to exit the loop ?

  • 3
    So, what error is it that you get? – ilkkachu Nov 13 '20 at 15:23

The error is probably because you need a space after the == however you won't be able to use wildcards like that in an expanded test. You could use =~ to do regex matching (something like AllBoxes.*) however if you quote it, it will be treated literally. If you only care about AllBoxes files however you could just modify your loop to:

for pdb in "${storage}/${experiment}"/AllBoxes*.pdb ; do
  • thank you! you are right, but I am testing IF as the conditional filter :-) and how to match in the same manner the directory names containat "AllBoxes" in any part of the name ? I tried via if [[ ${dir_name} =~ AllBoxes ]] , but it did not work – user3470313 Nov 13 '20 at 15:47
  • Are you checking the dir name or the filename? – jesse_b Nov 13 '20 at 15:48
  • now I am checking DIR name but it was defined in the simmilar manner as filename: for d in "${output}"/*/; do; dir_name=${d%*/}; dir_name=$(basename "$dir_name") so I apply IF on $dir_name – user3470313 Nov 13 '20 at 15:50
  • 1
    If you have a different question please post it as a new question. – jesse_b Nov 13 '20 at 16:12
  • right thank you! I just wondering acrually how to use the same condition if [[ ${name} =~ 'AllBoxes*' ]] but when 'AllBoxes' is located in ANY part of the string defined as (basename) – user3470313 Nov 13 '20 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.