我正在学习linux脚本,我排练了现有的主题,请帮忙。 在日期时间格式中避免单引号的帮助我正在构建最后2分钟的日期时间,例如 - ' - 2015-02-09 11:56。* INFO -e 2015-02-09 11:55。* INFO'
当我在$X
之类的变量中使用字符串时
它转换像 - : - e 2015-02-09 '11:56。* INFO'-e 2015-02-09 '11:55。* INFO'
在时间值之前使用单引号
我需要一些帮助,因为我可以将$x
替换为:
-e'2015-02-09 11:56。* INFO'-e'2015-02-09 11:55。* INFO'
以下是我的示例脚本:
#!/usr/bin/ksh -x
TEST_HOME=/home/
TEST_HOME_FILE=/home/tempop.txt
LOG_FILE=/home/testlogMin.txt
echo "test before"
grepPattern=''
cnt=1
finalGrepPattern=''
timeInMin=2
while [[ $cnt -le $timeInMin ]]; do
echo $cnt
date_var=`date --date="$cnt minutes ago" "+%Y-%m-%d %H:%M`
#grepPattern=$grepPattern" -e "`date --date="$cnt minutes ago" "+%Y-%m-%d %H:%M"`".*INFO"
#grepPattern=$grepPattern" -e "`date --date="$cnt minutes ago" "+%Y-%m-%d %H:%M"`".*INFO"
grepPattern="${grepPattern} -e ${date_var}"".*INFO"
#grepPattern=$grepPattern" -e "`date --date="$cnt minutes ago" "+%Y-%m-%d %H:%M"`".*ERROR"
#grepPattern=$grepPattern" -e "`date --date="$cnt minutes ago" "+%Y-%m-%d %H:%M"`".*WARNING"
#grepPattern=$grepPattern" -e \""`date --date="$cnt minutes ago" "+%Y-%m-%d %H:%M"`".*INFO"\" # I changed this line
((cnt++))
done
finalGrepPattern=$grepPattern
echo ">>>>>>>>>>>>>>>>>>"
echo "$finalGrepPattern"
echo "*******"
echo $finalGrepPattern
echo "<<<<<<<<<<<<<<<<<"
if [ -s $LOG_FILE ]; then
echo "File $LOG_FILE exists and its non-zero in size. Proceeding with greping last 10 mins file contents."
echo "$LOG_FILE"
egrep $finalGrepPattern $LOG_FILE > $TEST_HOME/tempop.txt
chmod 755 $TEST_HOME/tempop.txt
echo "after egrep"
if [ -s $TEST_HOME_FILE ]; then
echo "$TEST_HOME_FILE is not empty. Sending the file to XXXX server."
fi
else
echo "Did not find LOG_FILE GE ZERO"
fi
exit;
以下是最近一次运行的输出日志:
[XXXXX@ccgtestserver ~]$ ./test
+ TEST_HOME=/home/XXXXX
+ TEST_HOME_FILE=/home/XXXXX/tempop.txt
+ LOG_FILE=/home/XXXXX/testlogMin.txt
+ echo 'test before'
test before
+ grepPattern=''
+ cnt=1
+ finalGrepPattern=''
+ timeInMin=2
+ [[ 1 -le 2 ]]
+ echo 1
1
+ date '--date=1 minutes ago' '+%Y-%m-%d %H:%M'
+ date_var='2015-02-09 19:52'
+ grepPattern=' -e 2015-02-09 19:52.*INFO'
+ ((cnt++))
+ [[ 2 -le 2 ]]
+ echo 2
2
+ date '--date=2 minutes ago' '+%Y-%m-%d %H:%M'
+ date_var='2015-02-09 19:51'
+ grepPattern=' -e 2015-02-09 19:52.*INFO -e 2015-02-09 19:51.*INFO'
+ ((cnt++))
+ [[ 3 -le 2 ]]
+ finalGrepPattern=' -e 2015-02-09 19:52.*INFO -e 2015-02-09 19:51.*INFO'
+ echo '>>>>>>>>>>>>>>>>>>'
>>>>>>>>>>>>>>>>>>
+ echo ' -e 2015-02-09 19:52.*INFO -e 2015-02-09 19:51.*INFO'
-e 2015-02-09 19:52.*INFO -e 2015-02-09 19:51.*INFO
+ echo '*******'
*******
+ echo -e 2015-02-09 '19:52.*INFO' -e 2015-02-09 '19:51.*INFO'
2015-02-09 19:52.*INFO -e 2015-02-09 19:51.*INFO
+ echo '<<<<<<<<<<<<<<<<<'
<<<<<<<<<<<<<<<<<
+ [ -s /home/XXXXX/testlogMin.txt ]
+ echo 'File /home/XXXXX/testlogMin.txt exists and its non-zero in size. Proceeding with greping last 10 mins file contents.'
File /home/XXXXX/testlogMin.txt exists and its non-zero in size. Proceeding with greping last 10 mins file contents.
+ echo /home/XXXXX/testlogMin.txt
/home/XXXXX/testlogMin.txt
+ egrep -e 2015-02-09 '19:52.*INFO' -e 2015-02-09 '19:51.*INFO' /home/XXXXX/testlogMin.txt
+ 1> /home/XXXXX/tempop.txt
egrep: 19:52.*INFO: No such file or directory
egrep: 19:51.*INFO: No such file or directory
+ chmod 755 /home/XXXXX/tempop.txt
+ echo 'after egrep'
after egrep
+ [ -s /home/XXXXX/tempop.txt ]
+ exit
答案 0 :(得分:0)
我有一个粗略的解决方案,最好在连接日期字符串之前完成,使用tr和sed:
x="2015-02-09 '11:56.*INFO'"
x=`echo "$x" | tr -d "'" | sed "s/\([0-9].*INFO\)/'\1'/g"`
如果同时包含日期字符串和'-e',则使用它会使解析变得困难。
答案 1 :(得分:0)
单引号没问题。引号仅在调试输出中添加,因为它避免了*
(星号)扩展。删除shebang中的调试标志-x
。您无法修改调试输出:
+ echo -e 2015-02-09 '19:52.*INFO' -e 2015-02-09 '19:51.*INFO'
2015-02-09 19:52.*INFO -e 2015-02-09 19:51.*INFO
以+
开头的一行调试输出;)
[/]$ ksh -c "echo *"
archive bin boot cdrom dev etc hdd home initrd.img initrd.img.old lib lib64 lost+found media mnt opt proc root run sbin source srv sys tmp usr var vertica vmlinuz vmlinuz.old
[/]$ ksh -c "echo '*'"
*