I have a script which logs in to a server. Regardless of the success (i.e. whether the password is valid or not) the script will exit with return code 0, and the text:
Error login failed.
I'd like it to retry login if this happens by running another command, but because of the return code I can't.
If I pipe the output to grep for 'Error' so I can execute a command based on that then the login dialog doesn't show up in the terminal.
Is there anyway around this?
Pseudo code as this is for work:
loginCmd && echo "$?"
username:
password:
Error login failed.
0
Update
I tried using an idea from a commentator, but I can't capture the text with 'Error' as that line occurs after the command completes...
Here's what it actually looks like, with incorrect password:
agent registration
username: xyz
password:
Error login failed.
I tried this:
result=$(loginCmd | tee /dev/tty)
grep -q 'Error' <<< "$result" &&
loginCmd
The error string outputs after the command completes, so result
doesn't capture it:
result=agent registration
So I guess there is no way to capture this string?
Error login failed.
Any ideas?
Ideally on login failure the script would return 1, or have some sort of retry mechanism, but it doesn't and I can't change that, it's a company script and this only affects the terminal, not the GUI which is what customers use.
loginCmd | tee tempfile
and thengrep error tempfile
, but I suspect that will break somehowloginCmd
cannot output the error message afterloginCmd
completes. Maybe it writes the error message to stderr, or maybe directly to the tty. What happens if you runloginCmd 2> /dev/null
? How aboutresult=$(loginCmd 2>&1 | tee /dev/tty)
?