scripts

unixus scriptus
git clone https://s.sonu.ch/~roket1428/scripts.git
Log | Files | Refs

commit 33dbd7ae88f7bc22fe3d06d08359c35b6dd0cec8
parent 700ada53c2db02aa1b94da717e614399d0deed8d
Author: roket1428 <meorhan@protonmail.com>
Date:   Wed,  8 Sep 2021 23:27:15 +0300

cleanups & better error handling

Diffstat:
Maudio | 2+-
Mbat | 8++++----
Mfmc | 57++++++++++++++++++++++++++++++++-------------------------
Mold/cleaner | 4++--
Mold/clockfix | 2+-
5 files changed, 40 insertions(+), 33 deletions(-)

diff --git a/audio b/audio @@ -5,7 +5,7 @@ if [ "$(sndioctl -n output.mute)" -eq "0" ]; then level=$(sndioctl -n output.level) - output=$(echo "100 * $level" | bc -l) + output=$(printf "100 * $level" | bc) printf "%0.f%%\\n" "$output" diff --git a/bat b/bat @@ -2,10 +2,10 @@ # show remaining battery -bat=$(apm -l) +cap=$(apm -l) -if [ "$(apm -b)" -eq "3" ]; then - printf "%s%%+\\n" "$bat" +if [ "$(apm -a)" -eq "1" ]; then + printf "%s%%+\\n" "$cap" else - printf "%s%%\\n" "$bat" + printf "%s%%\\n" "$cap" fi diff --git a/fmc b/fmc @@ -3,7 +3,14 @@ # fmc - ffmpeg music converter # this tool converts multiple music files at once using ffmpeg -# after 231342134123 tries, I finally made it bulletproof (kinda) + +# error handling +die() +{ + printf "%s\\n" "$1" >&2 + exit 1 +} + # check input and change directory ckchdir() @@ -11,39 +18,38 @@ ckchdir() if [ -z "$1" ] then - echo "error: input is blank" - exit 1 + die "error: input is blank" else if [ -f "$1" ]; then - echo "error: input must be a directory" - exit 1 + die "error: input must be a directory" fi - cd "$1" || { echo "error: can't change directory, exiting" && exit 1; } + cd "$1" || die "error: can't change directory, exiting" fi } + # convert files from directory cvdir() { -list=$(find . -name "*.${inputext}" -maxdepth 1 | wc -l) +list=$(find . -name "*.$inputext" -maxdepth 1 | wc -l) line=1 while [ $line -le "$list" ] do ffmpeg -hide_banner -loglevel error -i \ - "$(find . -name "*.${inputext}" -maxdepth 1 | head -${line} | tail +${line} | sed -e 's/\.\///')" \ - "$(find . -name "*.${inputext}" -maxdepth 1 | head -${line} | tail +${line} | sed -e "s/\.\///;s/\.${inputext}$/\.${outputext}/")" + "$(find . -name "*.$inputext" -maxdepth 1 | head -$line | tail +$line | sed -e 's/\.\///')" \ + "$(find . -name "*.$inputext" -maxdepth 1 | head -$line | tail +$line | sed -e "s/\.\///;s/\.${inputext}$/\.${outputext}/")" - printf "converting %s\\n" "$(find . -name "*.${inputext}" -maxdepth 1 | head -${line} | tail +${line} | sed -e 's/\.\///')" + printf "converting %s\\n" "$(find . -name "*.$inputext" -maxdepth 1 | head -$line | tail +$line | sed -e 's/\.\///')" [ "$fillblanks" -eq "1" ] \ && mv \ - "$(find . -name "*.${outputext}" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///')" \ - "$(find . -name "*.${outputext}" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///;s/\ /_/g')" + "$(find . -name "*.$outputext" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///')" \ + "$(find . -name "*.$outputext" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///;s/\ /_/g')" line=$((line + 1)) done @@ -53,9 +59,9 @@ if [ "$removeold" -eq "1" ]; then while [ $rmline -le "$list" ] do - printf "removing %s\\n" "$(find . -name "*.${inputext}" -maxdepth 1 | head -1 | tail +1 | sed -e 's/\.\///')" + printf "removing %s\\n" "$(find . -name "*.$inputext" -maxdepth 1 | head -1 | tail +1 | sed -e 's/\.\///')" - rm "$(find . -name "*.${inputext}" -maxdepth 1 | head -1 | tail +1 | sed -e 's/\.\///')" + rm "$(find . -name "*.$inputext" -maxdepth 1 | head -1 | tail +1 | sed -e 's/\.\///')" rmline=$((rmline + 1)) done fi @@ -67,26 +73,26 @@ fi cvrec() { -list=$(find . -name "*.${inputext}" | wc -l) +list=$(find . -name "*.$inputext" | wc -l) line=1 while [ $line -le "$list" ] do - ffmpeg -hide_banner -loglevel error -y -i \ - "$(find . -name "*.${inputext}" | head -${line} | tail +${line} | sed -e 's/\.\///')" \ - "$(find . -name "*.${inputext}" | head -${line} | tail +${line} | sed -e "s/\.\///;s/\.${inputext}$/\.${outputext}/")" + ffmpeg -hide_banner -loglevel error -i \ + "$(find . -name "*.$inputext" | head -$line | tail +$line | sed -e 's/\.\///')" \ + "$(find . -name "*.$inputext" | head -$line | tail +$line | sed -e "s/\.\///;s/\.${inputext}$/\.${outputext}/")" - printf "converting %s\\n" "$(find . -name "*.${inputext}" | head -${line} | tail +${line} | sed -e 's/\.\///')" + printf "converting %s\\n" "$(find . -name "*.$inputext" | head -$line | tail +$line | sed -e 's/\.\///')" if [ "$fillblanks" -eq "1" ] then - cd "$(find . -name "*.${outputext}" | sed -e 's/\.\///;s#[^/]*$##g' | head -${line} | tail +${line})" || exit 1 + cd "$(find . -name "*.$outputext" | sed -e 's/\.\///;s#[^/]*$##g' | head -$line | tail +$line)" || exit 1 - mv "$(find . -name "*.${outputext}" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///')" \ - "$(find . -name "*.${outputext}" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///;s/\ /_/g')" + mv "$(find . -name "*.$outputext" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///')" \ + "$(find . -name "*.$outputext" -maxdepth 1 | grep '[[:space:]]' | head -1 | tail +1 | sed -e 's/\.\///;s/\ /_/g')" - cd "$rootdir" || exit 1 + cd "$rootdir" || die "error: can't change directory, exiting" fi line=$((line + 1)) done @@ -96,14 +102,15 @@ if [ "$removeold" -eq "1" ]; then while [ $rmline -le "$list" ] do - printf "removing %s\\n" "$(find . -name "*.${inputext}" | head -1 | tail +1 | sed -e 's/\.\///')" + printf "removing %s\\n" "$(find . -name "*.$inputext" | head -1 | tail +1 | sed -e 's/\.\///')" - rm "$(find . -name "*.${inputext}" | head -1 | tail +1 | sed -e 's/\.\///')" + rm "$(find . -name "*.$inputext" | head -1 | tail +1 | sed -e 's/\.\///')" rmline=$((rmline + 1)) done fi } + usage() { cat <<EOF diff --git a/old/cleaner b/old/cleaner @@ -4,7 +4,7 @@ # arch-linux based distros only -{ [ "$(whoami)" != "root" ] && echo "You must be a root user."; } && exit +{ [ "$(whoami)" != "root" ] && echo "You must be a root user." >&2; } && exit 1 output=0 @@ -22,7 +22,7 @@ done if [ "$output" -ge "1" ] then - printf "%s package/s removed.\\n" "$output" + printf "%s package(s) removed.\\n" "$output" else printf "system is clean\\n" fi diff --git a/old/clockfix b/old/clockfix @@ -5,7 +5,7 @@ # visit worldtimeapi.org and change this link according to your location. # check root access -[ "$(whoami)" != "root" ] && echo "You must be a root user." && exit 1 +{ [ "$(whoami)" != "root" ] && echo "You must be a root user." >&2; } && exit 1 # change this curl -so /tmp/datetime http://worldtimeapi.org/api/timezone/Continent/City.txt