0

Below is my simple bash script

!/usr/bin/sh
cmd_output=$(ctmcontb -ADD $2 $3)
echo below is the hex output of "CONDITION:${2} DATE:${3} added"
echo "CONDITION:${2} DATE:${3} added" | od -xc
echo below is the hex output for the variable cmd_ouput
echo "$cmd_output" | od -xc
echo raw input passed to cmdlet with user arguments inputted and a space added infront
echo " CONDITION:${2} DATE:${3} added" | od -xc
if [ "$cmd_ouput" = " CONDITION:${2} DATE:${3} added" ]; then
        echo successfull ran util
        exit 0
else
        echo error occurred running util
        exit 1
fi

Below is the output

ctmtest1-tctmsv80 [49] job_late.sh ctmtest1 u350932-14 0910
below is the hex output of CONDITION:u350932-14 DATE:0910 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    3134    2044    4154    453a    3039    3130    2061
           2   -   1   4       D   A   T   E   :   0   9   1   0       a
0000040     6464    6564    0a00
           d   d   e   d  \n
0000045
below is the hex output for the variable cmd_ouput
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    2d31    3420    4441    5445    3a30    3931    3020
           3   2   -   1   4       D   A   T   E   :   0   9   1   0
0000040     6164    6465    640a
           a   d   d   e   d  \n
0000046
raw input passed to cmdlet with user arguments in putted and a space added in front
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    2d31    3420    4441    5445    3a30    3931    3020
           3   2   -   1   4       D   A   T   E   :   0   9   1   0
0000040     6164    6465    640a
           a   d   d   e   d  \n
0000046
error occurred running util

So as you can see the string comparison line

if [ "$cmd_ouput" = " CONDITION:${2} DATE:${3} added" ]; then

doesn't appear to successfully compare the strings even though they are from what I can see the same.

I noticed there was a space added to the raw input after it is run through the cmdlet. So to combat this I basically added a space manually in the string comparison (I know not the greatest way of doing it but I ran out of ideas)

Basically im not sure why the string comparison is not working when the strings are the same and my bash code appears correct?

2
  • Can you add the output of bash -x job_late.sh ctmtest1 u350932-14 0910 as well? I notice that your shebang line lacks a #. Other than that, nothing obvious. Commented Sep 10, 2015 at 5:55
  • So I have a more general question, why don't you just make ctmcontb return a return code which indicates if it was successful or not. You can get the return code of the last executed command in bash using "$?" and then case out on this integer, it's a pretty useful trick. Commented Sep 10, 2015 at 6:03

1 Answer 1

1

This version appears to work?

Here was my code.

ctmcontb:

#!/bin/bash
echo " CONDITION:${2} DATE:${3} added"

test2.sh:

#!/bin/bash
cmd_output=$(./ctmcontb -ADD "$2" "$3")
expected=" CONDITION:${2} DATE:${3} added"
echo below is the hex output of "CONDITION:${2} DATE:${3} added"
echo "CONDITION:${2} DATE:${3}" | od -xc
echo below is the hex output for the variable cmd_ouput
echo "$cmd_output" | od -xc
echo raw input passed to cmdlet with user arguments inputted and a space added infront
echo "${expected}" | od -xc
if [ "${cmd_ouput}"="${expected}" ]
then
        echo successfull ran util
        exit 0
else
        echo error occurred running util
        exit 1
fi

Alternately, something like this, based on return codes instead of the exact returned string, seems a lot simpler and more consistent with the rest of unix, even if it requires changing the ctmcontb program a bit:

ctmcontb

#!/bin/bash
if [[ "$1" -eq 1 ]]; then
  exit 0
else
  exit 1
fi

test.sh

#!/bin/bash
./ctmcontb "$1"
result="$?"
if [[ result -eq 0 ]]; then
  echo "Success"
  exit 0
else
  echo "Fail"
  exit 1
fi


$ ./test3.sh 2
Fail
$ ./test3.sh 1
Success
$ ./test3.sh 0
Fail
Sign up to request clarification or add additional context in comments.

1 Comment

The above worked. I did end up though changing my code to use if [ $? -eq 0 ]; as suggested by Chris which just checked the return code is 0. Much simpler and easier to implement :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.