1

Directory structure like below :

├─thrift_master
   ├─Common
     └─common.thrift
   ├─folder2
     └─f1.thrift
   └─types.thrift
└─update.sh

I want to generate python-thrift package by using thrift -nowarn -gen py

Here's my attemp using shell, it works for I'm using absolute directory like Common/*.thrift, how can I make it work recursively?


cd `dirname $0`

TMP=thrift_master

#...

cd $TMP

for i in Common/*.thrift *.thrift folder2/*.thrift
do
        thrift  -nowarn -gen py $i
done

echo "update thrift_interface gen files..."


jia Jimmy
  • 1,693
  • 2
  • 18
  • 38
  • Possible duplicate of [Recursively look for files with a specific extension](https://stackoverflow.com/questions/5927369/recursively-look-for-files-with-a-specific-extension) – Inian Mar 07 '19 at 07:02

1 Answers1

1

You can use find utility on Unix/Linux systems:

cd thrift_master

find . -iname '*.thrift' -print0 |
while IFS= read -rd '' file; do
    thrift -nowarn -gen py "$file"
done
  • Using -print0 option of find to get output delimited by NUL character to address filenames with whitespaces / glob characters.
  • Correspondingly we need to use -IFS= and -d '' in read to handle filenames separated by NUL character.

PS: If you're bash then you can avoid pipeline by using process substitution:

while IFS= read -rd '' file; do
    thrift -nowarn -gen py "$file"
done < <(find . -iname '*.thrift' -print0)
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • thanks for your replying, it works, and could you pls tell the difference between command `find . -type f -name "*.thrift" | xargs -n 1 thrift -nowarn -gen py $file ` and yours? – jia Jimmy Mar 07 '19 at 07:40
  • Actually `xargs` command won't know anything about `$file` variable. Correct command would probably be: `find . -type f -name "*.thrift" | xargs -n 1 thrift -nowarn -gen py` – anubhava Mar 07 '19 at 07:44