所以我对bash很新,我只是编写一个基本脚本来为我运行一个应用程序实用程序。我想要的只是检查输出是否有特定的字符串响应,并记录是否有错误。现在,如果我只使用这一行,我就可以使脚本正常工作
if [[ $condition == *"added"* ]] ; then
一旦我试图寻找一个精确的比较
if [[ "$condition" == "CONDITION:${2} DATE:${3} added" ]] ; then
即使从我能看到的输出是正确的,它也不起作用。我有一种感觉,我正在比较错误或一些秘密字符被添加,这是一个bash功能,我还没有学到。任何帮助将不胜感激。
UPDATE!
因此,根据我给出的反馈,我试图让我的代码更清晰,并了解原始字符串值是什么。感谢大家到目前为止回复。以下是更新的脚本和输出
#!/usr/bin/sh
# This script posts a condition sent to it by the Shout Destination table
# in CCM. Used to generate JOB_LATE incidents.
# If an error is encountered it is logged in the
# job_late_error_log.txt file.
# utility -> ctmcontb -ADD <Condition Name> <Condition Date>
# Condition format -> L@%%JOBNAME-%%NODEID -> JOB_LATE format
# $2 = L@%%JOBNAME-%%NODEID $3 = <Condition Date>
cmd_output=$(ctmcontb -ADD $2 $3)
time_stamp=$(date)
working_directory=/apps/ctm/devsv/scriptsadmin/do_condition/
error_file=job_late_error_log.txt
write_file_to_location=$working_directory$error_file
#if [[ $cmd_output == *"added"* ]] ; then
echo below is the hex output of "CONDITION:${2} DATE:${3} added"
echo "CONDITION:${2} DATE:${3} added" | od -xc
echo below is the command for $cmd_output
echo "$cmd_output" | od -xc
cmd_ouput_no_space = "$(echo -e "${cmd_output}" | tr -d '[[:space:]]')"
echo below is the command with white spaces removed
echo $cmd_ouput_no_space
if [[ "$cmd_ouput_no_space" == "CONDITION:${2} DATE:${3} added" ]] ; then
echo $cmd_ouput_no_space
echo successfull ran util
exit 0
else
# echo $cmd_ouput_no_space $time_stamp >> $write_file_to_location
echo $cmd_ouput_no_space
echo error occurred running util
exit 1
fi
和输出:
ctmtest1-tctmsv80 [9] job_late.sh ctmtest1 u350932-test2 0816
below is the hex output of CONDITION:u350932-test2 DATE:0816 added
0000000 434f 4e44 4954 494f 4e3a 7533 3530 3933
C O N D I T I O N : u 3 5 0 9 3
0000020 322d 7465 7374 3220 4441 5445 3a30 3831
2 - t e s t 2 D A T E : 0 8 1
0000040 3620 6164 6465 640a
6 a d d e d \n
0000050
below is the command for CONDITION:u350932-test2 DATE:0816 added
0000000 2043 4f4e 4449 5449 4f4e 3a75 3335 3039
C O N D I T I O N : u 3 5 0 9
0000020 3332 2d74 6573 7432 2044 4154 453a 3038
3 2 - t e s t 2 D A T E : 0 8
0000040 3136 2061 6464 6564 0a00
1 6 a d d e d \n
0000051
job_late.sh[19]: cmd_ouput_no_space: not found
below is the command with white spaces removed
error occurred running util
我不熟悉od -xc调用,但看起来我的值是不同的,这是有道理的。不确定为什么呢?
更新2:
所以我尝试使用line
删除所有空格cmd_ouput_no_space = "$(echo -e "${cmd_output}" | tr -d '[[:space:]]')"
我在这里找到了:
How to trim whitespace from a Bash variable?
但它似乎不起作用。我已经更新了我原来的脚本并在这篇文章的顶部输出了错误即可。
答案 0 :(得分:0)
在捕获的CONDITION
中cmd_output
之前有一个空格。