简体   繁体   English

Linux bash 脚本:如何将命令行放入变量中?

[英]Linux bash script: How to put command line into variable?

I have a questions about bash script, i want to put part of find expression to variable.我对 bash 脚本有疑问,我想将部分查找表达式放入变量中。 I show on example better what i want to.我通过示例更好地展示了我想要的东西。 I have something like this:我有这样的事情:

find $DIR -type f -name "$NAME" $SIZE $CONTENT_COM;;

and i want to put into $CONTENT_COM something like this: (exactly like this)我想把这样的东西放入 $CONTENT_COM 中:(就像这样)

-exec grep -l "$CONTENT" {} +

For $SIZE i made this:对于 $SIZE 我做了这个:

SIZE=${SIZE/$SIZE/-size $SIZE};

and i wanted to make this for $CONTENT_COM (it looks similar to what i want, just change -size $SIZE etc to look like this我想为 $CONTENT_COM 做这个(它看起来与我想要的相似,只需将 -size $SIZE 等更改为如下所示

CONTENT_COM=${CONTENT_COM/$CONTENT/-exec grep -l "$CONTENT" {} +} 

but it doesnt work.但它不起作用。 ({} +} <---- error in editor) Is there any way to put such expression to variable then use it? ({} +} <---- 编辑器中的错误) 有没有办法将这样的表达式放入变量然后使用它?

Yes, you can use arrays to build up commands with arbitrary arguments , for example:是的,您可以使用 arrays 来构建带有任意 arguments 的命令,例如:

search_term='some regex'
content_command=('grep' '-l' "$search_term")
find . -exec "${content_command[@]}" {} +

Also, Use More Quotes™ !此外,使用更多报价™

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM