Handy scripts you'd like to share

Remove certain text at the beginning of many file names

Sample, if you have multiple files like

20221227 2210 - Random TV Chanel HD - Make Arch Great Again.ts

but you need only 'Make Arch Great Again.ts', use

for file in *.ts; do
    echo mv "$file" "${file#*-*- }"
done

in '#!/bin/bash' script.

Remove 'echo' if the output looks ok.


More on https://stackoverflow.com/ :wink:


3 Likes

Start and stop your conkys with a simple command

This script will start a conky if it's not running; if it is running, it will kill it.

Useful for keybindings; as the same keybinding will either start or stop the conky.

It takes the name of the conky config file as an argument.

Please note to use it, you must call your script "toggleconky.sh"; this is due to the grep declaring the PID variable: we only want the line output of ps that has the actual running conky.

PID=`ps -ef | grep -v -P 'grep|toggleconky' | grep -o -P $REGEX  | awk '{print $2}'`

You can name your script anything, but change that line accordingly.

#!/bin/sh

# USAGE: toggleconky.sh <name of conky config> 

CONFIG=""
if [ -z "$1" ]; then
	exit 0
else
	CONFIG=$1	
fi
#echo $CONFIG
REGEX='.+conky.+'$CONFIG
#echo $REGEX

PID=`ps -ef | grep -v -P 'grep|toggleconky' | grep -o -P $REGEX  | awk '{print $2}'`
#check if PID, if not, we start, otherwise, kill
#echo $PID
if [ -z "$PID" ]; then
	#echo 'not found...start'
	conky -c ~/.config/conky/$CONFIG
else
	#echo 'found, kill'	
	kill -9 $PID
fi
4 Likes

Many user use more than one conky files :wink:

I use

killall conky

done :wink:
And as sample

 conky -c ~/.config/conky/conky-shct

or use an alias for that.
But basically I have conky because I want to see it :wink:

But basically your script is a nice thing. :slight_smile:

4 Likes

Agree, Conky is made to be seen. But this is Linux, and you do it because you can :slight_smile:

Actually wrote the script for when I was developing lua conkys....I'd create the occaisional bug (not often, most of my code is bug free :slight_smile: ) and lua would start dumping reams of stdout....it was easy to just use my toggleconky.sh to stop it and start it again...."!!" became my friend.

I have a section of my monitor now for "wildcard" conkys. I can choose which one I want there and use my keybindings to display it (or kill it).

The script actually morphed from a similar use case where I needed to start/stop a process from a keybinding (because its a headless computer governed by a flirc remote). The tricky part is getting all the grep/regex to extract the PID so you can execute kill.

Like the "killall" suggestion...when I was a wee lad, all I had was "kill".

2 Likes

A couple simple fish shell abbreviations for the lazy ones like me:

.dots
abbr -a .dots -r '\.{3,}.*' --position anywhere -f _abbr_dots
function _abbr_dots
    string match -qr '\.\.(?<dots>\.+)(?<rest>.*)' -- $argv
    echo ..(string replace -a '.' '/..' -- $dots)$rest
end

Allows to shorten repeated leading ../ with multiple dots also when entering a filename, e.g.
micro .../file.txt expands to micro ../../file.txt.

.conf
abbr -a .conf -r '~~.*' --position anywhere -f _abbr_conf
function _abbr_conf
    test -n "$XDG_CONFIG"
    and set -l cfg $XDG_CONFIG
    or  set -l cfg ~/.config
    set -q _abbr_conf_tab   # skip expanding the shortcuts on tab completion
    or switch $argv         # shortcuts for frequently used files and dirs
        case '~~f'      ; set argv '~~fish'
        case '~~fc'     ; set argv '~~fish/config.fish'
        case '~~fcd'    ; set argv '~~fish/conf.d'
        case '~~ffn'    ; set argv '~~fish/functions'
        case '~~fa'     ; set argv (status -f) # this file
        case '~~s'      ; set argv '~~sway'
        case '~~ss'     ; set argv '~~sway/scripts'
        case '~~scd'    ; set argv '~~sway/config.d'
        case '~~w'      ; set argv '~~waybar'
        case '~~wc'     ; set argv '~~waybar/config'
        case '~~ws'     ; set argv '~~waybar/scripts'
        case '~~wss'    ; set argv '~~waybar/style.css'
        case '~~grub'   ; set argv '/etc/default/grub' # shortcuts do not need to be in .config
    end
    set -e _abbr_conf_tab
    string replace -r '^~~' $cfg/ -- $argv |string replace -r ^$HOME/ '~/'; or true
    # reset status in case the path wasn't in $HOME and the second replace failed
end~~~

Allows to shorten ~/.config[/name] with ~~[name], where 'name' can also be one of the configured shortcuts (the case statement and _abbr_conf_tab can be removed if no shortcuts are desired).


Note that Ctrl-Space can be used to avoid expanding, and Ctrl-Z to undo the expansion after the fact.

File completion won't work until after expansion, which is somewhat annoying.
I see no downside in binding / and tab to also first expand abbreviations though:

bind /  expand-abbr self-insert
bind \t expand-abbr complete # or (to skip shortcuts on tab completion):
bind \t 'set _abbr_conf_tab; commandline -f expand-abbr; commandline -f complete'
# NOTE: 'set _abbr_conf_tab' expand-abbr complete  does not work

and, why not (unrelated):

bind \e 'commandline -P; and commandline -f cancel; or commandline -f kill-whole-line'

Note: bindings (especially \t) may be ignored depending where they are issued, e.g. in conf.d.

3 Likes

Hangman is a must for every terminal user. (debatable)

https://chonkyrabbit.eu/git/random_games.git/tree/hangman.lua

if you have a word list, you could use cat worldlist | shuf -n 1 | xargs lua hangman.lua

https://chonkyrabbit.eu/git/raspi_power_button.git/about/

Power button for your Raspi, in case you ever lose control...

3 Likes