You are not logged in.

#1 2020-11-15 23:06:01

MountainX
Member
Registered: 2016-02-08
Posts: 371

[SOLVED] in script, check if latest package version is installed

I run my system updates (pacman -Syu) with a script that does a number of routine checks for me (both before and after the actual update). I like to run all these checks as part of any update.

I decided to make a proper Arch package for my script. (It seems maybe this was not a good idea, but I am still trying to make it work.) Previously, I would do a git pull before every system update to ensure I am using the latest version of my updater script. (The script updated itself, prompted to re-run it & exited if I was not running the latest version.) Now that it is a package, I could do this within my script so it will update itself:

pacman -S --needed --noconfirm mypackage 

However, I need to exit and restart my script if it was updated. The return code is 0 in both the case where it was updated as well as in the case where it did not need to be updated. Is there a better solution?

Last edited by MountainX (2020-11-15 23:30:23)

Offline

#2 2020-11-15 23:14:22

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED] in script, check if latest package version is installed

You do realize that that command should never udpate the package, it will just reinstall the currently installed version (the only exception to this is if you've previously done dangerous things that you shouldn't, and in that case this command would be a partial upgrade).

But for a direct answer, you could add the "--print" flag to the pacman command and check if it generates any output.  If the package in question is up to date (which it should always be in that command) then this will generate no ouput.  If it is not up to date, it will print a url.

Last edited by Trilby (2020-11-15 23:16:35)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2020-11-15 23:17:02

MountainX
Member
Registered: 2016-02-08
Posts: 371

Re: [SOLVED] in script, check if latest package version is installed

Trilby wrote:

You do realize that that command should never udpate the package, it will just reinstall the currently installed version (the only exception to this is if you've previously done dangerous things that you shouldn't, and in that case this command would be a partial upgrade).

This is my own package. It has no dependencies. It's a simple bash script. The command does cause an update because I use pacsync on my private repo before running it.

I guess I could do something like this to get it to work like it used to:

pacsync myrepo
iscurrent=$(pacman -S --needed --noconfirm mypackage 2>&1)
echo $iscurrent | grep 'there is nothing to do' 
if [[ $? -ne 0 ]]; then
    echo "This script has been updated. Restart this script."
    exit 1
fi

Also, the next steps after this are to run a full system update, so even if I ran "pacman -Sy" I don't think it would be a problem because a "pacman -Syu" is following very quickly.

EDIT: I see you added info about the "--print" option. I'll check that out. Thanks.

EDIT 2: the "--print" flag doesn't seem to act the way you described for my local repo. Currently the package is up to date. But I still get a file url as output:

pacman -Sp mypackage
file:///var/cache/pacman/pkg/mypackage-1.0.r1-1-any.pkg.tar.zst

Last edited by MountainX (2020-11-15 23:26:40)

Offline

#4 2020-11-15 23:27:02

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED] in script, check if latest package version is installed

I said ADD the print flag, not replace everything else with it.  You removed the --needed flag so it will print the file url whether or not an update is needed.  If you put --needed and --print it will do as I described, but this is better anyhow:

ver=$(pacman -S --print-format %v mypackage)
if [ ! -d var/lib/pacman/local/mypackage-$ver ]; then
   pacman -S mypackage
   exec "$0" "$@"
fi

Last edited by Trilby (2020-11-15 23:29:19)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2020-11-15 23:29:04

MountainX
Member
Registered: 2016-02-08
Posts: 371

Re: [SOLVED] in script, check if latest package version is installed

Trilby wrote:

I said ADD the print flag, not replace everything else with it.  You removed the --needed flag so it will print the file url whether or not an update is needed.  If you put --needed and --print it will do as I described, but this is better anyhow:

ver=$(pacman -S --print-format %v mypackage)
if [ ! -d var/lib/pacman/local/mypackage-$ver ]; then
   # install stuff here
fi

Thanks!

Offline

#6 2020-11-15 23:33:34

apg
Developer
Registered: 2012-11-10
Posts: 211

Re: [SOLVED] in script, check if latest package version is installed

pacsync $myrepo
if pacman -Qu $mypackage >/dev/null; then
    pacman -S $mypackage
    exec $0
fi

Offline

#7 2020-11-15 23:49:18

MountainX
Member
Registered: 2016-02-08
Posts: 371

Re: [SOLVED] in script, check if latest package version is installed

apg wrote:
pacsync $myrepo
if pacman -Qu $mypackage >/dev/null; then
    pacman -S $mypackage
    exec $0
fi

That looks like a nice approach, but I don't understand one line.

man exec:

The exec utility shall open, close, and/or copy file descriptors as specified by any redirections as part of the command.

I don't understand what the "exec $0" line is doing. EDIT: I guess it is just a cleaner way of exiting, right?

Last edited by MountainX (2020-11-15 23:50:23)

Offline

#8 2020-11-16 00:00:47

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: [SOLVED] in script, check if latest package version is installed

MountainX wrote:

don't understand what the "exec $0" line is doing. EDIT: I guess it is just a cleaner way of exiting, right?

Typically $0 is the running script/binary, idk the specific naming convention...iirc is something related to argv0.

So when you do 'exec "$0"', you re-run the script, if you do 'exec "$0" "$@"', you re-run the script with the same args.

...

Why tho? I'm confused...
Oh I get it, so you use the latest version instead.

Last edited by GaKu999 (2020-11-16 00:02:07)


My reposSome snippets

Heisenberg might have been here.

Offline

#9 2020-11-16 00:09:00

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED] in script, check if latest package version is installed

apg's version is much nicer.  I have no experience with -Qu as that is meaningless except in the context of an upgraded sync db but not upgraded packages - a situation I am never in.

As for exec, it is an unfortunately named command that has two completely different purposes: one for opening, closing, or redirecting file descriptors (equivalent to the system calls open, close, and dup2); the other purpose is actually to exec (equivent to the system call exec) which replaces the currently running process with a new one (see `man 3 exec`).

Last edited by Trilby (2020-11-16 00:15:20)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2020-11-16 00:10:08

MountainX
Member
Registered: 2016-02-08
Posts: 371

Re: [SOLVED] in script, check if latest package version is installed

GaKu999 wrote:

if you do 'exec "$0" "$@"', you re-run the script with the same args.

Thank you. Now I understand.

Offline

Board footer

Powered by FluxBB