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,738
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.


Para todos todo, para nosotros nada

Offline

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

HobbitJack
Member
From: Florida or Michigan
Registered: 2024-09-21
Posts: 13
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: 13
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: 60,805

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: 13
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

Board footer

Powered by FluxBB