2

So i'm quite new to bash and im just writing a basic script to run an application utility for me. 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. Any help would be greatly appreciated.

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. Not sure why though?

UPDATE 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?

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.

Community
  • 1
  • 1
Justin S
  • 1,409
  • 4
  • 22
  • 38
  • 2
    Look at a hex dump of the output. eg, `job_late.sh ctm... | xxd` – William Pursell Aug 17 '15 at 00:59
  • 1
    Please display `$condition` in hex. You can use: `echo "$condition"|od -xc`. Make sure `"$condition"` is inside double quotes here, otherwise we will miss extra whitespace. Further to @JohnathanLeffler's deleted post, please clarify your output. – cdarke Aug 17 '15 at 06:33
  • Are you "sourcing" this script using the `source` or dot (`.`) commands? If you are not, then the `return` statements are invalid - they can only be used in a sourced file or a function. You probably mean `exit`. – cdarke Aug 17 '15 at 06:37

1 Answers1

0

There is a space before CONDITION in the captured cmd_output.

tripleee
  • 175,061
  • 34
  • 275
  • 318