Juanpe
Juanpe

Reputation: 446

Comparing a variable to a string using wildcards

i am creating a script that reads each file in a certain directory and uses some info from each file, but there is a file called "logdia + date" in each directory which i dont want to include into my loop, i have tried tu use an if-statement to compare the rute of the file to a string using wildcards but the condition never fails, and it always includes the "logdia" file into my loop, what am i missing? Thankyou

list_dir=$(ls -tr /opt/srv001/app/sam/trazas/*$v_fecha*)

for dir in $list_dir
do
if [ $dir != "*logdia*" ]
 then
 //Do stuff
fi
done

Upvotes: 1

Views: 4424

Answers (1)

anubhava
anubhava

Reputation: 785226

Right way to do that is as follows:

for dir in /opt/srv001/app/sam/trazas/*$v_fecha*; do
   if [[ "$dir" != *"logdia"* ]]; then
      echo "Do stuff Here"
   fi
done
  1. While comparing using glob patterns keep glob pattern outside quotes.
  2. Avoid parsing ls's output.

Upvotes: 1

Related Questions