Bash脚本构建正确的$ cmd但无法执行复杂流

时间:2014-01-31 15:58:32

标签: bash cut

这个简短的脚本每天擦除一些日志文件以创建一个简单的提取。它可以从命令行工作,当我回显$ cmd并复制/粘贴时,它也可以工作。但是当我尝试从脚本本身执行时它会中断。

我知道这可能会改善模式的噩梦,但是我错过了一些简单的方法才能正确执行此操作吗?

#!/bin/bash
priorday=$(date --date yesterday +"%Y-%m-%d")
outputfile="/home/CCHCS/da14/$priorday""_PROD_message_processing_times.txt"
cmd="grep 'Processed inbound' /home/rules/care/logs/RootLog* | cut -f5,6,12,16,18 -d\" \" | grep '^"$priorday"' | sed 's/\,/\./' | sed 's/ /\t/g' | sed -r 's/([0-9]+\-[0-9]+\-[0-9]+)\t/\1 /' | sed 's/  / /g' | sort >$outputfile"
printf "command to execute:\n"
echo $cmd
printf "\n"
$cmd

输出中:

  
    

./ make_log_extract.sh命令执行:grep'Processed inbound'/home/rules/care/logs/RootLog.log /home/rules/care/logs/RootLog.log.1     /home/rules/care/logs/RootLog.log.10     /home/rules/care/logs/RootLog.log.11     /home/rules/care/logs/RootLog.log.12     /home/rules/care/logs/RootLog.log.2     /home/rules/care/logs/RootLog.log.3     /home/rules/care/logs/RootLog.log.4     /home/rules/care/logs/RootLog.log.5     /home/rules/care/logs/RootLog.log.6     /home/rules/care/logs/RootLog.log.7     /home/rules/care/logs/RootLog.log.8     /home/rules/care/logs/RootLog.log.9 | cut -f5,6,12,16,18 -d“”| grep的     '^ 2014-01-30'| sed's /\,/。/'| sed's / / \ t / g'| sed -r     's /([0-9] + - [0-9] + - [0-9] +)\ t / \ 1 /'| sed's / / / g'|分类     /home/CCHCS/da14/2014-01-30_PROD_message_processing_times.txt

  
     

grep:5,6,12,16,18:没有这样的文件或目录

1 个答案:

答案 0 :(得分:3)

作为grebneke comments,不要存储命令然后执行它。

你可以做的是执行它,但首先打印它:Bash: Print each command before executing?

priorday=$(date --date yesterday +"%Y-%m-%d")
outputfile="/home/CCHCS/da14/$priorday""_PROD_message_processing_times.txt"

set -o xtrace # <-- set printing mode "on"
grep 'Processed inbound' /home/rules/care/logs/RootLog* | cut -f5,6,12,16,18 -d\" \" | grep '^"$priorday"' | sed 's/\,/\./' | sed 's/ /\t/g' | sed -r 's/([0-9]+\-[0-9]+\-[0-9]+)\t/\1 /' | sed 's/  / /g' | sort >$outputfile"
set +o xtrace # <-- revert to normal