From 2e84e05fe5a8fc8aaed11c96024028a8c827fcce Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 6 Jul 2023 11:07:16 -0400 Subject: [PATCH] initial commit; repository moved --- README.md | 37 ++++++++++++++++++ banana-runner/.banana | 7 ++++ banana-runner/banana-runner.py | 69 +++++++++++++++++++++++++++++++++ banana-runner/banana-runner.sh | 17 ++++++++ goto/README.md | 15 +++++++ goto/goto.sh | 19 +++++++++ goto/resolve | 35 +++++++++++++++++ inet/README.md | 14 +++++++ inet/is-back | 12 ++++++ inet/is-back-icon.png | Bin 0 -> 3441 bytes inet/myip | 3 ++ inet/weather | 6 +++ mountain/mountain | 36 +++++++++++++++++ mountain/mountain-handle | 24 ++++++++++++ run/README.md | 5 +++ run/run | 10 +++++ 16 files changed, 309 insertions(+) create mode 100644 README.md create mode 100644 banana-runner/.banana create mode 100755 banana-runner/banana-runner.py create mode 100644 banana-runner/banana-runner.sh create mode 100644 goto/README.md create mode 100755 goto/goto.sh create mode 100755 goto/resolve create mode 100644 inet/README.md create mode 100755 inet/is-back create mode 100644 inet/is-back-icon.png create mode 100755 inet/myip create mode 100755 inet/weather create mode 100755 mountain/mountain create mode 100755 mountain/mountain-handle create mode 100644 run/README.md create mode 100755 run/run 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 0000000000000000000000000000000000000000..d2f5f8376825b7388b0b16bb84eccd1a40e508ce GIT binary patch literal 3441 zcmb7`_dnEu1I9l$+#Sxyh?GqvE0lfia+E|T^Rhy6Qs@#6aqdJ!*%EO^6d~CwGh}6y zlRYxC_xA1kAAG+*JkR^}dS1_O&o56X&hQ!w6CV=*04#dfvA6$`{9hqp|GW`p*A4(6 zS7&W)oU?-i07ynXjjGYVYQ)ATv&(S<08WMBY&mS3(!l%0*)TO zVkHENk-n|=2__HYf??;vVD(SXw)(A0no^Q!v&7|tDb-n$>a6Nrw&~*P-xDEzX#^|< zv_pLOZDh+(xv_k;SCD@5nd~{%9?wG!d*iM8Y@d=bI&t&7aTcu9b;8*|Ijo_mA+0k# z-}{y~uDv;?L88G%n^n>;cM}^i#^v5MN6mlsAy($^&d)DWPUGHzf3G&y3afVdy|i&9 zq&T=Q)lC~{>z}LpC5zP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H-!f2wXSloG+*jE`^_Dqs#~sXDr=0hEc3n+SW65r8_mt$= z+(-_V!L3|tD{D{wm`Kyhzu?chNAG_mNK-!DUSY;NopmleH*DOIyF@Qe5PbKt>!EvS zcChDCzjw?fu2tE|J7V-Va}ULkb!^JMs)>KBeZPj^(%2;(_!!;|b9@7rFxkGGe8*!1 z0w9|~W}(Mb8_Ue4@+%-zF`6=B-zt>CdYIf2yyA0YU14F=6cIL}Sc*m*)z< ze#n`bF%6)r2a+yCo3eU}oO$iV+DZ>OV_e8w=3@4&-7J4y8wjuW+T9^ROSMeGTh#J$ z#Gu{GpAbIyXQ@LBV~?Jyv*GrHY16#&$l6wZi2deg8Dermu^}`<$8951Gc1@ad>6qB g{y%_bVBZxK92&lqv6eIU@5uvtI)>Q%t9L2?0bi?6=>Px# literal 0 HcmV?d00001 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 +