簡體   English   中英

如何在子目錄中搜索.c文件並進行編譯(shell腳本)

[英]How to search subdirectories for .c files and compile them (shell scripting)

我需要接受一個參數,該參數是當前目錄的目錄,並搜索其文件夾並編譯這些文件夾中的所有C文件。 我剛剛開始在Bash中進行shell腳本編寫,有點不知所措。

到目前為止,我嘗試過的事情包括使用find搜索文件,然后將其通過管道傳送到xargs進行編譯,但始終收到錯誤消息,指出testing.c不是目錄。

find ~/directory -name *.c | xargs gcc -o testing testing.c

我也嘗試過ls -R搜索.c文件的文件夾,但是不知道如何使用路徑作為參數,然后移至並編譯?

find ~/directory -type f -name "*.c" -print0 |
while IFS= read -r -d '' pathname; do
    gcc -o "${pathname%.c}" "$pathname"
done
find directory -type f -name "*.c" -exec sh -c \
  'cd $(dirname $1);make $(basename $1 .c)' sh {} \;

正如@ shx2所建議的那樣,使用make (或其他構建系統)可能是最好的方法。 如果沒有適當的構建系統,您不想在某個源代碼樹中編譯文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM