如何在子目录中搜索 .c 文件并编译它们(shell 脚本) [英] How to search subdirectories for .c files and compile them (shell scripting)

查看:51
本文介绍了如何在子目录中搜索 .c 文件并编译它们(shell 脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要取一个参数,它是当前目录的目录,并搜索其文件夹并编译这些文件夹中的任何 C 文件.我刚刚开始在 Bash 中编写 shell 脚本,有点超出我的头脑.

I need to take an argument which is a directory of the current directory and search its folders and compile any C files in those folders. I'm just beginning shell scripting in Bash and am a little over my head.

到目前为止,我尝试使用的方法包括使用 find 搜索文件,然后将其通过管道传输到 xargs 进行编译,但一直收到错误消息,指出 testing.c 不是目录.

So far things I've tried included using find to search for the files and then pipe it to xargs to compile but kept getting an error saying that testing.c wasn't a directory.

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

我也试过 ls -R 在文件夹中搜索 .c 文件,但不知道如何将路径作为参数然后移动到并编译?

I've also tried ls -R to search folders for .c files but don't know how to then take the paths as arguments to then move to and compile?

推荐答案

find directory -type f -name "*.c" -exec sh -c \
  'cd $(dirname $1);make $(basename $1 .c)' sh {} \;

这篇关于如何在子目录中搜索 .c 文件并编译它们(shell 脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆