在shell脚本中查找命令错误

时间:2017-06-08 14:19:31

标签: linux bash shell awk find

我有一个文件" 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旁边分配一个整数,它可以正常工作,但是,如果我把&#34; $line | awk '{print $2}'&#34; ,它会抛出以下错误在find命令中。

---------
./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'

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

2 个答案:

答案 0 :(得分:1)

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

尝试替换

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

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

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

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

答案 1 :(得分:1)

更改

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

`echo $line | awk '{print $2}'`
相关问题