You are not logged in.

#3801 2024-12-12 06:49:32

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,796
Website

Re: Post your handy self made command line utilities

@HobbitJack is using a /bin/sh shebang, presumably to avoid the bash bloat, so they should probably read the "Case Conditional Construct" section of https://pubs.opengroup.org/onlinepubs/9 … hap02.html instead.

Bash == /bin/sh in Arch but most other distributions aren't so foolish.


Jin, Jîyan, Azadî

Offline

#3802 2024-12-12 09:04:33

HobbitJack
Member
From: Florida or Michigan
Registered: 2024-09-21
Posts: 17
Website

Re: Post your handy self made command line utilities

In this case it was mostly because this was bodged together several times (this is version 2 lol). Also something of force of Python habit, but this isn't Python. Next time I update it I'll want to merge the argument checking into a case statement, but if I recall I was just adding those ad hoc so whatever came out is what we got here.
I am at least aware of case statements, don't worry! =D
slides massive shell script monstrosities full of bloated case-esac used for data analysis under a rock

Last edited by HobbitJack (2024-12-12 09:06:58)


Astrophysicist and programmer. I like simple things.

Offline

#3803 2024-12-12 20:11:12

snakeroot
Member
Registered: 2012-10-06
Posts: 171

Re: Post your handy self made command line utilities

A seasonally-appropriate script that uses "find" to scan through FLAC files, find any with "Christmas" or "Xmas" in the file name and asks if the GENRE tag should add "Christmas" as a GENRE if it is absent.

#! /bin/bash
if [[ $# -eq 0 ]]; then
        /usr/bin/find . -regextype egrep -regex '.*(Xmas|Christmas).*\.flac' -exec $0 {} \;
else
        GENRE=$(/usr/bin/metaflac --show-tag=GENRE $1)
        if [[ ! $(echo $GENRE | /usr/bin/grep "Christmas") ]]; then
                echo "$1 does not contain Christmas in GENRE"
                read -p "Would you like to add it? [y/n]" answer
                if [[ $answer == y ]]; then
                        NEW_GENRE="$GENRE; Christmas"
                        NEW_GENRE=$(echo $NEW_GENRE | sed 's/GENRE=//')
                        echo "New GENRE is $NEW_GENRE"
                        /usr/bin/metaflac --preserve-modtime --remove-tag=GENRE --set-tag="GENRE=$NEW_GENRE" $1
                        /usr/bin/metaflac --show-tag=TITLE --show-tag=GENRE $1
                fi
        fi
fi

Offline

#3804 2025-01-12 01:12:42

HobbitJack
Member
From: Florida or Michigan
Registered: 2024-09-21
Posts: 17
Website

Re: Post your handy self made command line utilities

Another little script from me, this one to manage my ever-growing collection of 'noteson${thing}.txt'.

#!/usr/bin/sh

case $1 in
        ''|'-h'|'--help'|'help')
                echo 'Usage: note COMMAND|NOTE'
                echo 'manage notes'
                echo
                echo "Notes are saved at '~/.local/share/note/'."
                echo
                echo 'COMMAND:'
                echo '    cat NOTE        print the contents of NOTE to STDOUT'
                echo '    ls              list all notes'
                echo '    reinit          delete all notes and recreate note directory'
                echo '    rm  NOTE        delete NOTE'
                echo '    help            print this help'
                echo '    version         print version and license information'
                echo
                echo 'NOTE:'
                echo "    Open NOTE in '`basename ${EDITOR:-vi}`'"
                exit
                ;;
        '-v'|'--version'|'version')
                echo 'note v2.1.3'
                echo 'Copyright (C) 2024 Jack Renton Uteg.'
                echo 'License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.'
                echo 'This is free software: you are free to change and redistribute it.'
                echo 'There is NO WARRANTY, to the extent permitted by law.'
                echo
                echo 'Written by Jack Renton Uteg.'
                exit
                ;;
        'cat')
                if [ -z "$2" ]
                then
                        echo "note cat: no note specified"
                        exit 1
                fi
                cat ~/.local/share/note/$2
                ;;
        'ls')
                ls -1 ~/.local/share/note/
                ;;
        'reinit')
                rm -rf ~/.local/share/note/
                mkdir -p ~/.local/share/note/
                ;;
        'rm')
                if [ -z "$2" ]
                then
                        echo "note rm: no note specified"
                        exit 1
                fi
                rm ~/.local/share/note/$2
                ;;
        *)
                if [ -z "$1" ]
                then
                        echo "note: no note specified"
                        exit 1
                fi

                ${EDITOR:-vi} ~/.local/share/note/$1
esac

Exceptionally minimal, since all this is a basic wrapper for a few regular commands that you can otherwise do with a tad bit more typing. Likely not of interest to anyone but me -- but I at least did use 'case' this time lol


Astrophysicist and programmer. I like simple things.

Offline

#3805 2025-01-12 08:08:15

seth
Member
Registered: 2012-09-03
Posts: 61,477

Re: Post your handy self made command line utilities

echo "note rm: no note specified"

cd ~/.local/share/note/
select note in *; do echo "$note" && break; done

Or you look into autocompletion wink

You also likely want to quote the parameters everywhere, eg. you're testing "$2" but attempt to delete the unquoted $2

Offline

#3806 2025-01-14 04:13:15

HobbitJack
Member
From: Florida or Michigan
Registered: 2024-09-21
Posts: 17
Website

Re: Post your handy self made command line utilities

I *might* look into autocomplete, but a select interface is interactive and exactly not the kind of thing I want to deal with. I'd rather just be told I typed it wrong.

Fixed the quoting oversight, though.


Astrophysicist and programmer. I like simple things.

Offline

#3807 2025-02-10 14:14:27

EISENFELD
Member
From: Germany
Registered: 2024-03-13
Posts: 34
Website

Re: Post your handy self made command line utilities

If you are like me and always check for updates directly after boot with "yay", then you may want this automation too:
This small script creates and enables a systemd service. The service checks for updates and installs them if available with yay.

#!/bin/bash

# Prompt for the username
read -p "Enter the username: " USERNAME

SERVICE_FILE="yay-update.service"
SERVICE_PATH="/etc/systemd/system/${SERVICE_FILE}"

# Create the service file
cat <<EOF > "${SERVICE_PATH}"
[Unit]
Description=Yay System Update by user
After=network-online.target

[Service]
Type=oneshot
User=${USERNAME}
ExecStart=/bin/sh -c "sleep 5 && /usr/bin/yay --noconfirm --sudoloop"

[Install]
WantedBy=multi-user.target
EOF

# Enable and start the service
systemctl enable "${SERVICE_FILE}"
systemctl start "${SERVICE_FILE}"

echo "Service ${SERVICE_FILE} has been installed, enabled, and started for the user: ${USERNAME}."

If you get an error like "can't resolve host...", you have do change "sleep 5" to let's say "8" or "sleep 10".
It depends on how fast your internet connection is available. For my connection over cable It must be "5" or more.
The script must be executed as root of course.
Check the status of the new service with "systemctl status yay-update.service".


Ich weiß, dass ich nichts weiß !

Offline

#3808 2025-02-10 15:02:42

seth
Member
Registered: 2012-09-03
Posts: 61,477

Re: Post your handy self made command line utilities

If you get an error like "can't resolve host...", you have do change "sleep 5" to let's say "8" or "sleep 10".

https://bbs.archlinux.org/viewtopic.php … 1#p2090691

The script must be executed as root of course.

system services/timers run as root.

PSA: unattended updates (leaving alone with yay) are not a very good idea, you want to log that somewhere and present yourself that log after the session started.

Offline

#3809 2025-02-16 07:02:08

EISENFELD
Member
From: Germany
Registered: 2024-03-13
Posts: 34
Website

Re: Post your handy self made command line utilities

This Script will update with "yay" and informs the user about the progress with desktop notifications.
After that it completely cleans the Pacman / Yay cache and use logrotate on pacman.log file.
Every action is logged in /tmp/yay_update.log

You can start this script everytime you boot with a systemd service.
Here is my /usr/local/bin/yay_update.sh file.
Don't forget to make it executable (chmod +x yay_update.sh)
Maybe you have to adjust "sleep 5". My cable connection is ready in 5 seconds.

#!/bin/bash
# File: /usr/local/bin/yay-update.sh

LOG="/tmp/yay_update.log"

sleep 5 && ID=$(notify-send -p -t 1000 -u normal -a Yay --hint=string:sound-name:service-login 'Yay Update' 'System update started ...')

# Trim log file to prevent infinite growth
tail -n 100 $LOG > /tmp/yay_update.tmp && mv /tmp/yay_update.tmp $LOG

echo "$(date): Starting system update..." >> $LOG

# Perform system update
if yay --noconfirm --sudoloop --noprogressbar --needed 2>&1 | tee -a $LOG; then
    if grep -q "Error" /tmp/yay_update.log; then
        beep -f 2000 
        notify-send -r $ID -t 5000 -u critical -a Yay -i /usr/share/icons/Adwaita/symbolic/status/computer-fail-symbolic.svg --hint=string:sound-name:dialog-error 'Yay Update' 'Error during update! See /tmp/yay_update.log'
        exit 1
    fi
    beep -f 100
else
    beep -f 2000 
    notify-send -r $ID -t 5000 -u critical -a Yay -i /usr/share/icons/Adwaita/symbolic/status/computer-fail-symbolic.svg --hint=string:sound-name:dialog-error 'Yay Update' 'Error during update! See /tmp/yay_update.log'
    exit 1
fi

# Check if no update was necessary
if grep -q "there is nothing to do" $LOG; then
    beep -f 500
    notify-send -r $ID -t 1000 -u normal -a Yay -i /usr/share/icons/Adwaita/symbolic/categories/emoji-body-symbolic.svg --hint=string:sound-name:dialog-information 'Yay Update' 'No updates available!'
    echo "No cache cleanup and logrotate needed, as no update was performed!" >> $LOG
    exit 0
fi

# Clean cache
echo "- Cleaning Yay and Pacman cache ..." >> $LOG
yay -Sc --noconfirm 2>&1 | tee -a $LOG
sudo rm -rf /var/cache/pacman/pkg/ 2>&1 | tee -a $LOG
echo "Cache completely cleaned." >> $LOG

# Apply logrotate to pacman.log
echo "- Starting logrotate for /var/log/pacman.log" >> $LOG
if ! sudo logrotate -f -v /etc/logrotate.d/pacman 2>&1 | tee -a $LOG; then
    beep -f 2000
    notify-send -t 5000 -u critical -a Yay --hint=string:sound-name:dialog-error 'Yay Update' 'Error during logrotate!'
fi

echo "Done! Everything completed." >> $LOG
beep -f 500
notify-send -r $ID -t 1000 -u normal -a Yay -i /usr/share/icons/Adwaita/symbolic/categories/emoji-body-symbolic.svg --hint=string:sound-name:complete 'Yay Update' 'New updates installed!'

Now everything is logged in /tmp/yay_update.log
This script works very well under Gnome desktop/Wayland.

Here ist my yay-update.service file /etc/systemd/system/yay-update.service (Gnome/Wayland):
Don't forget to replace "User=lennart" with your username.

[Unit]
Description=Yay System Update by user
After=network-online.target
Wants=network-online.target

[Service]
TimeoutStartSec=infinity
Type=oneshot
User=lennart
Environment=DISPLAY=:0 WAYLAND_DISPLAY=wayland-0 XDG_RUNTIME_DIR=/run/user/1000
ExecStart=/usr/local/bin/yay-update.sh

[Install]
WantedBy=multi-user.target

Here is the configuration file for logrotate: /etc/logrotate.d/pacman:

/var/log/pacman.log {
 weekly
 rotate 4
 compress
 missingok
 notifempty
 create 644 root root
}

Install the service with:

sudo systemctl enable yay-update.service

Test the service with:

sudo systemctl start yay-update.service

As I wrote before, It works very well with Gnome / Wayland. For other systems, you have to adjust it.
btw. "beep", "logrotate" and "notify-send" must be installed

yay -S beep logrotate notify-send

Regards,
EISENFELD

Last edited by EISENFELD (2025-02-16 11:19:33)


Ich weiß, dass ich nichts weiß !

Offline

#3810 2025-02-16 08:38:47

seth
Member
Registered: 2012-09-03
Posts: 61,477

Re: Post your handy self made command line utilities

As discussed in the linked thread, the network-online.target typically does not do what you'd expect.
Wait until you've an icmp response from a relevant peer.

yay -Scc --noconfirm | tee -a /tmp/yay_update.log

This will drop stderr, otoh you're probably not interested in a copy of stdout?

… >> /tmp/yay_update.log 2>&1

Offline

#3811 2025-02-16 11:21:12

EISENFELD
Member
From: Germany
Registered: 2024-03-13
Posts: 34
Website

Re: Post your handy self made command line utilities

I think "yay --noconfirm --sudoloop --noprogressbar --needed 2>&1 | tee -a $LOG" is the best solution.
I updated the code in my post.


Ich weiß, dass ich nichts weiß !

Offline

#3812 2025-02-25 00:37:23

NuSkool
Member
Registered: 2015-03-23
Posts: 258

Re: Post your handy self made command line utilities

yay --noconfirm --sudoloop --noprogressbar --needed 2>&1 | tee -a $LOG

It might be worth pointing out that the following IIRC, would be an alternative equivalent.

 yay --noconfirm --sudoloop --noprogressbar --needed |& tee -a $LOG

  I commonly use '|&' to pipe both 'stdout' and 'stderr'.


Scripts I use: https://github.com/Cody-Learner
$ glxinfo | grep Device                                Device: AMD Radeon Vega 11 Graphics (radeonsi, raven, LLVM 19.1.7, DRM 3.60, 6.13.3-arch1-1.1) (0x15dd)
$ sudo dmesg | awk '/drm/ && /gfx/'       [    6.427009] [drm] add ip block number 6 <gfx_v9_0>

Offline

#3813 2025-02-25 05:36:28

EISENFELD
Member
From: Germany
Registered: 2024-03-13
Posts: 34
Website

Re: Post your handy self made command line utilities

If someone is interested. Here is the final (so far) version with install script:
It works very well on my system, I didn't test it on others. There are also some nice screenshots:
https://github.com/lennart1978/Yay-autoupdate

Last edited by EISENFELD (2025-02-25 05:38:34)


Ich weiß, dass ich nichts weiß !

Offline

#3814 Today 09:15:43

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,427

Re: Post your handy self made command line utilities

Just got a Rival SteelSeries 3 mouse with leds on it to be used on an always on PC.
To take care of leds, this one turns them off after 15miin inactivity and back on as soon as an input is registered; going to make a simple systemd service out of it:

while true ; do  
	if timeout 900 inotifywait /dev/input/event* ; then 
		c1="08080b" ; c2=$c1 ; 
		rivalcfg  --light-effect steady --logo-color $c1 --strip-top-color $c2 --strip-middle-color $c2 --strip-bottom-color $c2
	else 
		c1="000" ; c2=$c1 ; 
		rivalcfg  --light-effect steady --logo-color $c1 --strip-top-color $c2 --strip-middle-color $c2 --strip-bottom-color $c2
	fi
done

Now if I could find a scriptable way to turn off leds on the K70 core rgb, it would be great.

-edit-
seems i can leverage openlinkhub for that.

Last edited by kokoko3k (Today 12:35:41)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#3815 Today 16:41:06

seth
Member
Registered: 2012-09-03
Posts: 61,477

Re: Post your handy self made command line utilities

Since there seem to be ongoing hurdles with receiving distro news, this prints the ones for the current and previous month in a console-friendly way.

#!/bin/sh
export LC_ALL=C
THIS_MONTH="$(date +'%b %Y')"
LAST_MONTH="$(date +'%b %Y' -d -1month)"
curl -sL 'https://archlinux.org/feeds/news/' | tr '\n' ' ' | \
xmlstarlet sel -T -t -m "//rss/channel/item[contains(string(pubDate), '$LAST_MONTH') or contains(string(pubDate), '$THIS_MONTH')]" \
                        -o $'\n\e[33m' -v pubDate -o $'\n\e[0;1m' -v title -o $'\e[0m' -v description -o $'\n\n────────────────\n' |\
sed $'s/<p>/\\n\\n/g; s/<h.>/\\n\\n\e[1;34m/g; s%</\(h.\|b\|em\|strong\|pre\|code\)>%\e[0m%g;
    s/<li>/\\n· /g;
    s/<\(code\|pre\)>/\e[1;32m/g;
    s/<\(strong\|em\)>/\e[1m/g;
    s/&gt;/>/g; s/&lt;/</g; s/<[^>]*>//g' | fold -sw 100

Offline

Board footer

Powered by FluxBB