0

I want to iterate over all json files in a specific subdirectory.

#!/bin/sh

source_dir="nodes"

for file_path in $source_dir/*.json;
do
   file_name=$(basename $file_path .${file_path##*.})
   echo $file_name
done

My code is working as expected if there is at least one json file in the directory. If there is no json file in the directory, the loop will still be executed. The file_name is then "*".

How do I have to change the for loop so that it is only executed if there is at least one json file in the directory?

user5580578
  • 1,134
  • 1
  • 12
  • 28

1 Answers1

0

you can wrap you loop in an if clause to check if the pattern matches anything.

Check this SO question on how to do this: Check if a file exists with wildcard in shell script

Blindleistung
  • 672
  • 8
  • 21