Thanks @rvp
It's not necessarily the method of output that I'm concerned with (see the full script here for reference and feel free to point out any better ways of doing things there too).
I was more wanting to know if the use of pkg_chk
was the best option here? It takes a a little while to run but I suppose that's to be expected.
I thought about running a cronjob to intermittently check for updates but that would be pointmless unless I'd updated my pkgsrc tree. so I came up with this:
chupdate.sh
:
#!/bin/sh
set -e
# Check for an existing pkgsrc directory and run cvs update
# If no pkgsrc found then fetch and extract in /usr
if [ -d /usr/pkgsrc ]; then
echo "Running cvs update, this could take a minute..." ;
doas su bob -c "cd /usr/pkgsrc && cvs update -dP"
else
echo "No pkgsrc directory found in /usr, fetching latest current tarball" ;
echo "This might take a few minutes..." ;
ftp ftp://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc.tar.gz ;
doas tar -xzf pkgsrc.tar.gz -C /usr ;
doas chown -R bob /usr/pkgsrc # Bob the Builder...
fi
# Check for an existing pkgsrc/wip directory and update
# If not found then clone into pkgsrc directory
if [ -d /usr/pkgsrc/wip ]; then
echo "Updating pkgsrc/wip..." ;
doas su bob -c "cd /usr/pkgsrc/wip && git pull -r"
else
echo "Cloning pkgsrc-wip to /usr/pkgsrc/wip" ;
doas su bob -c "git clone git://wip.pkgsrc.org/pkgsrc-wip.git /usr/pkgsrc/wip ;
git config --global --add safe.directory /usr/pkgsrc/wip"
fi
# Show number of available updates in bar using pkg_chk
if [ -f ~/.config/sdorfehs/updates.txt ]; then
rm ~/.config/sdorfehs/updates.txt
fi
echo "Checking for available updates..."
pkg_chk -u -q | wc -l > ~/.config/sdorfehs/updates.txt
No AI was involved in making this script, just my little pea brain. Please let me know how to make this better 🤗
And obviously it's a little quirky because my user bob
is the owner and builder so I've had to switch users inside the script.
EDIT
Ok, I couldn't help myself, I asked copilot how to improve the script and now I have this:
#!/bin/sh
set -e
# Define paths
PKGDIR="/usr/pkgsrc"
WIPDIR="/usr/pkgsrc/wip"
UPDATES_FILE="$HOME/.config/sdorfehs/updates.txt"
# Function to update pkgsrc
update_pkgsrc() {
if [ -d "$PKGDIR" ]; then
echo "Running cvs update, this could take a minute..."
cd $PKGDIR
doas -u bob cvs update -dP
else
echo "No pkgsrc directory found in $PKGDIR, fetching latest current tarball..."
ftp ftp://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc.tar.gz
doas tar -xzf pkgsrc.tar.gz -C $PKGDIR
doas chown -R bob $PKGDIR
fi
}
# Function to update pkgsrc/wip
update_wip() {
if [ -d "$WIPDIR" ]; then
echo "Updating pkgsrc/wip..."
cd $WIPDIR
doas -u bob git pull -r
else
echo "Cloning pkgsrc-wip to $WIPDIR..."
doas -u bob git clone git://wip.pkgsrc.org/pkgsrc-wip.git $WIPDIR
doas git config --global --add safe.directory $WIPDIR
fi
}
# Update pkgsrc and wip
update_pkgsrc
update_wip
# Calculate available updates and show number in status bar
if [ -f "$UPDATES_FILE" ]; then
rm "$UPDATES_FILE"
fi
echo "Checking for available updates..."
available_updates=$(pkgchkxx -u -q | wc -l)
echo "Number of available updates: $available_updates"
echo "$available_updates" > "$UPDATES_FILE"
thoughts?
disclaimer:
As we all know by now, the I scripts conjure up are generally pointless or unnecessary utterly useless, for me and especially for others. I do this because it's fun and I enjoy it, and in general I have nothing better to do 😘