[英]linux find command is not taking proper argument
find . -maxdepth 1 ! -path . -type f ! -name "*.gz" ${FILE_PATTERN} -mtime +${DAYS_AFTER_ARCHIVE}
I am trying to execute above command inside the script where ${FILE_PATTERN}
and ${DAYS_AFTER_ARCHIVE}
are the variable provided while executing the script. 我试图在脚本中执行以上命令,其中
${FILE_PATTERN}
和${DAYS_AFTER_ARCHIVE}
是执行脚本时提供的变量。 The variable value for ${FILE_PATTERN}
would be ! -name "*warnings*"
${FILE_PATTERN}
的变量值为! -name "*warnings*"
! -name "*warnings*"
. ! -name "*warnings*"
。 I am looking for executing above command as like below command in script 我正在寻找像脚本中的以下命令一样执行上述命令
find . -maxdepth 1 ! -path . -type d ! -name "*warnings*" -mtime +7
I am providing file pattern argument as "! -name " warnings "" but receiving following error message 我提供文件模式参数为“!-name” warnings “”,但收到以下错误消息
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
suggest on above. 在上面建议。
First of all 首先
-name "*.gz" ${FILE_PATTERN}
has too many option values (this is what usually causes the message shown) 选项值太多(通常会导致显示此消息)
If you use bash or similar, escape the exclamation marks 如果您使用bash或类似工具,请避免出现感叹号
find . -maxdepth 1 \! -path . -type f \! -name "*.gz" ${FILE_PATTERN} -mtime +${DAYS_AFTER_ARCHIVE}
I am providing file pattern argument as "! -name "warnings"" but receiving following error message
我提供的文件模式参数为“!-name“ warnings””,但收到以下错误消息
You can't combine flags and their values like that. 您不能像这样组合标志及其值。 Also, you can't nest
"
like that. So, it could be like 另外,您不能像这样嵌套
"
! -name "$FILE_PATTERN"
If you're using BASH you can make use of BASH arrays: 如果您正在使用BASH,则可以使用BASH数组:
# find options
FILE_PATTERN=(! -name "warnings*")
# build the find command
cmd=(find . -maxdepth 1 ! -path . -type f \( ! -name "*.gz" "${FILE_PATTERN[@}}" \) -mtime +${DAYS_AFTER_ARCHIVE})
# execute the command
"${cmd[@]}"
If not using BASH then you will have to use eval
with caution : 如果不使用BASH,则必须谨慎使用
eval
:
FILE_PATTERN='! -name "warnings*"'
eval find . -maxdepth 1 ! -path . -type f ! -name "*.gz" "${FILE_PATTERN}" -mtime +${DAYS_AFTER_ARCHIVE}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.