2017-07-05 1 views
1

gitでエラーログインからメッセージをキャプチャする必要があります。シェルスクリプトでgitエラーログインを取得するには

私は次のコードのセクションがあります。そして、シェルスクリプトは、ユーザー名とパスワードFOT私に尋ねる

#!/bin/bash 

... 

git push origin master 

を:github.comため

ユーザー名:

パスワードをgithub.comため:

無効なユーザー名とpaを挿入しますssowrd。その後、Gitは私にエラーメッセージを与える:

リモート:無効なユーザー名またはパスワード。

致命的な:認証とは、どのように私は変数にシェルスクリプトでこれらのエラーメッセージをキャプチャすることができhttps://github.com/.../.../

に失敗しましたか?

感謝:)

答えて

0

それはと私と一緒に動作します '$?'

$ 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 

コマンドが成功した場合は、$? (var = $?のような)変数にキャッチするだけでよいでしょう。

ちょうどこの(標準エラー出力をファイルに)のようなあなたのエラー出力をリダイレクトし、エラーメッセージをキャッチする:

$ 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/' 

またはこの(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/' 
+0

ありがとう!それは働いている! – n0z

関連する問題