[英]bash string comparison with nested variables
So i'm quite new to bash and im just writing a basic script to run an application utility for me. 因此,我对bash还是很陌生,我只是编写了一个基本脚本来为我运行应用程序实用程序。 All i want to is then check the output for a specific string response and log if it has an error. 然后,我只想检查输出是否有特定的字符串响应,并记录是否有错误。 Now I am able to get the script working if I just use this line 现在,只要使用此行,我就能使脚本正常工作
if [[ $condition == *"added"* ]] ; then
as soon as i try to look for a exact comparision 一旦我尝试寻找精确的比较
if [[ "$condition" == "CONDITION:${2} DATE:${3} added" ]] ; then
it does not work even though from what I can see the output is correct. 即使从我看到的输出正确,它也不起作用。 I have a feeling im comparing wrong or some secrete characters are being added which is a bash function I havent learned yet. 我感觉比较错误或正在添加一些秘密字符,这是我尚未了解的bash函数。 Any help would be greatly appreciated. 任何帮助将不胜感激。
UPDATE! UPDATE!
So with the feedback I have been given I have tried to make my code more clear and get some idea of what the raw string values are. 因此,根据反馈,我试图使我的代码更加清晰,并对原始字符串值有所了解。 Thanks everyone so far for replying back. 到目前为止,感谢大家的回复。 Below is the updated script and output 下面是更新的脚本和输出
#!/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
and the output: 和输出:
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
im not to familiar with the od -xc call but it looks like my values are different which make sense. 我不熟悉od -xc调用,但是看起来我的值是不同的,这很有意义。 Not sure why though? 不知道为什么吗?
UPDATE 2: 更新2:
So i tried removing all white spaces using line 所以我尝试使用行删除所有空白
cmd_ouput_no_space = "$(echo -e "${cmd_output}" | tr -d '[[:space:]]')"
that i found here: 我在这里找到的:
How to trim whitespace from a Bash variable? 如何从Bash变量中修剪空格?
but it doesn't seem to work. 但它似乎不起作用。 I have updated my original script and ouput at the top of this post with the errors im having. 我已经使用错误即时消息更新了本文顶部的原始脚本和输出。
捕获的cmd_output
CONDITION
前有一个空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.