You are not logged in.

#1 2013-10-03 18:04:10

0X1A
Member
Registered: 2013-03-27
Posts: 43

[SOLVED] Check for Dependencies

Hey, I'm trying to write a script that'll check for dependencies to install and build something. I can check to see if the file exists but that seems kind of brute force. I'm trying to use

pacman -Q

but I can't get it quite right.

Example:

DEPS="python2-setuptools git curl wget"

if ( sudo pacman -Q $DEPS >/dev/null ) ; then
    echo "Deps not installed, installing now"
    sudo pacman -S $DEPS
else
    echo "Dependencies met."
fi

But this always returns that DEPS aren't installed and prompts to reinstall.

Last edited by 0X1A (2013-10-03 18:55:03)

Offline

#2 2013-10-03 18:06:33

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] Check for Dependencies

0X1A wrote:
if ( sudo pacman -Q $DEPS >/dev/null ) ; then

Are you doing this in bash? Use [[

Also, you don't need sudo for -Q


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2013-10-03 18:10:43

0X1A
Member
Registered: 2013-03-27
Posts: 43

Re: [SOLVED] Check for Dependencies

jasonwryan wrote:
0X1A wrote:
if ( sudo pacman -Q $DEPS >/dev/null ) ; then

Are you doing this in bash? Use [[

Also, you don't need sudo for -Q

Yeah I am, using [[ ]] expects a conditional binary operator

Offline

#4 2013-10-03 18:12:06

XURL
Member
Registered: 2013-09-26
Posts: 24

Re: [SOLVED] Check for Dependencies

Use -T switch.

pacman -T git python2-setuptools amarok
python2-setuptools
amarok

P.S. git is the only application I've installed. The rest are being listed as "targets".

Last edited by XURL (2013-10-03 18:13:03)


☑ CPU: Single core Intel Pentium M (-UP-) clocked at 1733.000 Mhz
☑ MEM: 490.1 MB
☑ HDD: 40.1 GB

Offline

#5 2013-10-03 18:18:05

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: [SOLVED] Check for Dependencies

Direct fix of your code would look like this:

#!/bin/bash

DEPS="python2-setuptools git curl wget"
pacman -Q $DEPS &> /dev/null
if [[ $? -ne 0 ]] ; then
    echo "Deps not installed, installing now"
    sudo pacman -S $DEPS
else
    echo "Dependencies met."
fi

, but as pointed above, it's not the best approach, what exactly are you trying to accomplish?

Last edited by kaszak696 (2013-10-03 18:26:40)


'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard

Offline

#6 2013-10-03 18:28:44

0X1A
Member
Registered: 2013-03-27
Posts: 43

Re: [SOLVED] Check for Dependencies

kaszak696 wrote:

Direct fix of your code would look like this:

#!/bin/bash

DEPS="python2-setuptools git curl wget"
pacman -Q $DEPS &> /dev/null
if [ $? -ne 0 ] ; then
    echo "Deps not installed, installing now"
    sudo pacman -S $DEPS
else
    echo "Dependencies met."
fi

, but as pointed above, it's not the best approach, what exactly are you trying to accomplish?

This works, but still prompts for re-installation. I'm just trying to see if the user has those packages installed and if not then install them.

Offline

#7 2013-10-03 18:32:46

XURL
Member
Registered: 2013-09-26
Posts: 24

Re: [SOLVED] Check for Dependencies

#!/bin/bash

DEPS="python2-setuptools git wget"

# Get missing dependencies
targets=$(pacman -T $DEPS)

# Install missing dependencies
sudo pacman -S $targets

Last edited by XURL (2013-10-03 18:33:45)


☑ CPU: Single core Intel Pentium M (-UP-) clocked at 1733.000 Mhz
☑ MEM: 490.1 MB
☑ HDD: 40.1 GB

Offline

#8 2013-10-03 18:32:47

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: [SOLVED] Check for Dependencies

0X1A wrote:

This works, but still prompts for re-installation. I'm just trying to see if the user has those packages installed and if not then install them.

Pacman has a very nifty switch --needed, that takes case of that. Or use XURL's code.

Last edited by kaszak696 (2013-10-03 18:33:28)


'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard

Offline

#9 2013-10-03 18:54:48

0X1A
Member
Registered: 2013-03-27
Posts: 43

Re: [SOLVED] Check for Dependencies

XURL wrote:
#!/bin/bash

DEPS="python2-setuptools git wget"

# Get missing dependencies
targets=$(pacman -T $DEPS)

# Install missing dependencies
sudo pacman -S $targets

This works perfectly, thanks

Offline

#10 2013-10-03 19:09:30

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,787
Website

Re: [SOLVED] Check for Dependencies

kaszak696 wrote:
...
    echo "Deps not installed, installing now"
    sudo pacman -S $DEPS
...

I'd recommend putting an --asdeps in that sync statement, or else $USER will have a lot of packages that he/she doesn't need/want later on when they remove $FOO.


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#11 2013-10-03 19:44:20

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] Check for Dependencies

kaszak696 wrote:

Direct fix of your code would look like this:

#!/bin/bash

DEPS="python2-setuptools git curl wget"
pacman -Q $DEPS &> /dev/null
if [[ $? -ne 0 ]] ; then
    echo "Deps not installed, installing now"
    sudo pacman -S $DEPS
else
    echo "Dependencies met."
fi

, but as pointed above, it's not the best approach, what exactly are you trying to accomplish?

You could simply have written

if ! pacman -Q $DEPS &> /dev/null; then

Offline

Board footer

Powered by FluxBB