You are not logged in.

#3626 2021-10-19 22:31:36

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,344

Re: Post your handy self made command line utilities

Ow shit, yes, I didn't even think of that, thanks;-)

Offline

#3627 2021-10-24 22:20:14

zpg443
Member
Registered: 2016-12-03
Posts: 231

Re: Post your handy self made command line utilities

FWIW, The musicbase bash script has been completed. Thanks for the feedback.

If you use kid3-qt, it may be helpful. The utility allows you to customize headers and format codes to build a database from the console with one go, a feature kid3-cli was lacking.

https://github.com/Harpo3/musicbase

Last edited by zpg443 (2021-10-25 17:49:21)

Offline

#3628 2021-10-30 03:11:41

zpg443
Member
Registered: 2016-12-03
Posts: 231

Re: Post your handy self made command line utilities

Continuing with music library utilities...

addplayedframe.sh

A kid3-based utility to add a custom TXXX frame for writing LastPlayedDate data directly to tags.
Generates and applies a random LastPlayedDate for each tag, with TIMEVAL either sql or epoch time.

Usage: addplayedframe.sh [option] DIRPATH TIMEVAL

This utility may be of use for those writing scripts to build smart playlists. The lastplayed data could be used, together with POPM rating and artist fields, to script playlists. Working on more along these lines to simplify the process for kid3 users with large libraries.

https://github.com/Harpo3/addplayedframe

removebypopm.sh

A musicbase utility to generate filtered output based on POPM rating values

Uses awk to filter music library tracks by POPM field when POPM is
included in the input database. Output is a data-separated-values
(DSV) file with carat "^" (or other specified) delimiter.

https://github.com/Harpo3/removebypopm

playedagefilter.sh

A musicbase utility to generate filtered output based on last played values

Uses awk to filter rated tracks FILE based on last-played-date and outputs to a database file of
not-recently-played tracks, which can then be used by other tools for playlist creation. Used
when POPM and last-played data are included in the input database.

https://github.com/Harpo3/playedagefilter

Last edited by zpg443 (2021-10-31 18:33:51)

Offline

#3629 2021-11-19 12:51:46

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,270
Website

Re: Post your handy self made command line utilities

kdiff.py - Check whether your running kernel is equal to the installed kernel.

#! /usr/bin/env python3
"""Check whether the running kernel equals the installed kernel package."""

from argparse import ArgumentParser, Namespace
from logging import INFO, WARNING, basicConfig, getLogger
from subprocess import check_output
from sys import exit    # pylint: disable=W0622
from typing import NamedTuple, Optional


__all__ = ['Kernel', 'get_running_kernel', 'get_installed_kernel', 'main']


LOGGER = getLogger('kdiff')


class Kernel(NamedTuple):
    """Representation of kernel flavor and version."""

    major: int
    minor: int
    micro: int
    revision: str
    patch: Optional[str] = None
    flavor: Optional[str] = None


class Suffix(NamedTuple):
    """Representation of kernel version suffixes."""

    revision: str
    patch: Optional[str] = None
    flavor: Optional[str] = None


def get_suffix(suffix: list[str]) -> Suffix:
    """Returns revision, patch and flavor."""

    try:
        patch, revision, flavor = suffix
    except ValueError:
        revision, flavor = suffix
        patch = None

        if revision.startswith('arch'):
            revision, patch, flavor = flavor, revision, None

    return Suffix(revision, patch, flavor)


def get_running_kernel() -> Kernel:
    """Returns the running kernel."""

    text = check_output(['/usr/bin/uname', '-r'], text=True)
    version, *suffix = text.strip().split('-')
    return Kernel(*map(int, version.split('.')), *get_suffix(suffix))


def get_installed_kernel(flavor: Optional[str] = None) -> Kernel:
    """Returns the installed kernel."""

    pkg = 'linux' if flavor is None else f'linux-{flavor}'
    text = check_output(['/usr/bin/pacman', '-Q', pkg], text=True)
    _, version = text.strip().split()
    version, revision = version.split('-')
    major, minor, micro, *patch = version.split('.')
    patch = '.'.join(patch) or None
    return Kernel(int(major), int(minor), int(micro), revision, patch, flavor)


def get_args(*, description: str = __doc__) -> Namespace:
    """Parses the command line arguments."""

    parser = ArgumentParser(description=description)
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='enable verbose output')
    return parser.parse_args()


def main() -> int:
    """Returns 0 if running and installed kernel equal else 1."""

    args = get_args()
    basicConfig(level=INFO if args.verbose else WARNING)
    running_kernel = get_running_kernel()
    LOGGER.info('Running kernel: %s', running_kernel)
    installed_kernel = get_installed_kernel(running_kernel.flavor)
    LOGGER.info('Installed kernel: %s', installed_kernel)
    return int(running_kernel != installed_kernel)


if __name__ == '__main__':
    exit(main())

Offline

#3630 2021-11-19 14:39:37

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,236
Website

Re: Post your handy self made command line utilities

In bash:

[[ $(uname -r | cut -d- -f3 --complement) == "$(pacman -Q "$1" | cut -d' ' -f2)" ]]

Last edited by Alad (2021-11-19 14:40:13)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#3631 2021-11-19 14:56:26

seth
Member
Registered: 2012-09-03
Posts: 25,107

Re: Post your handy self made command line utilities

Flavor…

pacman -Qs linux | grep "$(uname -r | sed 's/\([^-]*-[^-]*\)-\(.*\)/linux-\2 \1/g')"

Online

#3632 2021-11-19 16:00:06

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,270
Website

Re: Post your handy self made command line utilities

@Alad, @seth
Both your solutions do not work with either linux, linux-lts or linux-zen.

0 ✓ neumann@ThinkCentre ~ $ uname -r; pacman -Q linux
5.15.2-arch1-1
linux 5.15.2.arch1-1
0 ✓ neumann@ThinkCentre ~ $ pacman -Qs linux | grep "$(uname -r | sed 's/\([^-]*-[^-]*\)-\(.*\)/linux-\2 \1/g')"
1 ✗ neumann@ThinkCentre ~ $ ./kdiff.py 
0 ✓ neumann@ThinkCentre ~ $ 
0 ✓ srv ~ $ uname -r; pacman -Q linux-lts
5.10.79-1-lts
linux-lts 5.10.79-1
0 ✓ srv ~ $ pacman -Qs linux | grep "$(uname -r | sed 's/\([^-]*-[^-]*\)-\(.*\)/linux-\2 \1/g')"
local/linux-lts 5.10.79-1
0 ✓ srv ~ $ ./kdiff.py 
0 ✓ srv ~ $ 
0 ✓ gerd ~ $ uname -r; pacman -Q linux-zen
5.15.2-zen1-1-zen
linux-zen 5.15.2.zen1-1
0 ✓ gerd ~ $ pacman -Qs linux | grep "$(uname -r | sed 's/\([^-]*-[^-]*\)-\(.*\)/linux-\2 \1/g')"
.1 ✗ gerd ~ $ ./kdiff.py 
0 ✓ gerd ~ $ 

Last edited by schard (2021-11-19 16:05:16)

Offline

#3633 2021-11-19 16:05:54

seth
Member
Registered: 2012-09-03
Posts: 25,107

Re: Post your handy self made command line utilities

Correctshun:

pacman -Qs linux | grep "$(uname -r | sed 's/\([^-]*-[^-]*\)\(-.*\)/linux\2 \1/g')"

Edit: 'key the zen suffix needs more sophistication.

Last edited by seth (2021-11-19 16:07:21)

Online

#3634 2021-11-19 16:08:05

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,270
Website

Re: Post your handy self made command line utilities

seth wrote:

Correctshun:

pacman -Qs linux | grep "$(uname -r | sed 's/\([^-]*-[^-]*\)\(-.*\)/linux\2 \1/g')"

Edit: 'key the zen suffix needs more sophistication.

Nope. Only working on linux-lts now.

Offline

#3635 2021-11-19 18:01:22

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,236
Website

Re: Post your handy self made command line utilities

Well, yours doesn't work if the kernel has no patch version (e.g. 5.15.arch).

#!/bin/bash
{ IFS='-' read -r pkgname flavor
  read -r pkgver
} < <(pacman -Q "$1" | xargs -n1)

{ read -r version
  read -r suffix
} < <(pcregrep -o -e '(\d{1}\.\d{1,2}(?:\.\d{1,3})?)' -e '((?<=\.)\w+)?-\d' <<< "$pkgver")

[[ $(uname -r) == "$version-${suffix/-/}${flavor:+-$flavor}" ]]

This was fun, but I'm not sure on the utility. In support requests you give people two commands, uname -r and pacman -Q, not obscure regex or a 50 line Python script.

I guess if you wanted to check it at boot every time...

Last edited by Alad (2021-11-19 18:04:39)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#3636 2021-11-19 18:19:10

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

Re: Post your handy self made command line utilities

find /var/lib/pacman/local/ -name "linux*-$(uname -r | tr - ?)"

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

Offline

#3637 2021-11-19 20:12:02

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,236
Website

Re: Post your handy self made command line utilities

Nice, but I get no results from that find command... it seems the "flavor" is appended instead of prepended.


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#3638 2021-11-19 20:17:19

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

Re: Post your handy self made command line utilities

If you get no results, it means the running kernel doesn't match what's installed.  What do you mean by "flavor"?  If you mean lts/zen/etc, then the asterisk in the find glob covers those.

Perhaps revisions would be needed - but to me it seems much better to convert all the "." and "-" characters into a single common symbol rather than trying to figure out how to convert just some of them in the package version number.

Last edited by Trilby (2021-11-19 20:18:53)


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

Offline

#3639 2021-11-19 23:10:29

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,236
Website

Re: Post your handy self made command line utilities

Trilby wrote:

If you mean lts/zen/etc, then the asterisk in the find glob covers those.

No, it doesn't. The problem is (see schard's post) that lts is placed after linux in the package name, but after the kernel version in the uname output. So you'd need some grouping and swap it around, e.g. with sed.

Last edited by Alad (2021-11-19 23:11:46)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#3640 2021-11-19 23:32:00

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,270
Website

Re: Post your handy self made command line utilities

I regret, what I started here. smile
I will adjust my script for versions without micro version over the weekend.

Offline

#3641 2021-11-19 23:55:00

seth
Member
Registered: 2012-09-03
Posts: 25,107

Re: Post your handy self made command line utilities

You also have to make some hard assumptions about the structure and in particular what glyphs are permissable in patch and flavor tokens…

Tokenizing the stuff and matching the tokens w/o order:

werewastingourlives="$(uname -r | tr '.-' '\n' | sort | tr '\n' ' ')"
while read line; do echo $line |  tr ' .-' '\n' | sort | tr '\n' ' ' | grep $werewastingourlives && echo werewastingourlives; done < <(pacman -Qs linux | sed '/local\/linux/!d;s%local/linux-%%')

Drawback: runing kernel 4.5 while having 5.4 installed…

Online

#3642 2021-11-20 02:18:07

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,344

Re: Post your handy self made command line utilities

Thanks guys, your lines made me review mine and you gave me some cluess...
Works for 'linux - linux-lts - linux-hardened & linux-zen', at least. It's not a oneliner
I couldn't  get 'yours' to work :-(

nr() {
  core=$(uname -r | awk -F '-' '$0=$NF')
  [[ "${core}" = [0-9] ]] && kernel='linux' && core='' || kernel="linux-${core}"

  [ "$(pacman -Qi "${kernel}" | awk '/Version/{print $3}' | tr '.' '-')" \
   != "$(uname -r | sed "s|-${core}*$||" | tr '.' '-')" ] && echo reboot
}

Offline

#3643 2021-11-20 03:24:43

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

Re: Post your handy self made command line utilities

Ok then, how about a much simpler python-inspired approach of just trying to do something that assumes the running kernel files are present and handling the error / exception as it arises:

modprobe -n ext4 >/dev/null 2>&1 || echo reboot needed

Last edited by Trilby (2021-11-20 03:26:13)


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

Offline

#3644 2021-11-20 17:50:52

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,344

Re: Post your handy self made command line utilities

@Trilby, nice one.. and works too!
Using so few resources is nice when polling  when it's running on a bar.

Offline

#3645 2021-11-24 16:03:46

duxontraitor
Member
Registered: 2021-11-24
Posts: 2

Re: Post your handy self made command line utilities

Behold!
My wonderful "copy the output of cat" script, because I'm that lazy...

#!/bin/sh

cat "$1" | xclip -selection clipboard -i

Offline

#3646 2021-11-24 16:09:29

seth
Member
Registered: 2012-09-03
Posts: 25,107

Re: Post your handy self made command line utilities

https://porkmail.org/era/unix/award.html#cat

man xclip wrote:

SYNOPSIS
       xclip [OPTION] [FILE]...

Online

#3647 2021-11-24 21:02:02

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

Re: Post your handy self made command line utilities

And even if it wasn't for the above-note, why use cat at all?  That is definitely a most useless use of cat (in contrast to the avatar).  Don't cat to a pipe to another process, just use '<'.

Perhaps you'd want an alias to cover "-selection clipboard" ... I didn't realize xclip didn't have short options.  That's another reason to prefer xsel:

xsel -ib < $1

Of course, -b is the default, so just:

xsel -i < $1

Last edited by Trilby (2021-11-24 21:06:07)


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

Offline

#3648 2021-11-25 04:00:17

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,353

Re: Post your handy self made command line utilities

without -b xsel copies to primary clipboard, -b is needed, -i can be skipped.


Arch is home!
cwm rofi weaver clifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

Board footer

Powered by FluxBB