【问题标题】:find command error in shell script在 shell 脚本中查找命令错误
【发布时间】:2017-11-10 07:42:28
【问题描述】:

我有一个文件“log_data.txt”,其中包含各种日志文件和带有空格的名称旁边的 mtime。文件内容如下图,

Log1 2
Log2 6
Log3 5
Log4 7
Log5 3

这里的意图是我下面显示的脚本必须为自己分配日志文件名并根据它旁边的 mtime 删除文件。

IFS=''
while read line
do
find /opt/tmp/log/ -name `$line | awk '{print $1}'`*.log -mtime `$line | awk '{print $2}'` -exec rm -rf {} \;
echo ---------
done < log_data.txt

如果我在脚本中的 mtime 旁边分配一个整数,它可以正常工作,但是,如果我在 find 命令中输入 "$line | awk '{print $2}'",它会引发以下错误。

---------
./log_clear_script.sh: line 4: Log1      2: not found
./log_clear_script.sh: line 4: Log1      2: not found
find: invalid argument `-exec' to `-mtime'
---------
./log_clear_script.sh: line 4: Log2     6: not found
./log_clear_script.sh: line 4: Log2     6: not found
find: invalid argument `-exec' to `-mtime'
---------
./log_clear_script.sh: line 4: Log3       5: not found
./log_clear_script.sh: line 4: Log3       5: not found
find: invalid argument `-exec' to `-mtime'
---------
./log_clear_script.sh: line 4: Log4  7: not found
./log_clear_script.sh: line 4: Log4  7: not found
find: invalid argument `-exec' to `-mtime'
---------
./log_clear_script.sh: line 4: Log5        3: not found
./log_clear_script.sh: line 4: Log5        3: not found
find: invalid argument `-exec' to `-mtime'

请帮我解决它。非常感谢所有回复。 - 谢谢。

【问题讨论】:

    标签: linux bash shell awk find


    【解决方案1】:

    改变

     `$line | awk '{print $2}'`
    

    `echo $line | awk '{print $2}'`
    

    【讨论】:

      【解决方案2】:

      变量 $line 不是命令。它只是一个字符串。

      尝试替换

      `$line | awk '{print $1}'`  
      

      `echo -n $line | awk '{print $1}'`  
      

      `$line | awk '{print $2}'`  
      

      `echo -n $line | awk '{print $2}'`
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-26
        • 1970-01-01
        • 2017-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-07
        相关资源
        最近更新 更多