1

我在 Windows 机器上我打开一个 git bash shell 并运行

logparser "select date from testFile.log" -i:IISW3C -o:CSV

这行得通。

但是如果我创建一个 .sh 文件

cmd_to_run='logparser "select date from testFile.log" -i:IISW3C -o:CSV'
$cmd_to_run

这不起作用,我得到 logparser 错误:

Error: detected extra argument ""select" after query

我只需要通过 .sh 脚本运行 logparser。你能帮忙吗?

4

2 回答 2

0

首先,你为什么不直接运行它呢?

转义查询中的空格,因为 bash 会扩展它,并且您会获得 logparser 的位置参数而不是保留的字符串。如果这不起作用,请尝试eval.

为了说明,这里有一个与 wc 类似的例子:

$ wc -w <<< "select date from testFile.log"
4
$ cmd_to_run='wc -w <<< "select date from testFile.log"'
$ $cmd_to_run
wc: <<<: No such file or directory
wc: "select: No such file or directory
wc: date: No such file or directory
wc: from: No such file or directory
wc: testFile.log": No such file or directory
0 total
$ eval $cmd_to_run # quote as appropriate
4
于 2012-07-01T18:04:42.513 回答
0

事实上,我多年来一直在这样做。它使查询的可读性比从 cmd 提示符运行要好得多。这是我在 Cygwin Bash 中运行的示例 shell 脚本。

dumpBiz_All_aspx.sh

#! /usr/bin/bash
# cd /cygdrive/c/apps/jsd/log_analysis
# . ./dumpBiz_All_aspx.sh
REPORT_NAME=all_aspx
ROOTDIR=D:\\_other\\access_log_analysis
LOGDIR=${ROOTDIR}\\new_server
LOGDATE=21*
SITE=mysite
DATE=`date +%y%m%d.%H%M%S`
OUTPUT_FILE=C:\\apps\\jsd\\log_analysis\\reports\\${SITE}_${REPORT_NAME}_${DATE}.csv


SQL=$(cat <<-EOF
 SELECT To_Lowercase(cs-uri-stem) as Res, cs-method as Method, COUNT(*) as Hits 
 INTO ${OUTPUT_FILE}
 FROM ${LOGDIR}\\u_ex${LOGDATE}.log
 WHERE Res LIKE '%.aspx'
    AND sc-status = 200
    AND cs-host > ''
    AND cs-host NOT LIKE '192%'
    AND Res LIKE '/${SITE}/%'
 GROUP BY Res, cs-method
 ORDER BY Hits DESC
EOF
)

echo SQL = $SQL
echo

cat << EOF | sh
 LogParser -i:IISW3C "${SQL}" -o:CSV
EOF


echo Launching $OUTPUT_FILE ...

cmd.exe /c start ${OUTPUT_FILE}

输出:

SQL = SELECT To_Lowercase(cs-uri-stem) as Res, cs-method as Method, COUNT(*) as Hits INTO C:\apps\jsd\log_analysis\reports\mysite_all_aspx_211005.155939.csv FROM D:\_other\access_log_analysis\new_server\u_ex21*.log WHERE Res LIKE '%.aspx' AND sc-status = 200 AND cs-host > '' AND cs-host NOT LIKE '192%' AND Res LIKE '/backoffice/%' GROUP BY Res, cs-method ORDER BY Hits DESC

Statistics:
-----------
Elements processed: 55780063
Elements output:    167
Execution time:     240.30 seconds (00:04:0.30)

Launching report: mysite_all_aspx_20211005.155442.csv ...

这会生成一个文件mysite_all_aspx_20211005.155442.csv,并在默认的 csv 阅读器(如 Excel)中打开。

于 2021-10-05T20:13:17.440 回答