Capture output of op signin

dmillz
dmillz
Community Member

I cannot seem to exit a bash script if op signin company_name is unsuccessful, such as an incorrect password. The script has a couple processes that I'd only like to complete if the signin is successful. Otherwise, I'd like the script to exit.

I've tried to capture the output of the op signin command, but it is always empty. The output is super predictable, since it always starts with [LOG], which is great! But I can't store it in a variable for a conditional exit.

Here's an example of what I'd like to accomplish:

#!/bin/bash

op signin company_name

if [[ $? = *LOG* ]]; then
  echo "Error: Incorrect password"
  exit
fi

# rest of script

Any ideas or best practices? Ideally the user should be able to just fire off the script, type in their password, and either exit or continue based on that result.

1Password CLI version = 0.5.5


1Password Version: Not Provided
Extension Version: Not Provided
OS Version: macOS 10.14.3
Sync Type: Not Provided

Comments

  • MrC
    MrC
    Volunteer Moderator

    @dmillz

    Test the the exit code from op. It is stored in the bash variable $? and will be non-zero when there is an error, and 0 otherwise.

    $ op signin foobar
    ...
    $ echo $?
    1
    
    $ op signin mrc@example.com
    ...
    $ echo $?
    0
    

    If you did want to capture the output, you need to redirect STDERR, since that's where the output is going:

    retmsg=$(op signin foobar 2>&1)
    
  • dmillz
    dmillz
    Community Member

    Ahhh thanks @MrC! My approach originally was a little borked itself, but I couldn't get the exit codes to be reliable until I included 2>&1. Apparently the exit codes normally just default to 0, unless that's included.

    For anyone else looking for a loop to keep prompting for the password upon an incorrect password, here's what I used:

    function one_pass_signin() {
      eval $(op signin foo 2>&1)
    
      if [ $? == 1 ]; then
        echo "ERROR: Incorrect password. Try again."
        one_pass_signin
      fi
    }
    

    Works like a charm!

  • cohix
    cohix
    1Password Alumni

    That's a great trick @dmillz ! Thanks for sharing, glad you got it figured out.

  • jpgoldberg
    jpgoldberg
    1Password Alumni

    Apparently the exit codes normally just default to 0, unless 2>&1 is included.

    Not really. That was only a weird side effect of putting everything into a subshell, and then looking at what the shell returned instead of what op returned.

    We need to distinguish between output on the one hand and exit status on the other. The exit status is just an integer. 0 indicates success and anything other than zero indicates some sort of error. On an incorrect password, op exits with status 145. (Don't count on that undocumented value of 145, but you can count on it being something other than zero.)

    The shell or operator, ||, treats an exit status of 0 as "true", and so we can simplify @dmillz's approach. (I haven't tested this)

    function one_pass_signin() {
      op signin foo || (echo "try again" ; one_pass_signin )
    }
    
This discussion has been closed.