2

i need to capture message from error login in git.

I have the following code section:

#!/bin/bash

...

git push origin master

Then the shell script ask me fot username and password:

Username for github.com:

Password for github.com:

So I insert a invalid username and passowrd. Then Git give me a error message:

remote: Invalid username or password.

fatal: Authentication failed for https://github.com/.../.../

How can i capture these error messages in a shell script to a variable?

Thanks :)

1 Answer 1

1

it works with me with '$?'

$ git push origin master
Username for 'https://github.com': sld,sldk
Password for 'https://sld,[email protected]': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/KamelHacene/configeuh.git/'
$ echo $?
128

If the command has succeded, $? should return 0. Just catch it in a variable (like 'var=$?') and you'll be good to go.

To catch the error message, just redirect your error output like this (stderr to file):

$ git push origin master 2>plop
Username for 'https://github.com': lskdls
Password for 'https://[email protected]': 
$ cat plop
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/KamelHacene/configeuh.git/'

Or this (stderr to stdout. Use a variable to catch stdout) :

$ ploop=$(git push origin master 2>&1)
Username for 'https://github.com': skjdksd
Password for 'https://[email protected]': 
$ echo $ploop
remote: Invalid username or password. fatal: Authentication failed for 'https://github.com/KamelHacene/configeuh.git/' 
Sign up to request clarification or add additional context in comments.

1 Comment

This is the line I needed: ploop=$(git push origin master 2>&1).

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.