scripts

Various scripts for UNIX-like systems
git clone https://git.tronto.net/scripts
Download | Log | Files | Refs | README

commit b98e05ba63f2e108df634b4d9f9e5530769c6b80
parent ed61c5f085cebb86d3d3a34ce01ec22069c1fc70
Author: Sebastiano Tronto <sebastiano.tronto@gmail.com>
Date:   Thu, 10 Jun 2021 18:58:52 +0200

Added scripts and readme

Diffstat:
AREADME.md | 11+++++++++++
Aaddressgrep | 8++++++++
Abackup-config | 29+++++++++++++++++++++++++++++
Abattery-checknow | 21+++++++++++++++++++++
Abattery-status | 17+++++++++++++++++
Abrightness | 7+++++++
Admenu-dwm-sessionmanager | 24++++++++++++++++++++++++
Admenu-filepicker | 29+++++++++++++++++++++++++++++
Admenu-mail-aliases | 11+++++++++++
Admenu-screenshot | 23+++++++++++++++++++++++
Admenu-unmount | 26++++++++++++++++++++++++++
Admenu-urlselect | 10++++++++++
Admenu-websearch | 8++++++++
Adunst-toggle | 14++++++++++++++
Amail-checknow | 17+++++++++++++++++
Amail-compose | 8++++++++
Anotify-battery | 4++++
Anotify-time | 9+++++++++
Aopen-file | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aopen-stdin | 11+++++++++++
Aopen-url | 31+++++++++++++++++++++++++++++++
Apopup-cal12 | 4++++
Apopup-cal3 | 4++++
Apopup-terminal | 5+++++
Asfeed-browser | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aspawn | 5+++++
Atempless | 8++++++++
Atogglemute | 14++++++++++++++
Aurlgrep | 8++++++++
Avolume | 10++++++++++
Awebsearch | 10++++++++++
Axedit-filter | 11+++++++++++
Axkboptions | 6++++++
Axplumb | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Axwallpaper-random-notify | 13+++++++++++++
35 files changed, 621 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,11 @@ +These scripts are made to each do one thing well and be combined with each +other. Many are linked to a keybinding in my X session. + +Feel free to copy them all or in part, read them, study them, take them apart, +copy-paste lines from them and publicly ridicule them as you like. + +Send any suggestion for improvement at +`sebastiano [dot] tronto [at] gmail [dot] com.` + +I might add some examples here at some point, for now just look at the +source code. diff --git a/addressgrep b/addressgrep @@ -0,0 +1,8 @@ +#!/bin/sh + +# Find all email addresses in stdin, print them newline-separated to stdout + +reg='[a-z0-9\._\+\-%]+@[a-z0-9\._\+\-%]+\.[a-zA-Z]+' + +grep -Pio "$reg" + diff --git a/backup-config b/backup-config @@ -0,0 +1,29 @@ +#!/bin/sh + +# Backup local configuration files + +destdir=$HOME/Dropbox/configbackups +srcdir=$HOME/.local/src + +# Uncomment to clean old backups +#rm -rf $destdir/config $destdir/scripts $destdir/src +mkdir -p $destdir/config $destdir/src + +# First copy everything from .config and .bashrc and similar +cp -r $XDG_CONFIG_HOME/* $destdir/config/ +cp $HOME/.bash* $destdir/ + +# Then copy files from src (only source, config, makefile and READMEs) +srcsubdirs=$(ls $srcdir) +for i in $srcsubdirs; do + si="$srcdir/$i" + if [ -d "$si" ]; then + mkdir -p $destdir/src/$i + for f in "$si/*.c" "$si/*.h" "$si/config.*" "$si/Makefile" "$si/README*"; do + for ff in $f; do + [ -e $ff ] && cp $f $destdir/src/$i/ + done + done + fi +done + diff --git a/battery-checknow b/battery-checknow @@ -0,0 +1,21 @@ +#!/bin/sh + +# Check battery state, save result to a file. Pop up notification if changed. +# Requires: battery-status, notify-battery + +file="$XDG_DATA_HOME/batterystatus" +low=20 +crit=15 + +new=$(battery-status) +level=$(echo "$new" | sed 's/%.*//') +status=$(echo "$new" | awk '{print $2}') + +[ "$status" = "Discharging" ] && [ $level -le $low ] && status="Low" +[ "$status" = "Discharging" ] && [ $level -le $crit ] && status="Critical" + +if [ "$status" != "$(cat $file)" ] || [ "$status" = "Critical" ]; then + notify-battery +fi + +echo "$status" > "$file" diff --git a/battery-status b/battery-status @@ -0,0 +1,17 @@ +#!/bin/sh + +# Send battery information to stdout + +# Get battery status +battery="/sys/class/power_supply/BAT0" +message="" + +if [ -d $battery ]; then + capacity=$(cat $battery/capacity) + status=$(cat $battery/status) + message="$capacity% $status" +else + message="No battery" +fi + +echo "$message" diff --git a/brightness b/brightness @@ -0,0 +1,7 @@ +#!/bin/sh + +# Increase / decrease brightness, and pops up a notification +# Requires: xbacklight, notify-send +# Usage: brightness {inc|dec} n + +xbacklight -$1 $2 && notify-send "Brightness: $(xbacklight | sed 's/\..*$//')%" diff --git a/dmenu-dwm-sessionmanager b/dmenu-dwm-sessionmanager @@ -0,0 +1,24 @@ +#!/bin/sh + +# Prompts menu to shutdown/reboot/close dwm +# Requires: dmenu (or equivalent), dwm (optional) + +menu=${MENU:-dmenu} +menuopts="-i -p" +menuprompt="Do you want to quit?" +wmname="dwm" + +shutdown_cmd="sudo shutdown -h now" +reboot_cmd="sudo reboot" +closewm_cmd="pkill $wmname" + +value=$(echo "Shutdown\nReboot\nQuit dwm" | $menu $menuopts "$menuprompt") + +if [ "$value" = "Shutdown" ]; then + $shutdown_cmd +elif [ "$value" = "Reboot" ]; then + $reboot_cmd +elif [ "$value" = "Quit $wmname" ]; then + $closewm_cmd +fi + diff --git a/dmenu-filepicker b/dmenu-filepicker @@ -0,0 +1,29 @@ +#!/bin/sh + +# A dmenu-based file picker (prints selected file to stdout) +# Requires: dmenu (or similar) +# Usage: dmenu-filepicker [path] + +menu=${MENU:-dmenu} +menuopts="-i -l 15" + +basedir="$(pwd)" + +next="${@:-$(pwd)}" + +while true; do + if [ -z "$next" ]; then + break + elif [ "$next" = "." ]; then + pwd + break + elif [ -d "$next" ]; then + cd "$next" + next=$(ls -a | $menu $menuopts) + else + echo "$(pwd)/$next" + break + fi +done + +cd "$basedir" diff --git a/dmenu-mail-aliases b/dmenu-mail-aliases @@ -0,0 +1,11 @@ +#!/bin/sh + +# Select mail alias via dmenu +# The email address must be the second word in a line of $aliasfile +# Requires: dmenu (or similar) + +menu=${MENU:-dmenu} +menuopts="-l 20" +aliasfile="$XDG_CONFIG_HOME/mblaze/aliases" + +xargs printf "%-40s %s\n" <"$aliasfile" | $menu $menuopts | awk '{print $2}' diff --git a/dmenu-screenshot b/dmenu-screenshot @@ -0,0 +1,23 @@ +#!/bin/sh + +# Screenshot utility +# Requires: dmenu (or similar), imagemagick + +menu=${MENU:-dmenu} +menuopts="-i -p" +menuprompt="Select type of screenshot:" + +folder="$HOME/Pictures/screenshots" +filename="screenshot_$(date +%Y-%m-%d-%H%M%S).png" +filepath="${folder}/${filename}" + +t=$(echo "Full\nSelection" | $menu $menuopts "$menuprompt") +#op="" TODO: remove if not needed +if [ "$t" = "Full" ]; then + op="-window root" +else + pkill xbanish # otherwise I can't use mouse to select area +fi + +[ -n "$t" ] && sleep 0.1 && import $op $filepath +spawn xbanish # This can be removed if you don't use xbanish diff --git a/dmenu-unmount b/dmenu-unmount @@ -0,0 +1,26 @@ +#!/bin/sh + +# Prompts selection for mounted devices and unmounts the selected one +# Requires: udevil, dmenu (or similar), notify-send (optional) + +menu=${MENU:-dmenu} +writeout="notify-send" # Change this to whatever you want, e.g. echo + +listdev=$(grep "media" /proc/mounts | sed 's/\/media.*\///g' | \ + awk '{print $1" ("$2")"}') + +if [ -z "$listdev" ]; then + $writeout "Nothing to unmount" +else + seldev=$(echo "$listdev" | $menu | awk '{print $1}') + if [ -n "$seldev" ]; then + udevil unmount "$seldev" + failed=$(udevil info "$seldev" | grep mounted | awk '{print $3}') + if [ $failed -eq 1 ]; then + $writeout "Unmount FAILED" "Device is still mounted!" + else + $writeout "Device unmounted" + fi + fi +fi +# notify-send "$(udisksctl unmount -b $seldev)" diff --git a/dmenu-urlselect b/dmenu-urlselect @@ -0,0 +1,10 @@ +#!/bin/sh + +# Finds all URLs in stdin and prompts a dmenu choice, then writes the selected +# url to stdout. +# Requires: dmenu (or similar), urlgrep + +menu=${MENU:-dmenu} +menuopts="-l 20" + +urlgrep | $menu $menuopts diff --git a/dmenu-websearch b/dmenu-websearch @@ -0,0 +1,8 @@ +#!/bin/sh + +# Popup $TERMINAL prompting for websearch +# Requires: dmenu (or similar), websearch + +menu=${MENU:-dmenu} + +websearch $(echo "" | $menu -p "websearch:") diff --git a/dunst-toggle b/dunst-toggle @@ -0,0 +1,14 @@ +#!/bin/sh + +# Toggle "do not disturbe" mode with dunst +# Requires: dunst, notify-send + +if [ "$(dunstctl is-paused)" = "false" ]; then + notify-send -t 3000 "Notifications: OFF" "Notifications will be hidden" + sleep 3 + dunstctl set-paused true +else + dunstctl set-paused false + notify-send -t 3000 "Notifications: ON" "Notifications reactivated" +fi + diff --git a/mail-checknow b/mail-checknow @@ -0,0 +1,17 @@ +#!/bin/sh + +# Check for new email, save result to file. +# Requires: mpop, notify-send + +file="$XDG_DATA_HOME/newmail" +notify="notify-send" + +l=$(cat "$file") +n=$(mpop -s | awk '/new*./{print $2}') + +if [ -n "$n" ]; then + echo "$n" > "$file" + if [ "$n" != "no" ] && [ "$l" != "$n" ]; then + $notify "New email ($n)" + fi +fi diff --git a/mail-compose b/mail-compose @@ -0,0 +1,8 @@ +#!/bin/sh + +# Open a mail composer. +# Requires: mblaze + +terminal=${TERMINAL:-xterm} + +$terminal -e mcom $@ diff --git a/notify-battery b/notify-battery @@ -0,0 +1,4 @@ +#!/bin/sh + +# Popup battery notification, based on the script battery-status +notify-send "Battery" "$(battery-status)" diff --git a/notify-time b/notify-time @@ -0,0 +1,9 @@ +#!/bin/sh + +# Little notification to show time and date +# Requirements: notify-send + +t=$(date +"%H:%M:%S") +d=$(date +"%d\ %B\ %Y,\ %A") + +notify-send "$t" "$d" diff --git a/open-file b/open-file @@ -0,0 +1,77 @@ +#!/bin/sh + +# Inspired by https://github.com/salman-abedin/launch.sh +# Launches files based on their mimetypes +# Use option -s devour to swallow or -s " " to open normally from terminal +# Use -t to specify mimetype +# Requires: dmenu_path or similar (fallback) + +menu=${MENU:-dmenu} + +while getopts "s:t:" opt; do + case "$opt" in + s) + launcher="$OPTARG" + ;; + t) + mimetype="$OPTARG" + ;; + esac +done + +shift `expr $OPTIND - 1` + +[ -z "$launcher" ] && launcher="spawn" +[ -z "$mimetype" ] && mimetype="$(file --mime-type "$1" -bL)" + +[ -d "$1" ] && $launcher $XFILEMANAGER "$1" && exit 0 +[ ! -f "$1" ] && echo "$1: bad argument" && exit 1 + +case "$mimetype" in + video/*) + $launcher $VIDEOPLAYER "$1" + ;; + audio/*) + $launcher $MUSICPLAYER "$1" + ;; + application/pdf | application/postscript | image/vnd.djvu) + $launcher $VIEWER "$1" + ;; + image/*) + $launcher $IMAGEVIEWER "$1" + ;; + application/msword | \ + application/vnd.oasis.opendocument.text | text/rtf | \ + application/vnd.openxmlformats-officedocument.wordprocessingml.*) + $launcher $TEXTDOCUMENT "$1" + ;; + application/ms-excel | application/vnd.oasis.opendocument.spreadsheet | \ + text/rtf | application/vnd.openxmlformats-officedocument.spreadsheetml.*) + $launcher $SPREADSHEET "$1" + ;; + text/html | text/enriched) + $launcher $BROWSER --new-window "$1" + ;; + text/*) + $launcher $XEDITOR "$1" + ;; + application/zip) + unzip "$1" + ;; + application/x-rar-compressed) + unrar "$1" + ;; + application/x-archive | application/x-tar | \ + application/x-bzip2 | application/gzip | application/x-lzip | \ + application/x-lzma | application/x-xz | application/x-gtar) + tar -tavf "$1" + ;; + *) + prog=$(dmenu_path | $menu -p "How to open $1 ?") + if [ -n "$prog" ]; then + $launcher $prog "$1" + else + exit 1 + fi + ;; +esac diff --git a/open-stdin b/open-stdin @@ -0,0 +1,11 @@ +#!/bin/sh + +# Saves standard input in a download folder and opens it with myopen +# Usage: open-stdin +# Requires: open-file + +folder=$HOME/Downloads/open-stdin + +tempfile=$(mktemp -p "$folder") + +cat > "$tempfile" && open-file -t "$1" "$tempfile" diff --git a/open-url b/open-url @@ -0,0 +1,31 @@ +#!/bin/sh + +# Open link in preferred application +# Requires: mail-compose, xsel (optional) + +browser=${BROWSER:-firefox} +imageviewer=${IMAGEVIEWER:-imv} + +[ -z "$1" ] && exit 0 + +# Optional: copy url to clipboard +echo "$@" | xsel -ib + +case "$@" in + mailto:*) + mail-compose $@ + ;; + *.jpg | *.jpeg | *.png) + spawn $imageviewer "$@" + ;; + *.gif) + spawn $imageviewer "$@" + ;; +# https://www.youtube.com/watch\?v=*) +# spawn $VIDEOPLAYER "$1" +# ;; + *) + spawn $browser "$@" + ;; +esac + diff --git a/popup-cal12 b/popup-cal12 @@ -0,0 +1,4 @@ +#!/bin/sh + +# Popup $TERMINAL displaying cal -3 +$TERMINAL -T "stfloat" -g 66x33+620+250 -e sh -c 'cal $(date +%Y); read x' diff --git a/popup-cal3 b/popup-cal3 @@ -0,0 +1,4 @@ +#!/bin/sh + +# Popup $TERMINAL displaying cal -3 +$TERMINAL -T "stfloat" -g 64x9+620+450 -e sh -c 'cal -3; read x' diff --git a/popup-terminal b/popup-terminal @@ -0,0 +1,5 @@ +#!/bin/sh + +# TODO this replaces $FLOATINGTERMINAL + +$TERMINAL -T 'stfloat' -g 85x25+550+300 $@ diff --git a/sfeed-browser b/sfeed-browser @@ -0,0 +1,62 @@ +#!/bin/sh + +# This script is based on sfeed (https://codemadness.org/sfeed.html), but +# it allows you organize your feeds in directories and subdirectories. +# In your $sfd directory (see below) you should have two folders: +# urls: it can contain more subfolders and files. Each file should contain +# only one line with the url to the feed. The name of the file is the +# name of the feed (it can contains spaces and such). +# files: this one can be empty, it will be filled with the feed files +# An older version of this script also marked viewed items and removed +# them before displaying the choice of feeds. Contact me if you are interested +# in using it. + +# Requires: sfeed, dmenu-file-picker (or similar), open-url (or similar) + +filepicker="dmenu-filepicker" # Try "nnn -p -" +menu=${MENU:-dmenu} +menuopts="-l 35 -i" +urlopener=open-url +sfd="$XDG_CONFIG_HOME/sfeed" +showlast=10 + +# Might add more stuff later +fixurl() { + sed 's/old\.reddit\.com/reddit\.com/' +} + +dirtofeedpaths() { + while read line; do + find "$line" | while read fname; do + [ -f "$fname" ] && echo "$fname" + done + done +} + +pathstosfeedrc() { + printf "sfeedpath=\"$sfd/files\"\n\nfeeds() {\n" + while read line; do + feedname=$(echo "$line" | sed 's/.*\///') + read feedurl <"$line" + printf "\tfeed \"$feedname\" \"$feedurl\"\n" + done + printf "}\n" +} + +feedmenu() { + while read line; do + feedname=$(echo "$line" | sed 's/.*\///') + sfeed_plain "$sfd/files/$feedname" | head -$showlast + done | $menu $menuopts +} + +openfeeds() { + while read line; do + url=$(echo "$line" | sed 's/.*[\t ]//' | fixurl) + [ -n "$url" ] && echo $url + done | xargs $urlopener +} + +$filepicker "$sfd/urls" | dirtofeedpaths > "$sfd/last" +pathstosfeedrc < "$sfd/last" > "$sfd/sfeedrc" +sfeed_update "$sfd/sfeedrc" && feedmenu < "$sfd/last" | openfeeds diff --git a/spawn b/spawn @@ -0,0 +1,5 @@ +#!/bin/sh + +# Simple command to spawn and detach an application + +"$@" >/dev/null 2>&1 & diff --git a/templess b/templess @@ -0,0 +1,8 @@ +#!/bin/sh + +# Write standard in to a temporary file and sends it to a pager. + +pager=${PAGER:-less} +tempfile=$(mktemp) + +cat > "$tempfile" && $pager "$tempfile" diff --git a/togglemute b/togglemute @@ -0,0 +1,14 @@ +#!/bin/sh + +# Toggle mute state and notify +# Requires: pulsemixer, notify-send + +ismute=$(pulsemixer --toggle-mute --get-mute) + +if [ "$ismute" = "1" ]; then + m="muted" +else + m="unmuted" +fi + +notify-send "Audio $m" diff --git a/urlgrep b/urlgrep @@ -0,0 +1,8 @@ +#!/bin/sh + +# Find all url in stdin, print them newline-separated to stdout + +reg='(((http|https|ftp)|mailto)[.:][^ >"\t]*|www\.[-a-z0-9.]+)[^ .,;\t>">\):]' + +grep -Po "$reg" + diff --git a/volume b/volume @@ -0,0 +1,10 @@ +#!/bin/sh + +# Change volume and notify +# Usage: volume {+n|-n} +# Requires: pulsemixer, notify-send + +#TODO: use this to replace myvolumeup/myvolumedown + +newvol=$(pulsemixer --change-volume "$1" --get-volume | awk '{print $1}') +notify-send "Volume" "$newvol%" diff --git a/websearch b/websearch @@ -0,0 +1,10 @@ +#!/bin/sh + +# Perform a web search + +browser=${BROWSER:-firefox} +searchengine="https://duckduckgo.com/?q=" +# For google: https://www.google.com/search?q= + +arg=$(echo "$@" | sed 's/ /\+/g') +[ -n "$arg" ] && spawn "$browser" "${searchengine}$arg" diff --git a/xedit-filter b/xedit-filter @@ -0,0 +1,11 @@ +#/!bin/sh + +asfd +# Open stdin in a text editor and sends modified selection to stdout + +xeditor=${XEDITOR:-gedit} + +f=$(mktemp) +cat > "$f" +$xeditor "$f" && cat "$f" + diff --git a/xkboptions b/xkboptions @@ -0,0 +1,6 @@ +#!/bin/sh + +# Loads my xkboptions + +setxkbmap -option compose:ralt +setxkbmap -option caps:swapescape diff --git a/xplumb b/xplumb @@ -0,0 +1,66 @@ +#!/bin/sh + +# My interpretation of a plumber (plan9 style). +# It scans the selected text (xsel) and decides what to do with it among +# some possibilities (open file, open url, man page...). If none is matched, +# a choice is prompted. + +# Requires: dmenu (or similar), mail-compose, open-file, websearch, xsel + +# TODO: +# send mail to email address; +# calendar if it is a date +# choice: pipe to another program + +browser=${BROWSER:-firefox} +terminal=${TERMINAL:-xterm} +xeditor=${XEDITOR:-gedit} +menu=${MENU:-dmenu} +menuopts="-l 10" + +text=$(xsel | sed 's/\n/ /g') +if [ $(echo "$text" | wc -c) -ge 11 ]; then + shorttext=$(echo "$text" | cut -b 1-10)... +else + shorttext="$text" +fi + +choice() { + chosen=$(printf "websearch\nedit" | \ + $menu $menuopts -p "What to do with \"$shorttext\"?") + + if [ "$chosen" = "websearch" ]; then + websearch $text + elif [ "$chosen" = "edit" ]; then + f=$(mktemp) + xsel > "$f" + $xeditor "$f" + fi +} + +trymail() { + addr=$(echo "$1" | addressgrep) + ( [ -n "$addr" ] && mail-compose $addr ) || return 1 +} + +tryman() { + number=$(echo "$1" | sed 's/[^1-8]//g') + name=$(echo "$1" | sed 's/([1-8])//g' | sed 's/ //g') + if [ $(man -w "$number" "$name" | wc -l) = 1 ]; then + $terminal -e man "$number" "$name" + return 0 + else + return 1 + fi +} + +tryurl() { + url=$(echo "$1" | urlgrep) + ( [ -n "$url" ] && $browser $url ) || return 1 +} + +open-file "$text" || \ +tryurl "$text" || \ +trymail "$text" || \ +tryman "$text" || \ +choice diff --git a/xwallpaper-random-notify b/xwallpaper-random-notify @@ -0,0 +1,13 @@ +#!/bin/sh + +# Set a random background from the wallpapers folder and notify about it +# Requires: xwallpaper (or feh), notify-send (optional) + +folder=~/Pictures/wallpapers +pic=$(ls $folder | sort -R | head -1) + +# Alternative: use feh +#feh --bg-fill --no-fehbg $folder/$pic +xwallpaper --zoom $folder/$pic + +notify-send "Background of the day" $pic