commit 2e84e05fe5a8fc8aaed11c96024028a8c827fcce Author: Valerie Date: Thu Jul 6 11:07:16 2023 -0400 initial commit; repository moved diff --git a/README.md b/README.md new file mode 100644 index 0000000..7666ca4 --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ + +# Shell Scripts + +A handful of scripts I've written to make my life easier on Linux. + +## banana-runner + +A Python script that runs command shortcuts from an ini file. Quick and easy to set +up. + +## codecat + +A bash functino that wraps GNU `source-highlight` in a way that isn't a complete +nightmare to use. Automatically sets the `-f`, `-i`, and `--src-lang` flags where +appropriate. + +## goto + +A local jump bash function. Propagates backwards through the working path to find a +`.goto` ini file, and supports chaining jumps if you need that kind of thing. + +## inet + +A collection of network related scripts: + +- `is-back`: send a notification once the internet connection is online +- `myip`: gets the machine's local IP address +- `weather`: wraps [wttr.in](https://wttr.in) with location + +## mountain + +Friendly and consistent device mounting/unmounting. + +## run + +Just runs `.run` in a directory. + diff --git a/banana-runner/.banana b/banana-runner/.banana new file mode 100644 index 0000000..5a858a8 --- /dev/null +++ b/banana-runner/.banana @@ -0,0 +1,7 @@ +[$] +TEST = foo bar + +[test] +command = echo '${TEST}' +shell = true + diff --git a/banana-runner/banana-runner.py b/banana-runner/banana-runner.py new file mode 100755 index 0000000..1e209c2 --- /dev/null +++ b/banana-runner/banana-runner.py @@ -0,0 +1,69 @@ +#!/usr/bin/python +from configparser import ConfigParser +from os import chdir, getcwd +from os.path import devnull, expanduser, isdir, isfile +from re import search +from shlex import split +from subprocess import Popen, run +from sys import argv, stdin, stdout, stderr + +config = ConfigParser() +CONFIG_PATH = f'{getcwd()}/.banana' +if not isfile(CONFIG_PATH): + print("banana-runner: no config found!") + exit(1) +config.read(CONFIG_PATH) +sections = config.sections() + +sections.remove("$") + +if len(argv) == 1 or argv[0] == "$": + print("banana-runner: no arguments given") + exit(1) + +if argv[1] == '-l' or argv[1] == '--list': + # do thing + for section in sections: + if section == '$': + continue + print(f'{section}') + exit() + +name = argv[1] +if name not in sections: + print(f'banana-runner: no section {name} found') + exit(1) +section = config[name] + +cmd = section['command'] +alias_list = config['$'] + +def sub_aliases(string): + output = string + for alias in alias_list: + substring = '${' + alias.upper() + "}" + if substring in output: + output = output.replace(substring, alias_list[alias]) + return output + +cmd = sub_aliases(cmd) + +if 'target' in section: + tdir = sub_aliases(section['target']) + tdir = expanduser(tdir) + if isdir(tdir): + chdir(tdir) + else: + if isfile(tdir): + print(f'banana-runner: "{tdir}" is not a directory!') + else: + print(f'banana-runner: "{tdir}" does not exist!') + exit(1) + +print(f" {cmd}") + +if 'shell' in section and section['shell']: + run(split(cmd)) +else: + with open(devnull, 'w') as _: + Popen(cmd, shell = True, stdout = _, stderr = _) diff --git a/banana-runner/banana-runner.sh b/banana-runner/banana-runner.sh new file mode 100644 index 0000000..3066e14 --- /dev/null +++ b/banana-runner/banana-runner.sh @@ -0,0 +1,17 @@ +#!/usr/bin/xonsh +from configparser import ConfigParser + +config = ConfigParser() +CONFIG_PATH = "$(pwd)/.banana" +if not isfile(CONFIG_PATH): + echo "banana-runner: no config found!") + exit 1 +config.read(CONFIG_PATH) +sections = config.sections() + +sections.remove('$') + +if len($($!)) == 1 or $($1) == '$': + echo 'banana-runner: no arguments given' + + diff --git a/goto/README.md b/goto/README.md new file mode 100644 index 0000000..bb93f49 --- /dev/null +++ b/goto/README.md @@ -0,0 +1,15 @@ +# Goto +I don't like how jump works, so I made a different solution: I got tired of using cd to go through structured directories for development, so my `goto` uses a `.goto` ini file in the root directory of a project to store aliases for directories. For a sample configuration: + +```ini +[goto] +root = . +foo = ./some/long/directory/path/ +bar = ./some/other/long/path/ +``` + +`goto foo` replaces `cd some/long/directory/path/`, and from `some/long/directory/path/` to get back to the "root" we put the file at, `goto root` replaces `cd ../../../../`. + +## Installing goto + +just move the two scripts into `/usr/lib/goto/` and add `source /usr/lib/goto/goto.sh` to your shell `rc` file. \ No newline at end of file diff --git a/goto/goto.sh b/goto/goto.sh new file mode 100755 index 0000000..706d809 --- /dev/null +++ b/goto/goto.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +function goto() { + if [ "$1" == "-l" ]; then + /usr/lib/goto/resolve "$@" + return + fi + if [[ $# > 1 ]]; then + for name in "$@" + do + jump=$(/usr/lib/goto/resolve "$name") + builtin cd "$jump" + done + return + fi + td=$(/usr/lib/goto/resolve "$@") + builtin cd "$td" + return +} diff --git a/goto/resolve b/goto/resolve new file mode 100755 index 0000000..be1c5a9 --- /dev/null +++ b/goto/resolve @@ -0,0 +1,35 @@ +#!/usr/bin/python +from configparser import ConfigParser +from os import chdir, getcwd +from os.path import isfile +from pathlib import Path +from signal import signal, SIGPIPE, SIG_DFL +from sys import argv + +config = ConfigParser() +FILE = f'{getcwd()}/.goto' +while not isfile(FILE): + if(getcwd() == '/'): + exit() + else: + chdir(Path(getcwd()).parent) + FILE = f'{getcwd()}/.goto' +config.read(FILE) +places = config['goto'] + +if len(argv) == 0: + exit() + +if argv[1] == '-l' or argv[1] == '--list': + for title in places: + print(f'{title} -> {places[title]}') + exit() + +if argv[1] not in places: + exit() + +path = places[argv[1]] +if path.startswith('./'): + path = f'{str(Path(FILE).parent)}/{path[2:]}' +signal(SIGPIPE, SIG_DFL) +print(path) diff --git a/inet/README.md b/inet/README.md new file mode 100644 index 0000000..a1dc9f6 --- /dev/null +++ b/inet/README.md @@ -0,0 +1,14 @@ +# Networking-related scripts + +## is-back + +Pings `archlinux.org` until successful. Sends a notification when it is. + +## myip + +Check a device's IP address with way less typing. + +## weather + +Just curls [wttr.in](https://wttr.in) with the location stored in `~/.config/weather_location`. + diff --git a/inet/is-back b/inet/is-back new file mode 100755 index 0000000..586dbe5 --- /dev/null +++ b/inet/is-back @@ -0,0 +1,12 @@ +#!/usr/bin/bash + +while true; do + if (ping archlinux.org -W 5 -c 1); then + echo "Ping responded; internet is up." + notify-send "Internet is up." --app-name="Ping Test" --icon="$(pwd)/is-back-icon.png" + exit 0 + else + continue + fi +done + diff --git a/inet/is-back-icon.png b/inet/is-back-icon.png new file mode 100644 index 0000000..d2f5f83 Binary files /dev/null and b/inet/is-back-icon.png differ diff --git a/inet/myip b/inet/myip new file mode 100755 index 0000000..1d777c3 --- /dev/null +++ b/inet/myip @@ -0,0 +1,3 @@ +#!/usr/bin/bash +ip addr | grep -Po '\d+\.\d+\.\d+\.\d+/24' | awk '{ print substr($1, 0, length($1) - 3) }' + diff --git a/inet/weather b/inet/weather new file mode 100755 index 0000000..9a93d6d --- /dev/null +++ b/inet/weather @@ -0,0 +1,6 @@ +#!/usr/bin/bash + +location="$(cat ~/.config/weather_location)" + +curl "https://wttr.in/$location?0mQ" + diff --git a/mountain/mountain b/mountain/mountain new file mode 100755 index 0000000..80b8015 --- /dev/null +++ b/mountain/mountain @@ -0,0 +1,36 @@ +#!/usr/bin/bash + +TEXT=$(/usr/lib/mountain/mountain-handle $1) +CODE=$? + +if [[ $CODE -eq 1 ]]; then + echo "mountain: no device supplied" + exit 1 +elif [[ $CODE -eq 2 ]]; then + echo "mountain: no settings for \"$1\"" + exit 2 +fi + +read -t 1 -ra PROP <<< $TEXT +DEV=${PROP[0]} +DIR=${PROP[1]} + +mount | grep "$DEV" >> /dev/null +EXISTS=$? + +if [[ "$2" == "u" ]]; then + if [[ $EXISTS -ne 0 ]]; then + echo "mountain: $DEV is not mounted" + exit 2 + fi + echo "Unmounting $DIR" + sudo umount $DIR + exit 0 +fi + +if [[ $EXISTS -ne 1 ]]; then + echo "mountain: $DEV is already mounted" + exit 2 +fi +echo "Mounting $DEV at $DIR" +sudo mount $DEV $DIR diff --git a/mountain/mountain-handle b/mountain/mountain-handle new file mode 100755 index 0000000..6d83754 --- /dev/null +++ b/mountain/mountain-handle @@ -0,0 +1,24 @@ +#!/usr/bin/python +from configparser import ConfigParser +from os import environ +from os.path import expanduser +from sys import argv + +if len(argv) == 1: + exit(1) + +config = ConfigParser() +CONFIG_PATH = expanduser("~/.config/mountain.ini") +config.read(CONFIG_PATH) +sections = config.sections() + +name = argv[1] +if name not in sections: + exit(2) + +if config.has_option(name, "alias"): + name = config[name]["alias"] + +dev = config[name]["dev"] +dir = config[name]["dir"] +print(dev, dir) diff --git a/run/README.md b/run/README.md new file mode 100644 index 0000000..61ba993 --- /dev/null +++ b/run/README.md @@ -0,0 +1,5 @@ +# run + +Dead-simple script to run `./.run` with all arguments passed through (`$@` in bash). +I mostly use it for build scripts and running games. + diff --git a/run/run b/run/run new file mode 100755 index 0000000..af2a95f --- /dev/null +++ b/run/run @@ -0,0 +1,10 @@ +#!/bin/bash + +if [[ -f ./.run ]]; then + ./.run $@ + exit $? +else + echo "run: ./.run does not exist" + exit 1 +fi +