From 3364af2dbdea4c1d816b8499cf58ce0fa1790a62 Mon Sep 17 00:00:00 2001 From: davidovski Date: Sun, 13 Feb 2022 17:58:39 +0000 Subject: renamed shell scripts to .sh --- Makefile | 15 ++++---- src/parseconf | 106 ------------------------------------------------------- src/parseconf.sh | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/xichroot | 58 ------------------------------ src/xichroot.sh | 58 ++++++++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 172 deletions(-) delete mode 100755 src/parseconf create mode 100755 src/parseconf.sh delete mode 100755 src/xichroot create mode 100755 src/xichroot.sh diff --git a/Makefile b/Makefile index 4836466..4c2ad4c 100755 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ PREFIX=/usr .DEFAULT_GOAL := build -install: install-hbar install-colors install-parseconf +install: install-hbar install-colors install-parseconf install-shtests build: build-hbar install-headers: src/*.h @@ -16,13 +16,10 @@ install-shtests: src/shtests install -m755 src/shtests.sh ${DESTDIR}${PREFIX}/bin/shtests install-chroot: src/xichroot - install -m755 src/xichroot ${DESTDIR}${PREFIX}/bin/ + install -m755 src/xichroot.sh ${DESTDIR}${PREFIX}/bin/xichroot install-parseconf: src/parseconf - install -m755 src/parseconf ${DESTDIR}${PREFIX}/bin/ - -check-parseconf: - shtests ./test/parseconf.sh + install -m755 src/parseconf.sh ${DESTDIR}${PREFIX}/bin/parseconf install-hbar: build-hbar install -m755 bin/hbar ${DESTDIR}${PREFIX}/bin @@ -30,11 +27,13 @@ install-hbar: build-hbar install-colors: src/colors.list sh src/generate_colors.sh ${DESTDIR}${PREFIX} src/colors.list +check-parseconf: + shtests ./test/parseconf.sh -clean: - rm -r bin build-hbar: src/hbar.c install-colors mkdir -pv bin ${CC} src/hbar.c -o bin/hbar ${FLAGS} +clean: + rm -r bin diff --git a/src/parseconf b/src/parseconf deleted file mode 100755 index c5ed801..0000000 --- a/src/parseconf +++ /dev/null @@ -1,106 +0,0 @@ -#!/bin/bash - -usage () { - printf "Usage $0 " - echo << "EOF" -OPTIONS... [FILTER] - -Print the parsed config file filtering by the keys -Arguments: - -f file read configuration from a file, uses /dev/sdtin otherwise - -v only print values - -c n print the last [n] -EOF -} - -# print the parsed values from the config file in key:value format -# -parse () { - local file="$1" - local level="" - local list="" - while IFS= read -r line; do - line=$(sed "s/\s\+/ /g" <<< "$line" | sed "s/^\s\|\s$\|;*$//g") - - grep -q "^#" <<< "$line" && continue - grep -q "." <<< "$line" || continue - - local key=$(echo $line | cut -d" " -f1) - local value=$(echo $line | cut -d" " -f2-) - - [ "$key" = "include" ] && parse $value && continue - - case ${value: -1} in - "{") - level="$level$key." - ;; - "[") - list="$list$key." - printf "$level$key:" - ;; - "}") - level=$(sed "s/[^\.]\w*\.$//g" <<< "$level") - ;; - "]") - printf "\n" - list=$(sed "s/[^\.]\w*\.$//g" <<< "$list") - ;; - *) - - grep -q "." <<< "$list" && - printf "$line " || - printf "$level$key:$value\n" - ;; - esac - done < "$file" -} - -# Filter the parsed file for specific keys -# -filter () { - local pattern= - - [ $# = 0 ] && - pattern=".*" || - pattern=$(sed "s/\*/.*/g"<<< "$@") - - $print_keys && - pattern="s/^($pattern:.+)/\1/p" || - pattern="s/^$pattern:(.+)/\1/p" - - - parse $CONF_FILE | sed -rn $pattern -} - -# Use the env variable if exists -[ -z ${CONF_FILE} ] && CONF_FILE="/dev/stdin" - -# initialise options -print_keys=true -count= - -while getopts ":f:c:v" opt; do - case "${opt}" in - f) - [ "${OPTARG}" = "-" ] && - CONF_FILE="/dev/stdin" || - CONF_FILE="${OPTARG}" - ;; - - v) - print_keys=false - ;; - c) - count="${OPTARG}" - ;; - *) - esac -done - -shift $((OPTIND-1)) - -[ -z ${count} ] && - filter "$@" || - filter "$@" | tail -n $count - - diff --git a/src/parseconf.sh b/src/parseconf.sh new file mode 100755 index 0000000..c5ed801 --- /dev/null +++ b/src/parseconf.sh @@ -0,0 +1,106 @@ +#!/bin/bash + +usage () { + printf "Usage $0 " + echo << "EOF" +OPTIONS... [FILTER] + +Print the parsed config file filtering by the keys +Arguments: + -f file read configuration from a file, uses /dev/sdtin otherwise + -v only print values + -c n print the last [n] +EOF +} + +# print the parsed values from the config file in key:value format +# +parse () { + local file="$1" + local level="" + local list="" + while IFS= read -r line; do + line=$(sed "s/\s\+/ /g" <<< "$line" | sed "s/^\s\|\s$\|;*$//g") + + grep -q "^#" <<< "$line" && continue + grep -q "." <<< "$line" || continue + + local key=$(echo $line | cut -d" " -f1) + local value=$(echo $line | cut -d" " -f2-) + + [ "$key" = "include" ] && parse $value && continue + + case ${value: -1} in + "{") + level="$level$key." + ;; + "[") + list="$list$key." + printf "$level$key:" + ;; + "}") + level=$(sed "s/[^\.]\w*\.$//g" <<< "$level") + ;; + "]") + printf "\n" + list=$(sed "s/[^\.]\w*\.$//g" <<< "$list") + ;; + *) + + grep -q "." <<< "$list" && + printf "$line " || + printf "$level$key:$value\n" + ;; + esac + done < "$file" +} + +# Filter the parsed file for specific keys +# +filter () { + local pattern= + + [ $# = 0 ] && + pattern=".*" || + pattern=$(sed "s/\*/.*/g"<<< "$@") + + $print_keys && + pattern="s/^($pattern:.+)/\1/p" || + pattern="s/^$pattern:(.+)/\1/p" + + + parse $CONF_FILE | sed -rn $pattern +} + +# Use the env variable if exists +[ -z ${CONF_FILE} ] && CONF_FILE="/dev/stdin" + +# initialise options +print_keys=true +count= + +while getopts ":f:c:v" opt; do + case "${opt}" in + f) + [ "${OPTARG}" = "-" ] && + CONF_FILE="/dev/stdin" || + CONF_FILE="${OPTARG}" + ;; + + v) + print_keys=false + ;; + c) + count="${OPTARG}" + ;; + *) + esac +done + +shift $((OPTIND-1)) + +[ -z ${count} ] && + filter "$@" || + filter "$@" | tail -n $count + + diff --git a/src/xichroot b/src/xichroot deleted file mode 100755 index 7136e25..0000000 --- a/src/xichroot +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/bash -# This script is intended for root users to automatically mount requried dirs and chroot into an installed system -# simplifed version of arch-chroot from - -args=$@ -target=$1 - -die() { printf "$@\n"; exit 1;} - -ignore_error() { - "$@" 2>/dev/null - return 0 -} - -chroot_add_mount() { - mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}") -} - -chroot_maybe_add_mount() { - local cond=$1; shift - if eval "$cond"; then - chroot_add_mount "$@" - fi -} - -chroot_setup() { - CHROOT_ACTIVE_MOUNTS=() - [[ $(trap -p EXIT) ]] && die '(BUG): attempting to overwrite existing EXIT trap' - trap 'chroot_teardown' EXIT - - chroot_add_mount proc "$target/proc" -t proc -o nosuid,noexec,nodev - chroot_add_mount sys "$target/sys" -t sysfs -o nosuid,noexec,nodev,ro - ignore_error chroot_maybe_add_mount "[[ -d '$1/sys/firmware/efi/efivars' ]]" \ - efivarfs "$target/sys/firmware/efi/efivars" -t efivarfs -o nosuid,noexec,nodev - chroot_add_mount udev "$target/dev" -t devtmpfs -o mode=0755,nosuid - chroot_add_mount devpts "$target/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec - chroot_add_mount shm "$target/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev - chroot_add_mount /run "$target/run" --bind - chroot_add_mount tmp "$target/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid -} - -chroot_teardown() { - if (( ${#CHROOT_ACTIVE_MOUNTS[@]} )); then - umount "${CHROOT_ACTIVE_MOUNTS[@]}" - fi - unset CHROOT_ACTIVE_MOUNTS -} - -[ "$EUID" -eq 0 ] || die "Please run this as root" -[[ -d $target ]] || die "$target is not a directory; cannot chroot!" - -chroot_setup $@ - -if [ $# -gt 1 ]; then - chroot $@ -else - chroot $@ /bin/sh -l -fi diff --git a/src/xichroot.sh b/src/xichroot.sh new file mode 100755 index 0000000..7136e25 --- /dev/null +++ b/src/xichroot.sh @@ -0,0 +1,58 @@ +#!/bin/bash +# This script is intended for root users to automatically mount requried dirs and chroot into an installed system +# simplifed version of arch-chroot from + +args=$@ +target=$1 + +die() { printf "$@\n"; exit 1;} + +ignore_error() { + "$@" 2>/dev/null + return 0 +} + +chroot_add_mount() { + mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}") +} + +chroot_maybe_add_mount() { + local cond=$1; shift + if eval "$cond"; then + chroot_add_mount "$@" + fi +} + +chroot_setup() { + CHROOT_ACTIVE_MOUNTS=() + [[ $(trap -p EXIT) ]] && die '(BUG): attempting to overwrite existing EXIT trap' + trap 'chroot_teardown' EXIT + + chroot_add_mount proc "$target/proc" -t proc -o nosuid,noexec,nodev + chroot_add_mount sys "$target/sys" -t sysfs -o nosuid,noexec,nodev,ro + ignore_error chroot_maybe_add_mount "[[ -d '$1/sys/firmware/efi/efivars' ]]" \ + efivarfs "$target/sys/firmware/efi/efivars" -t efivarfs -o nosuid,noexec,nodev + chroot_add_mount udev "$target/dev" -t devtmpfs -o mode=0755,nosuid + chroot_add_mount devpts "$target/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec + chroot_add_mount shm "$target/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev + chroot_add_mount /run "$target/run" --bind + chroot_add_mount tmp "$target/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid +} + +chroot_teardown() { + if (( ${#CHROOT_ACTIVE_MOUNTS[@]} )); then + umount "${CHROOT_ACTIVE_MOUNTS[@]}" + fi + unset CHROOT_ACTIVE_MOUNTS +} + +[ "$EUID" -eq 0 ] || die "Please run this as root" +[[ -d $target ]] || die "$target is not a directory; cannot chroot!" + +chroot_setup $@ + +if [ $# -gt 1 ]; then + chroot $@ +else + chroot $@ /bin/sh -l +fi -- cgit v1.2.1