0

Failed to capture the response for the below command;

URL="https://gsdfdsfithub.com/gitexpert/testGithub.git" > /dev/null 

git ls-remote $URL -q

if [ $? -nq 0 ]; then
    echo "Failed, please provide valid url"
fi

output:

fatal: unable to access 'https://gsdfdsfithub.com/gitexpert/testGithub.git/': Received HTTP code 404 from proxy after CONNECT
128
line 4: [: -nq: binary operator expected

I tried the above code snippet but still it's catching the error. I want to suppress the error message , and have custom message as an output. like below

"Failed, please provide valid url"
torek
  • 448,244
  • 59
  • 642
  • 775
itgeek
  • 549
  • 1
  • 15
  • 33
  • You want the `-ne` operator, not `-nq`, but it'd be even better to put the command directly in the `if` clause (`if ! git ls-remote "$URL" -q; then`, see [this question](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern)). [shellcheck.net](https://www.shellcheck.net) will point out many problems like these. Also, why are you redirecting the output of an assignment command (which doesn't produce any output) to /dev/null? – Gordon Davisson May 13 '22 at 01:36
  • There are several problems with your script. May I suggest running it through [shellcheck](https://www.shellcheck.net/)? – user1934428 May 13 '22 at 06:17

1 Answers1

1

Print stdout and stderr to /dev/null.

URL="https://gsdfdsfithub.com/gitexpert/testGithub.git" > /dev/null 

git ls-remote $URL -q >> /dev/null 2>&1

if [ $? != 0 ]; then
    echo "Failed, please provide valid url"
fi
过过招
  • 3,722
  • 2
  • 4
  • 11