2

So basically, I want to run these 2 commands but I want to check if the keys already existed. I know how to do the ifthenelse in bash but I'm not sure how to get the result and store in variable for me to check so that i can put it in the conditional operator. For example, if it already existed then I can skip downloading and adding it again.

Example 1 (--fetch-keys):

    sudo apt-key adv --fetch-keys https://packages.microsoft.com/keys/microsoft.asc
sudo tee /etc/apt/sources.list.d/vscode.list > /dev/null <<- END
    deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main
END

Example 2 (--recv-keys):

    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B9A06DE3
sudo tee /etc/apt/sources.list.d/inkscape.list > /dev/null <<- END
    deb [arch=amd64] http://ppa.launchpad.net/inkscape.dev/stable-daily/ubuntu bionic main
END

3 Answers 3

1

How about something like:

TEST=$(apt-key list 2> /dev/null | grep my_app_name)
if [[ ! $TEST ]]; then
    echo "Missing - need to run --fetch-keys or --recv-keys"
fi

I piped stderr into the null device ( 2> /dev/null ) to hide the warning you get for using apt-key list in a script.

Try running "sudo apt-key list" on its own so you can see what the output looks like, in case you want to change the "grep" to be more selective.

4
  • Hi there, gpg list gives me different output than the keys i have added using apt-key can you confirm? Are they using the same “keyring” mechanism?
    – sudoranger
    Sep 26, 2019 at 1:47
  • 1
    Thanks for the question. I should have used apt-key list in my example. I've just modified it.
    – Eric Mintz
    Sep 26, 2019 at 10:25
  • I've accepted your answer. It should work now. I can use the subshell to check if one of my keys are matching the one that already existed on the machine, thus skipping them. Thank you so much!
    – sudoranger
    Sep 27, 2019 at 6:43
  • How do you check for .asc or .pub key already exist instead? For example packages.microsoft.com/keys/microsoft.asc or dl.google.com/linux/linux_signing_key.pub
    – sudoranger
    Mar 26, 2020 at 8:46
0

You could save each key in its own file and test if the file exist.

keyring=/etc/apt/trusted.gpg.d/inkscape.gpg
if [ ! -f $keyring ]; then
  sudo apt-key --keyring $keyring \
    adv --keyserver keyserver.ubuntu.com --recv-keys B9A06DE3
fi
sudo tee /etc/apt/sources.list.d/inkscape.list > /dev/null <<- END
deb [arch=amd64] http://ppa.launchpad.net/inkscape.dev/stable-daily/ubuntu bionic main
END
0

the command 'apt-key list' may not list all the keys you already have, in that case 'grep' would not inform you that a key exists.

if you imported a key using 'gpg --import', then 'gpg --list-keys' followed by a grep would inform you if the key exists or not. for example

gpg --list-keys | grep zoom 
if [[ $? -ne 0 ]]; then
  wget https://zoom.us/linux/download/pubkey?version=5-12-6
  gpg --import  pubkey?version=5-12-6
fi 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .