From cc319fa9944a1793436ffec265fceab6662ef235 Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Sun, 28 Nov 2021 18:29:51 +0100 Subject: [PATCH] Initial upload --- README.md | 140 ++++++++++++++++++++++++++++++++++++ battery-warn | 21 ++++++ check-system-update | 48 +++++++++++++ hotspot | 68 ++++++++++++++++++ mount-nextcloud | 28 ++++++++ mount-nextcloud.desktop | 5 ++ perform-system-update.patch | 5 ++ update-cargo-clif | 27 +++++++ 8 files changed, 342 insertions(+) create mode 100644 README.md create mode 100755 battery-warn create mode 100755 check-system-update create mode 100755 hotspot create mode 100755 mount-nextcloud create mode 100644 mount-nextcloud.desktop create mode 100644 perform-system-update.patch create mode 100755 update-cargo-clif diff --git a/README.md b/README.md new file mode 100644 index 0000000..deb0289 --- /dev/null +++ b/README.md @@ -0,0 +1,140 @@ +# Some useful bash scripts +Here are some little bash scripts I coded along the way. + +You can install all of them or just some individually. Be sure to check the script description in this README for additional instructions. (This could be critical. Please, +read them.) + +Generally, all of these scripts can be copied into your user's binary folder: +```shell +$ cp script_name ~/.local/bin/ +$ chmod +x ~/.local/bin/script_name +``` +You can check whether your shell looks for binaries in this path by running: +```shell +$ echo $PATH | grep ~/.local/bin +``` +If there's text, you're good to go. If not, edit your shell's init file and add the following line: +```sh +export PATH=~/.local/bin:$PATH +``` + +## Table of contents +- battery-warn +- hotspot +- update-cargo-clif +- mount-nextcloud +- check-system-update + +## battery-warn +> Periodically query the battery level and play a sound if the level is low. + +**This script does not provide a help text!** + +I originally coded this because my display driver broke and I only had the console. Unfortunately the console doesn't come with any way to notify me when my battery is dying. + +You can run `battery-warn` in the background: +```shell +$ battery-warn & +``` + +It would be useful to add this line to your shell's init file if you need it regularly. + +To check the battery level yourself run this: +```shell +$ battery-warn check +``` + +## hotspot +> Quickly start a wifi hotspot using NetworkManager. + +**This script does not provide a help text!** + +Starting/stopping the simple hotspot is as easy as: +```shell +$ hotspot +``` +This will also show the SSID, password, and a QR-code. + +In order to reduce power consumption it will also disable Wifi when disabling the hotspot. This behavior can be changed inside of the script (`turn_off_wifi=0`). Or you can +only turn the hotspot off manually: +```shell +$ hotspot off +``` + +If you want to be sneaky, you can start a hidden hotspot with a different randomly generated password each time: +```shell +$ hotspot random +``` + +To show the SSID, password, and QR-code again without changing the hotspot: +```shell +$ hotspot show +``` + +## update-cargo-clif +> Download and install the latest build artifact for `rustc_codegen_cranelift`. + +**This script does not provide a help text!** + +This script also needs `jq` to be installed. + +Before using this script be sure to replace the `$username` and `$access_token` variables with your own GitHub username and an [access +token](https://github.com/settings/tokens/) with scope `workflow`. (This will also enable the scope `repo`.) + +After the initial setup, run without arguments to download the latest GitHub Action build artifact for linux and install to /opt/cargo-clif: +```shell +$ update-cargo-clif +``` +It will symlink the new `cargo` command to `/usr/local/bin/cargo-clif` (i.e. use with `cargo-clif` when building your projects). + +The script uses `sudo` internally, so there is no need to run `sudo update-cargo-clif`. + +## mount-nextcloud +> Mount your Nextcloud account automatically using `gio` and `pinentry`. + +**This script does not provide a help text!** + +Before using this script be sure to replace the `$server` and `$user` variables with your Nextcloud server and account. + +If you are not using GNOME, replace `pinentry-gnome3` with the correct `pinentry` binary for your desktop environment. + +You _could_ run `mount-nextcloud` manually (e.g. without a graphical UI in the console): +```shell +$ mount-nextcloud +``` +But I recommend installing the autostart file. Assuming you are in the directory where you downloaded this repo: +```shell +$ cp mount-nextcloud.desktop ~/.config/autostart/ +``` + +## check-system-update +> Update AUR and Flatpak packages directly and install Arch Linux packages from the repos at next boot. + +**This script does not provide a help text!** + +This script only works on distributions based on Arch Linux. You will also need to install `systemd-system-update-pacman` from the AUR (e.g. with `yay`): +```shell +$ yay -S systemd-system-update-pacman +``` + +Because the script downloads the packages into a special cache directory, you have to patch the updater script from that package. Assuming you are in the directory where +you downloaded this repo: +```shell +$ sudo patch /usr/lib/systemd-system-update-pacman/perform-system-update{,.patch} +``` + +To check for any updates - be it from the AUR, Flatpak, or the repos - run: +```shell +$ check-system-update +``` +This will update AUR packages and Flatpaks directly (i.e. now), and repo packages later at the next time you boot up you system, so you can go on with your work like nothing +happened. If you have any critical system packages (e.g. drivers, kernels, etc.) installed from the AUR, installing them now could be a problem though. In order to check +which packages come from non-repo sources, run: +```shell +$ pacman -Qm +``` + +The script uses `sudo` internally, so there is no need to run `sudo check-system-update`. + +## Contribution +Issues and Pull Requests are welcome. Please describe your problems or feature requests as detailed as possible. diff --git a/battery-warn b/battery-warn new file mode 100755 index 0000000..4de14e4 --- /dev/null +++ b/battery-warn @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +level() { + busctl --system get-property org.freedesktop.UPower /org/freedesktop/UPower/devices/battery_BAT0 org.freedesktop.UPower.Device Percentage | sed "s/d //" +} + +icon-name() { + busctl --system get-property org.freedesktop.UPower /org/freedesktop/UPower/devices/battery_BAT0 org.freedesktop.UPower.Device IconName | sed -E "s/(s )|\"//g" +} + +if [ "$1" = "check" ]; then + echo $(level)% + exit +fi + +while true; do + icon_name=$(icon-name) + if echo $icon_name | grep -qv charging && echo $icon_name | grep -q caution; then + ffplay /usr/share/sounds/freedesktop/stereo/dialog-warning.oga -autoexit -nodisp -hide_banner -v -8 >&- 2>&- + fi + sleep 30 +done diff --git a/check-system-update b/check-system-update new file mode 100755 index 0000000..7a99e86 --- /dev/null +++ b/check-system-update @@ -0,0 +1,48 @@ +#!/usr/bin/bash +yay -Syua + +echo "==> Updating flatpaks..." +flatpak update + +echo "==> Syncing database..." +sudo pacman -Sy --noconfirm || exit 1 + +function update_init_fail { + echo "==> Initialising system update failed." >&2 + echo "==> Cleaning up..." >&2 + sudo rm -rf /var/lib/system-update + exit 1 +} + +if pacman -Qu >&-; then + echo "==> System updates available." + echo "==> Downloading system updates..." + if [ ! -e /var/lib/system-update ]; then + sudo mkdir -p /var/lib/system-update || update_init_fail + fi + sudo pacman -Suw --noconfirm --cachedir /var/lib/system-update || update_init_fail + sudo ln -s /var/lib/system-update /system-update || update_init_fail + echo "==> System update scheduled for next reboot." +else + echo "==> Up to date." + if [ -e /system-update ]; then + echo "==> There is another system update pending." + while true; do + read -n1 -p"==> Unschedule system update? [Y/n]: " q + [ ! $q = "" ] && echo + case $q in + y|Y|"") + sudo rm /system-update || { + echo "==> Could not unschedule pending system update" >&2 + exit 1 + } + echo "==> Unscheduled pending system update" + break;; + n|N) + break;; + *) + continue;; + esac + done + fi +fi diff --git a/hotspot b/hotspot new file mode 100755 index 0000000..117742c --- /dev/null +++ b/hotspot @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +# Change this to 0 if you don't want your Wifi turned off when turning off the hotspot +turn_off_wifi=1 + +hotspot=Hotspot +if [ "$1" = "random" ]; then + hotspot=RandomHotspot + declare -i pwbits=256 + if [ -n "$2" ]; then + pwbits=$2 + fi +elif [ "$1" = "off" ]; then + if [ -n "$(nmcli -g GENERAL.STATE c s Hotspot)" ]; then + nmcli c do Hotspot + elif [ -n "$(nmcli -g GENERAL.STATE c s RandomHotspot)" ]; then + nmcli c do RandomHotspot + fi + exit +elif [ "$1" = "show" ]; then + nmcli d w s + exit +fi + +random_hotspot_on() { + nmcli -p d w ho con-name RandomHotspot ssid "We never met, ok?" password $(pwmake $pwbits) + nmcli c mod RandomHotspot 802-11-wireless.hidden true + nmcli d w s +} + +hotspot_on() { + nmcli -p d w ho + nmcli d w s +} + +# Wifi on? +if nmcli r wi | grep enabled -q; then + # yes + + # hotspot on? + if [[ -n "$(nmcli -g GENERAL.STATE c s $hotspot)" && "$hotspot" != "RandomHotspot" ]]; then + echo Turning off Wifi and hotspot + # yes, turn off hotspot (and Wifi) + if [ $turn_off_wifi -eq 0 ]; then + nmcli c do $hotspot + else + nmcli r wi off + fi + else + echo Turning on hotspot + # no, turn on hotspot + if [ "$hotspot" = "RandomHotspot" ]; then + random_hotspot_on + else + hotspot_on + fi + fi +else + echo Turning on Wifi and hotspot + # no + nmcli r wi on + # hotspot neither + if [ "$hotspot" = "RandomHotspot" ]; then + random_hotspot_on + else + hotspot_on + fi +fi diff --git a/mount-nextcloud b/mount-nextcloud new file mode 100755 index 0000000..1d4c353 --- /dev/null +++ b/mount-nextcloud @@ -0,0 +1,28 @@ +#!/bin/bash +user=ExampleUser +server=cloud.example.com +account=$user@$server +uri=davs://$account/remote.php/webdav +uri_esc=$(echo $uri | sed s/\\//\\\\\\//g) +online=$(gio mount -l | sed "/^Mount/! d;/$uri_esc/! d" | wc -l) +echo $online +if [ $online -gt 0 ]; then + exit +fi + +case $XDG_SESSION_TYPE in + tty) + gio mount $uri /dev/tty1 2>&1 +--- +> /usr/bin/pacman -Su --noconfirm --cachedir /var/lib/system-update --color=auto > /dev/tty1 2>&1 +> /usr/bin/rm -rf /var/lib/system-update diff --git a/update-cargo-clif b/update-cargo-clif new file mode 100755 index 0000000..1d4f28e --- /dev/null +++ b/update-cargo-clif @@ -0,0 +1,27 @@ +#!/usr/bin/bash + +# Your GitHub username and access token +username="" +access_token="" + +artifact="$(curl https://api.github.com/repos/bjorn3/rustc_codegen_cranelift/actions/artifacts | jq -c ".artifacts|map(select(.name==\"cg_clif-Linux\"))[0]")" +id="$(echo "$artifact" | jq .id)" +expired="$(echo "$artifact" | jq .expired)" + +if [ "$(cat /opt/cargo-clif/artifact-id 2>&-)" = "$id" ]; then + echo cargo-clif is up to date + exit +elif [ "$expired" = "true" ]; then + echo New version is already expired >&2 + false + exit +else + sudo mkdir -p /opt/cargo-clif + sudo bash -c "echo $id > /opt/cargo-clif/artifact-id" +fi + +sudo rm -rf /opt/cargo-clif/lib/librustc_std* /opt/cargo-clif/lib/rustlib/x86_64-unknown-linux-gnu/lib/* +curl -Lu$username:$access_token "$(echo "$artifact" | jq -r .archive_download_url)" | funzip | sudo tar xJC /opt/cargo-clif --strip-components 1 +if [ ! -e /usr/local/bin/cargo-clif ]; then + sudo ln -s /opt/cargo-clif/cargo /usr/local/bin/cargo-clif +fi