Bash scripting access

So this might actually be the wrong place for this since it's probably a bash scripting question but I'm trying to learn with the CLI in Ubuntu and I'm writing a script to grab a username and password (once signed in) for a specified site. What I've got so far is below:

#!/bin/bash

usage()
{
cat << EOF
usage: $0 options

options:
    -h    show this message
    -v    specify the vault
    -s    specify the site
EOF
}

VAULT=
SITE=

while getopts "hv:s:" OPTION 
do
    case $OPTION in
        h) 
            usage
            exit 1;;
        v) 
            VAULT=$OPTARG;;
        s) 
            SITE=$OPTARG;;
        ?)
            usage
            exit;;
    esac
done

if [[ -z $VAULT ]] || [[ -z $SITE ]] 
then
    usage
    exit 1
fi

username="$(op get item \"$SITE\" --vault=$VAULT | jq '.details.fields[] | select(.designation=="username").value')"
echo "CMD is op get item \"$SITE\" --vault=$VAULT | jq '.details.fields[] | select(.designation==\"username\").value')"
echo $username

When the command is run from the script (third line from the bottom), I get an "Unable to find item" error. However if I run the command on my own in a bash shell it pulls the user name just fine. Am I missing something really obvious?


1Password Version: Not Provided
Extension Version: Not Provided
OS Version: Ubuntu 16.04
Sync Type: Not Provided

Comments

  • rickfillionrickfillion Junior Member

    AgileBits Team Member

    Hi @Jonathan Tom,

    It looks like you forgot to escape the quotes surrounding "username".

    So instead of:

    username="$(op get item \"$SITE\" --vault=$VAULT | jq '.details.fields[] | select(.designation=="username").value')"
    

    You probably want:

    username="$(op get item \"$SITE\" --vault=$VAULT | jq '.details.fields[] | select(.designation==\"username\").value')"
    

    I suspect that that will make it work. You did it right for the line below it with echo.

    Rick

  • Hi @rickfillion,

    Thanks so much for the response but unfortunately that didn't do it, I get a compile error with that solution:

    jq: error: syntax error, unexpected INVALID_CHARACTER (Unix shell quoting issues?) at <top-level>, line 1: .details.fields[] | select(.designation==\"username\").value jq: 1 compile error

    A little backstory/explanation about this: I'm trying to handle items whose names have spaces in them by encasing the request in double quotes. In playing around today I got an error with SITE being set to "Some Site" that was similar to "Unable to find: Some " which made me think that it might be a case of escaping the space so I played around with a variety of SITE="$(echo $SITE | sed 's/ /\\ /g'" but didn't get anywhere with it.

    Am I going about this correctly to try to search for vault items with spaces in their names?

    Thanks again for your input.

    jt

  • rickfillionrickfillion Junior Member

    AgileBits Team Member

    I'm far from being the best bash scripter out there. I'm not seeing anything obviously wrong with what you're doing.

    I'd look at solving this by breaking this down into more lines. Something like:

    item=$(op get item "$SITE" --vault=$VAULT)
    username=$(echo $item | jq '.details.fields[] | select(.designation=="username").value')
    

    Rick

  • Sorry for the delay, I've been busy with work recently. Splitting up the statements solved the issue - apparently something with having it all in a single evaluation was causing the problem. I'll keep that tidbit in the bag of tricks!

    Thanks again for helping me out with this Rick!

  • beyerbeyer

    AgileBits Team Member

    @Jonathan Tom: On behalf of Rick, you're very welcome!

    If we can be of further assistance, please let us know. We are always here to help!

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file