查找命令在 -exec arg 上大惊小怪 [英] find command fusses on -exec arg

查看:13
本文介绍了查找命令在 -exec arg 上大惊小怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从脚本构建和运行 find 命令.但我从 find 收到一条非常神秘的错误消息.下面基本总结了我是如何构建命令行并运行的

I am trying to build and run a find command from a script. But I get a very cryptic error message from find. The following basically sums up how I build the command line and run it

$ xx="find . -name 'p*' -mmin +10 -exec echo {} \;"
$ echo "$xx" #.....and I get the same print from echo $xx
find . -name 'p*' -mmin +10 -exec echo {} ;
$ $xx
find: missing argument to `-exec'
$ find . -name 'p*' -mmin +10 -exec echo {} ;
./p2.sh
./p1.sh
$ read xx
find . -name 'p*' -mmin +2 -exec echo {} \;
$ $xx
find: missing argument to `-exec'

我被困住了,感谢您的帮助.我也想知道是什么原因造成的.我在 SLES 上使用 bash 3.2.51.

I am stuck and will appreciate your help. I am also wondering what's causing this. I am using bash 3.2.51 on SLES.

我要执行的实际命令有点长,但我在这里使用 echo 只是为了说明.

The actual command I want to execute is a little bit longer but I used echo here just to illustrate.

谢谢饭菜

推荐答案

试图将复杂的命令存储在 bash 变量中,然后很好地评估变量是行不通的.

Trying to store complicated commands in bash variables and then evaluate the variables pretty well never works.

如果您需要分段构建命令,请使用数组.请参阅这个有用的 Bash 常见问题解答:我正在尝试将命令放入变量中,但复杂的情况总是失败!.

If you need to build a command in pieces, use an array. See this useful Bash FAQ: I'm trying to put a command in a variable, but the complex cases always fail!.

这是基本策略:

# Make an array
declare -a findcmd=(find .)
# Add some arguments
findcmd+=(-name 'p*')
findcmd+=(-mmin +10)
findcmd+=(-exec echo {} ;)
# Run the command
"${findcmd[@]}"

您需要了解 bash 引用的工作原理.请记住,引用(和取消引用)只发生一次,当您键入命令时(或当 bash 从脚本文件中读取它时).进入变量值的引号只是普通字符.

You need to understand how bash quoting works. Remember that the quoting (and de-quoting) only happens once, when you type the command (or when bash reads it from a script file). Quotes which get into the values of variables are just ordinary characters.

如果您正在尝试使用 set -x,还请记住 set -x 插入引号 以消除歧义.这些引号不是变量的一部分.虽然这显然是必不可少的,但对于不熟悉 bash 执行模型的程序员来说,这似乎会让人感到困惑.

If you're experimenting with set -x, remember also that set -x inserts quotes in order to remove ambiguities. These quotes are not part of the variables. While that is clearly essential, it seems to be confusing to programmers who are not familiar with the bash execution model.

这篇关于查找命令在 -exec arg 上大惊小怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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