You are not logged in.

#3726 2023-08-16 05:48:25

Awebb
Member
Registered: 2010-05-06
Posts: 6,164

Re: Post your handy self made command line utilities

Trilby wrote:

Couldn't you acheive the same goal by just looking for broken links:

find -L /etc/systemd/ -type l

...and I could then feed this to pkgfile, in case I really want to know where the link came from. Nice.

Last edited by Awebb (2023-08-16 05:51:38)

Offline

#3727 2023-08-20 23:09:51

vikramr
Member
Registered: 2009-09-22
Posts: 36

Re: Post your handy self made command line utilities

Small script to calculate when I should  start charging my EV to make sure  its ready by 8:00 AM.

#!/bin/bash
#in kwh
battery_capacity=60
#in kw
charge_rate=2.8
charge_complete_time=8
current_battery_level=$1

if [[ $# -ne 1 ]]; then
    echo 'Too many/few arguments, expecting one' >&2
    exit 1
fi

if [[ $(echo "if (( $current_battery_level < 0 ) || ( $current_battery_level > 1 )) 1 else 0" | bc) -eq 1 ]]

then
	echo "current charge level must be between 0 and 1"
	exit 1
fi

echo "
	scale=2;
	print \"current charge level=  \" ; $current_battery_level;
	charge_duration=$battery_capacity*(1 - $current_battery_level)/$charge_rate;
	print \"charge_duration = \" ;charge_duration;
	charge_complete_time=$charge_complete_time
	print \"target complete time = \" ;charge_complete_time;
	start_time=charge_complete_time-charge_duration - 1;
	if (start_time <= 0) start_time +=24;
	print \"start_time = \"; start_time;"	|  bc

Offline

#3728 2023-08-27 18:43:13

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

Re: Post your handy self made command line utilities

Script to delete orphaned packages (and their configs) but retain packages installed as makedepends for AUR builds. On a measure-twice-cut-once basis, it can be used to list orphans cum makedepends, makedepends and orphans ex makedepends in lieu of actually deleting.

This works on the assumption that the .SRCINFO for AUR packages is kept in /home/AUR/builds/cache/<pkgname>/ and the variable AUR_SRCINFO may need to be modified accordingly.

#! /bin/bash

AUR_SRCINFO='/home/AUR/builds/cache/*/.SRCINFO'
MAKEDEPENDS=$(mktemp)
ORPHANS=$(mktemp)
TRUE_ORPHANS=$(mktemp)

for i in ${AUR_SRCINFO}; do
        if sed -n '/pkgname/ s/^pkgname = //p' "${i}" | pacman -Qi - > /dev/null 2>&1 ; then
                sed -n '/makedepends/ s/^.*makedepends = //p' ${i} >> "${MAKEDEPENDS}"
        fi
done
sort -u "${MAKEDEPENDS}" -o "${MAKEDEPENDS}"
pacman -Qqtd > "${ORPHANS}"
grep -Fvxf "${MAKEDEPENDS}" "${ORPHANS}" > "${TRUE_ORPHANS}"
case ${1} in
        "M")
                echo "makedepends packages:"
                cat "${MAKEDEPENDS}"
        ;;
        "O")
                echo "orphaned and makedepends packages:"
                cat "${ORPHANS}"
        ;;
        "R")
                echo "Removing true orphaned packages and their config files and dependencies"
                cat "${TRUE_ORPHANS}" | pacman -Rns -
        ;;
        "T")
                echo "true orphaned packages:"
                cat "${TRUE_ORPHANS}"
        ;;
        *)
                echo "Parameters:"
                echo "M for makedepends packages"
                echo "O for orphaned and makedepends packages"
                echo "R to remove true orphaned packages and their config files and dependencies. THIS MUST BE RUN AS ROOT"
                echo "T for true orphaned packages"
        ;;
esac
rm "${MAKEDEPENDS}"
rm "${ORPHANS}"
rm "${TRUE_ORPHANS}"

Offline

#3729 2023-09-16 23:45:14

sweet_tea
Member
Registered: 2022-12-29
Posts: 3

Re: Post your handy self made command line utilities

An interactive Yes/No script similar to fzf, gum choose / gum confirm. Entirely in bash.

#!/bin/bash
# simple script for an interactive YES/NO prompt
# set -x

menu() {
    tput civis
    index=0
    def="Continue? "
    clear
    key="j"

    prompt="${1:-$def}"
    prompt2="${2:-}"

    while true; do
        tput cup 0 0 && printf '\e[35m%s\e[0m\n' "${prompt}"
        tput cup 1 0 && printf '\e[33m%s\n' "${prompt2}"
    if ((index == 1)); then
        tput cup 2 0 && printf '\e[2k' && printf '\e[31m%s\e[0m\n' "> Yes"
        tput cup 3 0 && printf '\e[2k' && printf '\e[32m%s\e[0m\n' "  No"
    elif ((index == 0)); then
        tput cup 2 0 && printf '\e[2k' && printf '\e[32m%s\e[0m\n' "  Yes"
        tput cup 3 0 && printf '\e[2k' && printf '\e[31m%s\e[0m\n' "> No"
    fi

        
        case "$key" in
            j|$'\x1b\x5b\x41') if [[ "$index" = 0 ]]; then index=1 ; elif [[ "$index" = 1 ]]; then index=0 ; fi ;;
            k|$'\x1b\x5b\x42') if [[ "$index" = 0 ]]; then index=1 ; elif [[ "$index" = 1 ]]; then index=0 ; fi ;;
            $'\x0a') break ;;
            *) echo >/dev/null ;;
        esac

        unset K1 K2 K3
        read -sN1
        K1="$REPLY"
        read -sN2 -t 0.001
        K2="$REPLY"
        read -sN1 -t 0.001
        K3="$REPLY"
        key="$K1$K2$K3"

    done
    # index will be 1 or 0 which is also return codes for success and fail
    # used for the last condition on whether to start the game or not
    stty sane
    echo "$index"
    return "$index"
}

cleanup() {
    # clear
    tput cnorm
    stty echo
}

trap cleanup EXIT

menu "${@}"

Improvements and advice welcomed

Last edited by sweet_tea (2023-09-16 23:51:25)

Offline

#3730 2023-09-18 10:54:16

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

Re: Post your handy self made command line utilities

Wrapper around bluetoothctl

#!/bin/bash

ACTION() {
echo "Choose device using id"
read id
echo "What do you want to do?
provide space separated command/s
pair unpair trust untrust connect disconnect remove"
read action

xdotool search --name blue && xdotool windowclose $(xdotool search --name blue)

for x in $action; do
	bluetoothctl "$x" "$id"
done
}

bluetoothctl devices
echo "choose what to do?
a. act on known device b. scan for new"
read ans
if [[ $ans = a ]]; then
    ACTION
else
    lxterminal -t blue --geometry=70x15 -e bluetoothctl scan on
    ACTION
fi

Offline

Board footer

Powered by FluxBB