-3

I am writing a bash file to install applications using apt-get in Ubuntu but I don't know how to do a check if the applications are installed.

The pseudo code I'm thinking of is something like this:

string app_list = {list of applications}

for i in app_list {
    if i exists = False {
        apt-get install i    
    }
    else {}
}
Alex
  • 119
  • 6
  • 1
    Thanks to read doc before doing Cargo-cult programming. This is not bash but pseudo code – Gilles Quénot Mar 19 '18 at 00:24
  • 1
    FAQ: http://mywiki.wooledge.org/BashFAQ | Guide: http://mywiki.wooledge.org/BashGuide | Ref: http://gnu.org/s/bash/manual | http://wiki.bash-hackers.org/ | http://mywiki.wooledge.org/Quotes | Check your script: http://www.shellcheck.net/ | Mailing list: https://lists.gnu.org/mailman/listinfo/help-bash | Devel: https://git.savannah.gnu.org/cgit/bash.git?h=devel – Gilles Quénot Mar 19 '18 at 00:26
  • Thank you Gilles for the bash reference guides, I'll definitely keep them for reference. My code above was supposed to be pseudo, sorry I didn't specify that. My difficulties are not with bash but with Linux. – Alex Mar 20 '18 at 02:51

1 Answers1

0
dpkg -l "package name"
if [ $? = '0' ]
then 
    printf "Package is installed"
elif [ $? = '1' ]
then
    printf "package is not installed"
fi

Of course you will need to set this up to suite your purpose, but can use the general idea of getting the commands error code to check the installed status of the package.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
Kevin Gardenhire
  • 626
  • 8
  • 22
  • 1
    This is a simple check to see if a program is installed. He can use the return code of 1 to know that it is not installed and run his code. How does this not answer his question? – Kevin Gardenhire Mar 19 '18 at 00:25
  • Kevin fantastic, worked like a charm. I don't know what Linux executables return, like in the case of dpkg returning 0 or 1. I know I've probably seen this in all the resources I've search but if you have a link to a reference that helps me know what the standard Linux/Ubuntu applications return would be even better. – Alex Mar 20 '18 at 03:07
  • If by linux executables do you mean commands such as ls and dpkg? If so, then in general a zero return code is success, and anything else is a failure, generally a 1. The best thing to do is to search the return code for each command when you need it. Hope that helps! – Kevin Gardenhire Mar 20 '18 at 03:11
  • Thanks for the answer Kevin, I got it. – Alex Mar 20 '18 at 03:46