0

I have this if else command that always result in "no match" although I am expecting it to say "match"

$ skopeo inspect docker://abc:xyz 2>&1
time="2023-03-03T04:33:12Z" level=fatal msg="Error parsing image name \"docker://abc:xyz\": reading manifest xyz in docker-release-abc: manifest unknown: manifest unknown"

$ if skopeo inspect docker://abc:xyz 2>&1 | grep -q "manifest unknown"; then echo "match" ; else echo "no match"; fi
no match

I've tried many things including diverting the stderr to stdout using 2>&1, also tried using >/dev/null but all in vain

Vishal
  • 1
  • 1
  • Related: [How do I use a file grep comparison inside a bash if/else statement?](https://stackoverflow.com/q/2480584/11082165) – Brian61354270 Mar 03 '23 at 21:09
  • 5
    Some commands produce different output when writing to a terminal than a pipe. What does `skopeo inspect docker://abc:xyz 2>&1 | cat` show? – Barmar Mar 03 '23 at 21:11
  • 1
    It would also help you answer this yourself to collect `output=$(skopeo inspect docker://abc:xyz 2>&1)`, at which point you can test code that `grep`s through the contents of `$output`; or send that output to a tool like xxd or hexdump that converts it into unambiguous format so you can see if there are, f/e, hidden characters breaking your `grep`. And if you use `set -x` to enable xtrace logging, then `printf '%s\n' "$output" | grep -q "manifest unknown"` will log that `printf` in a way that will give you a [mre] people who don't have skopeo can run to see the problem themselves. – Charles Duffy Mar 03 '23 at 21:13
  • @Barmar It gives the same result with cat too: $ skopeo inspect docker://abc:xyz 2>&1 | cat time="2023-03-03T21:54:50Z" level=fatal msg="Error parsing image name \"docker://abc:xyz\": reading manifest zyz in docker-release-abc: manifest unknown: manifest unknown" – Vishal Mar 03 '23 at 21:58
  • @Brian Isn't that exactly what they're doing? – Barmar Mar 03 '23 at 21:58
  • Unless `skopeo` is writing directly to `/dev/tty`, your code should work. But in that case you would see the output when you run the command in the `if` statement. – Barmar Mar 03 '23 at 22:00
  • Wouldn't `if ! skopeo inspect docker://abc:xyz >/dev/null; then echo 'failed'; fi` be sufficient? – Andreas Louv Mar 03 '23 at 22:34
  • 1
    Do you have a match when you introduce a tmpfile: `skopeo inspect docker://abc:xyz 2>&1 > /tmp/skopeo.out; grep -o "manifest unknown" /tmp/skopeo.out`? When this grep fails, look what is unexpected in the tmpfile. When `grep` finds something, try `if grep -q "manifest" /tmp/skopeo.out; ..` – Walter A Mar 03 '23 at 23:06
  • 1
    Or try `if skopeo inspect docker://abc:xyz 2>&1 | tee /tmp/skopeo.out | grep -q "manifest unknown"; then echo "match" ; else echo "no match"; fi` and examine tmpfile when ready. – Walter A Mar 03 '23 at 23:08

1 Answers1

0

Using the input you provided, the problem cannot be reproduced with a test script.

Test script:

#!/bin/bash

testval='time="2023-03-03T04:33:12Z" level=fatal msg="Error parsing image name \"docker://abc:xyz\": reading manifest xyz in docker-release-abc: manifest unknown: manifest unknown"'

if echo "${testval}" 2>&1 | grep -q "manifest unknown"
then
    echo "match"
else
    echo "no match"
fi
Eric Marceau
  • 1,601
  • 1
  • 8
  • 11