1

I have written a c program, which works, it works on the terminal. And I wanted to run a shell script to test my code. The issue is I can't seem to figure out how to get the output of my q1_sequence to match with my expected value.

#!/bin/sh

OUTPUT="$(./q1_sequence 5 1 3 2 1)"
echo $OUTPUT

EXPECTED="5 1 3 2 1 : returns a value of 99 and 15 iterations"

if [ "$OUTPUT" == "$EXPECTED" ]
then
    echo "Test was true"
else
    echo "Test was not true"
fi

I expected this to be true, and for the console to state that this was true. however I don't get that at all.

What I get is:

sh testing.sh
5 1 3 2 1 : returns a value of 99 and 15 iterations testing.sh: 8: [: 5 1 3 2 1 : returns a value of 99 and 15 iterations: unexpected operator Test was not true

I'm not sure what I'm doing wrong, or what the unexpected operator is or was. I've tried a lot of different things, and I think I'm missing something but I can't for the life of me figure it out. I've done OUTPUT == "5 1 3 2 1 : returns a value of 99 and 15 iterations"

I've tried a lot of different sort of combinations, but I can't just figure it out so any help would be helpful.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    This is not `c`. It's `bash`. – EUS May 30 '19 at 15:39
  • try to find the difference in strings. https://duckduckgo.com/?q=bash+strings+difference&t=ffsb&ia=web. Possible duplicate question. – Dudi Boy May 30 '19 at 16:00
  • When displaying the output from the C program check for unexpected whitespace, use something like `echo "<$OUTPUT>"`. The `< >` are there just to detect additional spaces at start or end, which you would not otherwise see. Also use quotes, there could be multiple spaces between words and these would be hidden without quotes. – cdarke May 30 '19 at 18:32

1 Answers1

1

Use a single equal sign. if [ "$OUTPUT" = "$EXPECTED" ]

sh uses a single equal sign for comparison of strings and not, like in C, == Alternatively, use #!/bin/bash or bash testing.sh.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • POSIX, with the `test` command (`[`) requires `=`, so is better for portability, but bash does not insist, it accepts `==`. – cdarke May 30 '19 at 18:28
  • The single equal sign fixed it. I don't really understand why though because when I looked it up and asked a friend theirs works with the double == so this whole thing has only confused me more. When do I use double equal. When do I use single. Or why that was different but it was fixed. Thank you ljm. – user9416023 May 31 '19 at 10:42
  • @cdarke: right. but OP uses `#!/bin/sh` (I corrected the answer; thx) – Ljm Dullaart Jun 01 '19 at 10:53
  • @user9416023: you are using `sh`, which is probably not bash. I added some extra info to the answer (and corrected an error). If that clears it up sufficiently, please mark the question as solved. – Ljm Dullaart Jun 01 '19 at 11:00
  • @LjmDullaart: quite right, I had missed that. – cdarke Jun 01 '19 at 14:35
  • @LjmDullaart: you ask when you should use `=` and when `==`: that is language dependant - i.e.which shell you are using. It is safer to use `=` with the `[` command and the `[[`keyword. `bash` (and some other shells) support `(( ))` for arithmetics, and this requires `==`. – cdarke Jun 01 '19 at 14:40