find specific file names and execute a command depending on file's name

Hi,

As a newbie, I'm desperate ro make my shell script work. I'd like a script which checks all the files in a directory, check the file name, if the file name ends with "extracted", store it in a variable, if it has a suffix of ".roi" stores in another variable. I'm going to use these two variables in a new command that needs them. So I came up with:

e#!/bin/bash

for file in path/*
if [$file == *_extracted]
then
extracted_file =$file
else
if [$file== *.roi]
then 
roi_file=$file
fi
fi

done;

what am doing wrong? Any input is gratefully appreciated

Hello, welcome to the forum.

How about:

#!/bin/bash

for extracted_file in /path-to-dir/*extracted
do
  echo "do stuff with $extracted_file"
done

for roi_file in /path-to-dir/*.roi
do
  echo "do stuff with $roi_file"
done

*Edit*
Your version needs ksh93/bash style double straight brackets and proper spacing inside for pattern matching to work:

if [[ $file == *_extracted ]]

The same can be done with a more universal case statement inside the loop:

case $file in
  *_extracted) 
      extracted_file=$file ;;
  *.roi)
      roi_file=$file;;
esac

plus you need do..done for the for loop...
There can be no space before or after an assignment operator (=)

If you post code on this forum it would be good if you could use code tags (

```text
...
```

) and properly indent your code..

Hi,

Thanks for the suggestion. but the problem is that I woud need both of these two files, at the same time in an in-house command that needs two inputs like:

command file1 file2

that's why I have gone for if. The case in works fine. Thank you very much

#!/bin/bash

for file in path/*_extracted
do
  extracted_file=$file
done

for file in path/*_roi
do
  roi_file=$file
done

if [ -n $extracted_file -a -n $roi_file ]
then
  yourcommand $extracted_file $roi_file
fi

In fact you do not need for loop if there is only 1 extracted file and 1 roi file

yourcommand path/*_extracted path/*_roi