BASH:if语句执行命令和函数

发布于 2024-11-19 00:03:39 字数 360 浏览 1 评论 0原文

我遇到了一个我认为应该很容易解决的问题,但对于我的一生,我无法解决。可能是真的很晚了;没有把握。

所以我有一个 shell 脚本和一个需要运行的 if 语句。问题是我在这个 bash 脚本中有一个函数,我用它来在 if 语句中实际构建这个 find 命令的一部分。我想知道如何在不收到错误[:太多参数的情况下同时执行这两项操作。

这是当前的代码:

if [ -n `find ./ `build_ext_names`` ];then

这就是我真正需要发布的内容。我需要弄清楚如何在 find 命令中运行 build_ext_names ,而该命令又位于 ifstatement 中

I've run into an issue in which I think should be easy to resolve, but for the life of me, I can't figure it out. Could be that it's really late; not sure.

So I have a shell script and I have an if statement that I need to run. The problem is that I have a function inside this bash script that I am using to actually build part of this find command inside the if statement. I want to know how I can do both, without receiving an error [: too many arguments.

Here's the current code:

if [ -n `find ./ `build_ext_names`` ];then

That's all I really need to post. I need to figure out how to run that build_ext_names inside that find command, which in-turn is inside the ifstatement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友欢 2024-11-26 00:03:39

Michael Aaron Safyan 的想法是正确的,但解决眼前的问题,您可以使用更简单的 $(command) 构造来代替 命令替换。它允许更简单的嵌套:

if [ -n "$(find ./ "$(build_ext_names)")" ]; then

Michael Aaron Safyan has the right idea, but to fix the immediate problem you can just use the simpler $(command) construct instead of ```command` `` for command substitution. It allows for much simpler nesting:

if [ -n "$(find ./ "$(build_ext_names)")" ]; then
无力看清 2024-11-26 00:03:39

如果你把它分开的话会更容易:

function whateverItIsYouAreTryingToDo() {
   local ext_names=$(build_ext_names)
   local find_result=$(find ./ $ext_names)
   if [ -n "$find_result" ]  ; then
       # Contents of if...
   fi
}

This is easier if you split it up:

function whateverItIsYouAreTryingToDo() {
   local ext_names=$(build_ext_names)
   local find_result=$(find ./ $ext_names)
   if [ -n "$find_result" ]  ; then
       # Contents of if...
   fi
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文