Bash脚本构建正确的$ cmd,但无法执行复杂的流 [英] Bash script builds correct $cmd but fails to execute complex stream

查看:74
本文介绍了Bash脚本构建正确的$ cmd,但无法执行复杂的流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This short script scrapes some log files daily to create a simple extract. It works from the command line and when I echo the $cmd and copy/paste, it also works. But it will breaks when I try to execute from the script itself.

我知道这是我可以改善的噩梦,但是我缺少一些简单的方法吗?只需正确执行此操作即可?

I know this is a nightmare of patterns that I could probably improve, but am I missing something simple to just execute this correctly?

#!/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'处理入站'/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 |切-f5,6,12,16,18 -d | grep
‘^ 2014-01-30’| s’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

./make_log_extract.sh command to execute: 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' | sort /home/CCHCS/da14/2014-01-30_PROD_message_processing_times.txt

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

grep: 5,6,12,16,18: No such file or directory


推荐答案

grebneke注释,请勿存储该命令然后执行

As grebneke comments, do not store the command and then execute it.

您可以执行的操作是先打印它: Bash:在执行前打印每个命令吗?

What you can do is to execute it but firstly print it: 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

这篇关于Bash脚本构建正确的$ cmd,但无法执行复杂的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆