You are not logged in.
I am writing a script for my use to install Arch Linux.
Suppose I have:
PKGS=(
'firefox'
'libreoffice'
'vlc'
'***'
)
I would like the script to validate my packages ensuring there is no typo and ensuring none of the packages was removed from the repository.
I am thinking of something around:
for packages in "${PKGS[@]}"; do
Validate all the packages one by one
done
if packages validated successfully
echo we are good!
else
echo package names that failed validation e.g. firegox ginp
exit and stop running the script
fi
How can I achieve this please?
Offline
Is this really necessary? Pacman errors out if any of the packages you tell it to install cannot be found:
$ sudo pacman -S vnc hohoho
error: target not found: hohoho
But if you really need it, you can just check the return code of pacman -Si:
found_all=true
for p in "${PKGS[@]}"; do
if ! pacman -Si "$p" >/dev/null; then
found_all=false
fi
done
if $found_all; then
echo all good
else
# pacman -Si above already prints all packages it could not find
exit 1
fi
(this is probably also rather a programming&scripting question than an installation question)
{,META,RE}PKGBUILDS │ pacman-hacks (includes makemetapkg and remakepkg) │ dotfileslocaldir
Offline
Thank you for your answer.
Well, if there was a typo or package not found error it breaks the installation half-way through and then I have to manually finish the installation to correct the missing steps.
I would rather ensure this is not going to happen so that all the installation will finish correctly.
I tried your commands above and strangly: I got the below:
error: package 'base-devel' was not found
error: package 'xorg' was not found
error: package 'dnsutils' was not found
error: package 'qemu' was not found
error: package 'xorg-apps' was not found
error: package 'xorg-drivers' was not found
Any idea how to avoid this?
NB I appreciate what you said. I think my question is actually more related to scripting. Apologies.
Offline
Well, if there was a typo or package not found error it breaks the installation half-way through and then I have to manually finish the installation to correct the missing steps.
Or just fix the package list and rerun the script, that would be easier.
And if your script does not allow that (i.e. if it is not idempotent yet), I would focus on fixing that.
I would rather ensure this is not going to happen so that all the installation will finish correctly.
That's a case of TOCTOU: Who guarantees that the packages that exist while you're checking will also be there while you're installing?
It's saner not to assume that you can check everything upfront and guarantee that something will always work. What about network interruptions? Or power loss? Or other hardware/software failures?
It's cleaner to properly handle error cases the moment they (inevitably) happen.
___
If you still want to pursue the current approach of separately checking the package existence upfront: Some of them are package groups, others are "virtual" package names provided by another package, and `pacman -Si` does indeed not show anything for them. I hadn't thought of that, sorry.
There's a way to determine group existence with -Sg, but AFAIK there's nothing for provides (at least in pacman itself), so if you want to extend it to include groups and provides, this needs a bit more effort.
In this case, I'm using expac and grep, but I guess you could also just parse the package DB manually if you prefer:
…
if ! expac -S ' %n %P ' | grep -q " $p " >/dev/null && ! pacman -Sg "$p" >/dev/null; then
printf "Package or group '%s' was not found\\n" "$p" >&2
found_all=false
fi
…
That being said, it makes absolutely no sense to install all of xorg or xorg-drivers.
{,META,RE}PKGBUILDS │ pacman-hacks (includes makemetapkg and remakepkg) │ dotfileslocaldir
Offline
I tried your commands above and strangly: I got the below:
error: package 'base-devel' was not found error: package 'xorg' was not found error: package 'dnsutils' was not found error: package 'qemu' was not found error: package 'xorg-apps' was not found error: package 'xorg-drivers' was not found
Any idea how to avoid this?
That's because none of those are actual packages, they're the names of package groups or virtual packages. The script is working exactly how you asked
Also you really shouldn't be installing things such as the xorg-drivers group as having all of the drivers installed can lead to conflicts.
Offline
OK thank you very much for the really well explained answer.
I will create a seperate script for the packages where I will validate them first using your script above before I install the system.
I will then creat a package list from that and add it to the install script and run it.
I will also look into xorg and xorg-drivers etc to figure out which ones I actually need.
The problem might be that I might not understand what each specific package does.
Offline
If you want to check, do not use -Si as suggested above as it will fail with groups (as you have seen) and it is needlessly complex and requires the loop. You can use -Sp which does not require root permissions and handles groups properly but will not actually do anything other than report errors or give urls:
pacman -Sp ${PKGS[@]} >/dev/null || error_out_here
"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" - Richard Stallman
Offline
Oh, TIL about -p. I retract my previous suggestions.
{,META,RE}PKGBUILDS │ pacman-hacks (includes makemetapkg and remakepkg) │ dotfileslocaldir
Offline
pacman -Sp ${PKGS[@]} >/dev/null || error_out_here
I did not get how would this work with @ayekat's commands. Could you please explain further? Replacing Si with Sp does not seem to work.
Offline
I'm not suggesting using it with any other commands. That one command replaces the entire script(s). To put it in the context of your original script:
pacman -Sp ${PKGS[@]} >/dev/null || exit
echo we are good!
Last edited by Trilby (2022-12-03 22:42:48)
"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" - Richard Stallman
Offline
I will also look into xorg and xorg-drivers etc to figure out which ones I actually need.
The problem might be that I might not understand what each specific package does.
The wiki tells you how to determine this based on your hardware.
CLI Paste | How To Ask Questions
Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L
Offline