0

Trying to write a small script to install/update dotfiles, but I can't get this part to function properly: The array to read files to install:

APPARRAY=(curl htop ncdu pydf tree tmux vim)

And this is a function that gets called when needed. I'd like it to check if a app exists, if not install it or if it fails then log that to a logfile.

function app_installer(){
    for APP in "${APPARRAY[@]}"
    do
        # echo $APP
        #install $APP
        if command -v  $APP 2> /dev/null; then
            echo "$APP already installed!" #>> $LOG
        # if command doesnt exist, install it
         elif -x command -v  $APP 2>/dev/null ; then
            echo installing $APP #install $APP
        else
            echo "$APP FAILED TO INSTALL!!!" #>> $LOG
        fi
    done 
}
b0red
  • 47
  • 7
  • 1
    If you're just going to install it, don't bother checking for it first. Just try to install it, and catch the error (if appropriate) from your package manager. – chepner Feb 24 '19 at 22:55
  • 1
    To add onto what @chepner is saying, if you are using a package manager you can automate the install process by setting a 'yes' flag to bypass any y/n interactions and the package manages should typical provide some mechanism of handling already installed packages (for example by treating it as a re-installation or upgrade request rather than a installation request) – francium Feb 24 '19 at 22:59
  • 2
    To be more specific, most package managers will either have a non-zero exit status or do nothing if you attempt to install an already installed package. I'm not aware of any package manager that would attempt to re-install (or even upgrade) a package without being explicitly told to do so. – chepner Feb 24 '19 at 23:03
  • Yes, might try that. Guess I'm a bit tired, been staring at the screen for too many hours now. And of course I found out how to check for "not installed app" with !. Thanx guys! – b0red Feb 24 '19 at 23:03
  • @chepner Can only speak for `pacman` on arch - `warning: vim-8.1.0877-1 is up to date -- reinstalling` when running `pacman -S vim --noconfirm` (`noconfirm` skips y/n) (here `S` is the regular install this package option) – francium Feb 24 '19 at 23:06
  • @francium Ick. Is there an option to disable that? – chepner Feb 24 '19 at 23:16
  • @chepner Haven't had the need myself, but looks like it, https://superuser.com/questions/568967/prevent-pacman-from-reinstalling-packages-that-were-already-installed – francium Feb 24 '19 at 23:17

1 Answers1

1

How about this approach using which

APPARRAY=(curl htop tree tmux vim vimx)
function app_installer(){
    for APP in "${APPARRAY[@]}"
    do
        which $APP > /dev/null 2>&1
        rc=$?
        if [ $rc == 0 ]; then
            continue
        fi

        echo installing $APP
        # try and install app
        # if install fails, log to file
    done
}

app_installer

Since I have everything but vimx (I dont think vimx is a thing, I just made something up on the spot) installed, it will output,

installing vimx
francium
  • 2,384
  • 3
  • 20
  • 29