diff --git a/utils/.local/bin/clock b/utils/.local/bin/clock index 82ca65b..c584baf 100755 --- a/utils/.local/bin/clock +++ b/utils/.local/bin/clock @@ -1,3 +1,17 @@ #!/usr/bin/env bash +set -eo pipefail -watch -tn 1 date '+%l:%M:%S%p' +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat </dev/null; then - exec pbcopy -elif hash xclip 2>/dev/null; then - exec xclip -selection clipboard -elif hash putclip 2>/dev/null; then - exec putclip -else - rm -f /tmp/clipboard 2> /dev/null - if [ $# -eq 0 ]; then - cat > /tmp/clipboard - else - cat "$1" > /tmp/clipboard - fi +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat </dev/null; then + exec pbcopy +elif hash xclip 2>/dev/null; then + exec xclip -selection clipboard +elif hash putclip 2>/dev/null; then + exec putclip +else + rm -f /tmp/clipboard 2> /dev/null + if [ $# -eq 0 ]; then + cat > /tmp/clipboard + else + cat "$1" > /tmp/clipboard + fi fi diff --git a/utils/.local/bin/curlstat b/utils/.local/bin/curlstat index 5576130..c682c42 100755 --- a/utils/.local/bin/curlstat +++ b/utils/.local/bin/curlstat @@ -1,6 +1,22 @@ #!/usr/bin/env bash set -eo pipefail +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Display detailed curl statistics for a URL. + +Options: + -h, --help Show this help message + +Arguments: + url URL to fetch statistics for + +EOF + exit 0 +fi + curl -sLw @- -o /dev/null "$@" <<'EOF' URL:\t\t\t%{url}\n Address:\t\t%{remote_ip}:%{remote_port}\n diff --git a/utils/.local/bin/extract b/utils/.local/bin/extract index bd4a1dd..ef24b7f 100755 --- a/utils/.local/bin/extract +++ b/utils/.local/bin/extract @@ -1,10 +1,13 @@ #!/usr/bin/env bash set -eo pipefail -if [ -z "$1" ]; then - echo "Usage: extract ." - echo " extract [path/file_name_2.ext] [path/file_name_3.ext]" - exit 1 +if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then + echo "Usage: $(basename "$0") ." + echo " $(basename "$0") [path/file_name_2.ext] [path/file_name_3.ext]" + echo + echo "Options:" + echo " -h, --help Show this help message" + exit 0 fi for n in "$@"; do diff --git a/utils/.local/bin/flushdns b/utils/.local/bin/flushdns index 0b3aac3..9997828 100755 --- a/utils/.local/bin/flushdns +++ b/utils/.local/bin/flushdns @@ -1,3 +1,17 @@ #!/usr/bin/env bash +set -eo pipefail + +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Check if a command is available. + +Options: + -h, --help Show this help message + +Arguments: + command Command to check + +Returns 0 if command exists, 1 otherwise. + +EOF + exit 0 +fi + [ -n "$1" ] && command -v "$1" || exit 1 diff --git a/utils/.local/bin/len b/utils/.local/bin/len index ddb062b..8a58a76 100755 --- a/utils/.local/bin/len +++ b/utils/.local/bin/len @@ -1,4 +1,24 @@ #!/usr/bin/env bash set -eo pipefail -echo -n "$@" | wc -c | awk '{print $1}' +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Display length of a string. + +Options: + -h, --help Show this help message + +Arguments: + string String to measure + +EOF + exit 0 +fi + +if [[ ! -t 0 ]]; then # Read from stdin (pipe) + wc -c | awk '{print $1}' +else # Read from arguments + echo -n "$@" | wc -c | awk '{print $1}' +fi diff --git a/utils/.local/bin/line b/utils/.local/bin/line index f9aab2f..c9479ea 100755 --- a/utils/.local/bin/line +++ b/utils/.local/bin/line @@ -1,5 +1,27 @@ #!/usr/bin/env bash set -eo pipefail +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Display a specific line from a file. + +Options: + -h, --help Show this help message + +Arguments: + lineno Line number to display (1-based) + file File to read from + +EOF + exit 0 +fi + lineno="$1"; shift -sed -n "${lineno}p" -- "$@" + +if [[ ! -t 0 ]]; then # Read from stdin (pipe) + sed -n "${lineno}p" +else # Read from file + sed -n "${lineno}p" -- "$@" +fi diff --git a/utils/.local/bin/mkcd b/utils/.local/bin/mkcd index ba7b25f..624f1d9 100755 --- a/utils/.local/bin/mkcd +++ b/utils/.local/bin/mkcd @@ -1,4 +1,21 @@ #!/usr/bin/env bash +set -eo pipefail + +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Create directory and change to it. + +Options: + -h, --help Show this help message + +Arguments: + directory Directory to create and enter + +EOF + exit 0 +fi mkdir -p "$1" -cd "$1" || exit +cd "$1" || false diff --git a/utils/.local/bin/mksh b/utils/.local/bin/mksh index 3fc234e..ff3f677 100755 --- a/utils/.local/bin/mksh +++ b/utils/.local/bin/mksh @@ -1,21 +1,35 @@ #!/usr/bin/env bash set -eo pipefail -if [ ! $# -eq 1 ]; then - echo 'mksh takes one argument' 1>&2 - exit 1 -elif [ -e "$1" ]; then - echo "$1 already exists" 1>&2 - exit 1 +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Create a new bash script with boilerplate. + +Options: + -h, --help Show this help message + +Arguments: + script Script filename to create + +EOF + exit 0 fi -echo '#!/usr/bin/env bash -set -e -set -u -set -o pipefail +if [ ! $# -eq 1 ]; then + echo 'mksh takes one argument' 1>&2 + exit 1 +elif [ -e "$1" ]; then + echo "$1 already exists" 1>&2 + exit 1 +fi -' > "$1" +{ + echo '#!/usr/bin/env bash' + echo 'set -eo pipefail' + echo +} > "$1" chmod u+x "$1" - -nano "$1" +exec "$EDITOR" "$1" diff --git a/utils/.local/bin/myip b/utils/.local/bin/myip index 6b8412b..4cdd77c 100755 --- a/utils/.local/bin/myip +++ b/utils/.local/bin/myip @@ -1,4 +1,22 @@ #!/usr/bin/env bash +set -eo pipefail -curl http://ipecho.net/plain -echo +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Change ownership of files to current user. + +Options: + -h, --help Show this help message + +Arguments: + files Files or directories to change ownership + +EOF + exit 0 +fi + sudo chown "$(whoami)". -R --changes --preserve-root "$@" diff --git a/utils/.local/bin/pasta b/utils/.local/bin/pasta index 586b241..2c05d08 100755 --- a/utils/.local/bin/pasta +++ b/utils/.local/bin/pasta @@ -1,12 +1,25 @@ #!/usr/bin/env bash set -eo pipefail -if hash pbpaste 2>/dev/null; then - exec pbpaste -elif hash xclip 2>/dev/null; then - exec xclip -selection clipboard -o -elif [[ -e /tmp/clipboard ]]; then - exec cat /tmp/clipboard -else - echo '' +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat </dev/null; then + exec pbpaste +elif hash xclip 2>/dev/null; then + exec xclip -selection clipboard -o +elif [[ -e /tmp/clipboard ]]; then + exec cat /tmp/clipboard +else + echo '' fi diff --git a/utils/.local/bin/perm b/utils/.local/bin/perm index 64d8a3a..2787e5c 100755 --- a/utils/.local/bin/perm +++ b/utils/.local/bin/perm @@ -1,4 +1,22 @@ #!/usr/bin/env bash +set -eo pipefail + +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat </dev/null; then - exec php -S "localhost:$port" + exec php -S "localhost:$port" elif hash python3 2>/dev/null; then - exec python3 -m http.server "$port" + exec python3 -m http.server "$port" elif hash python 2>/dev/null; then - major_version="$(python -c 'import platform as p;print(p.python_version_tuple()[0])')" - if [[ "$major_version" == '3' ]]; then - exec python -m http.server "$port" - else - exec python -m SimpleHTTPServer "$port" - fi + major_version="$(python -c 'import platform as p;print(p.python_version_tuple()[0])')" + if [[ "$major_version" == '3' ]]; then + exec python -m http.server "$port" + else + exec python -m SimpleHTTPServer "$port" + fi else - echo 'unable to start HTTP server' 1>&2 - exit 1 + echo 'unable to start HTTP server' 1>&2 + exit 1 fi diff --git a/utils/.local/bin/timer b/utils/.local/bin/timer index 6875d4b..49ed3e4 100755 --- a/utils/.local/bin/timer +++ b/utils/.local/bin/timer @@ -1,6 +1,22 @@ #!/usr/bin/env bash set -eo pipefail +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Set a timer with desktop notification. + +Options: + -h, --help Show this help message + +Arguments: + seconds Timer duration in seconds + +EOF + exit 0 +fi + sleep "$1" notify-send 'Timer complete!' \ -u normal \ diff --git a/utils/.local/bin/today b/utils/.local/bin/today new file mode 100755 index 0000000..926cbae --- /dev/null +++ b/utils/.local/bin/today @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -eo pipefail + +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Move files to trash using gio. + +Options: + -h, --help Show this help message + +Arguments: + files Files or directories to move to trash + +EOF + exit 0 +fi gio trash "$@" diff --git a/utils/.local/bin/tryna b/utils/.local/bin/tryna index 813be3d..eca180c 100755 --- a/utils/.local/bin/tryna +++ b/utils/.local/bin/tryna @@ -1,8 +1,24 @@ -#!/usr/bin/env bash -set -u +#!/husr/bin/env bash +set -uo pipefail + +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Retry a command until it fails. + +Options: + -h, --help Show this help message + +Arguments: + command Command to repeatedly execute + +EOF + exit 0 +fi "$@" while [[ "$?" -eq 0 ]]; do - sleep 0.5 - "$@" + sleep 0.5 + "$@" done diff --git a/utils/.local/bin/trynafail b/utils/.local/bin/trynafail index eb127e4..29916b5 100755 --- a/utils/.local/bin/trynafail +++ b/utils/.local/bin/trynafail @@ -1,8 +1,24 @@ #!/usr/bin/env bash -set -u +set -uo pipefail + +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Retry a command until it succeeds. + +Options: + -h, --help Show this help message + +Arguments: + command Command to repeatedly execute + +EOF + exit 0 +fi "$@" while [[ "$?" -ne 0 ]]; do - sleep 0.5 - "$@" + sleep 0.5 + "$@" done diff --git a/utils/.local/bin/unixtime b/utils/.local/bin/unixtime new file mode 100755 index 0000000..c8857ef --- /dev/null +++ b/utils/.local/bin/unixtime @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat </dev/null && { echo echo "===========================" - echo "Upgarding apt packages..." + echo "Upgrading apt packages..." echo "===========================" echo sudo apt update @@ -14,7 +27,7 @@ is apt >/dev/null && { is snap >/dev/null && { echo echo "===========================" - echo "Upgarding snap packages..." + echo "Upgrading snap packages..." echo "===========================" echo sudo snap refresh @@ -23,7 +36,7 @@ is snap >/dev/null && { is flatpak >/dev/null && { echo echo "===========================" - echo "Upgarding flatpak packages..." + echo "Upgrading flatpak packages..." echo "===========================" echo sudo flatpak update -y diff --git a/utils/.local/bin/url2md b/utils/.local/bin/url2md index 6f6a58e..02f5775 100755 --- a/utils/.local/bin/url2md +++ b/utils/.local/bin/url2md @@ -1,12 +1,38 @@ #!/usr/bin/env bash set -eo pipefail -curl "https://r.jina.ai/$1" \ - -sS \ - -H "DNT: 1" \ - -H "X-Base: final" \ - -H "X-Engine: direct" \ - -H "X-Md-Em-Delimiter: *" \ - -H "X-Md-Heading-Style: setext" \ - -H "X-Md-Link-Reference-Style: collapsed" \ - -H "X-Md-Link-Style: referenced" +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Convert webpage URL to markdown using jina.ai. + +Options: + -h, --help Show this help message + +Arguments: + url URL of webpage to convert + +EOF + exit 0 +fi + +# More details: https://jina.ai/reader/ + +url="$1"; shift +curl "https://r.jina.ai/$url" \ + -sS \ + -H "DNT: 1" \ + -H "X-Md-Hr: ---" \ + -H "X-Base: final" \ + -H "X-Timeout: 10" \ + -H "X-Locale: ru-RU" \ + -H "X-No-Cache: true" \ + -H "X-Engine: browser" \ + -H "X-User-Agent: url2md" \ + -H "X-Md-Em-Delimiter: *" \ + -H "X-Md-Link-Style: inlined" \ + -H "X-Return-Format: markdown" \ + -H "X-Keep-Img-Data-Url: true" \ + -H "X-Md-Link-Reference-Style: collapsed" \ + "$@" diff --git a/utils/.local/bin/vcompress b/utils/.local/bin/vcompress index 8ff46ec..750db78 100755 --- a/utils/.local/bin/vcompress +++ b/utils/.local/bin/vcompress @@ -1,4 +1,30 @@ #!/usr/bin/env bash +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Compress MP4 video using ffmpeg. + +Options: + -h, --help Show this help message + +Arguments: + input.mp4 Input video file (must be .mp4) + +Output: + _compressed.mp4 + +EOF + exit 0 +fi + filename="${1%.mp4}" -ffmpeg -i "$filename".mp4 -c:v libx264 -crf 28 -preset veryslow -c:a aac -b:a 128k "$filename"_compressed.mp4 +ffmpeg \ + -i "$filename".mp4 \ + -c:v libx264 \ + -crf 28 \ + -preset veryslow \ + -c:a aac \ + -b:a 128k \ + "$filename"_compressed.mp4 diff --git a/utils/.local/bin/waitfor b/utils/.local/bin/waitfor index afae933..4e49f97 100755 --- a/utils/.local/bin/waitfor +++ b/utils/.local/bin/waitfor @@ -1,11 +1,27 @@ #!/usr/bin/env bash set -eo pipefail -if hash systemd-inhibit 2>/dev/null; then - systemd-inhibit \ - --who=waitfor \ - --why="Awaiting PID $1" \ - tail --pid="$1" -f /dev/null -else - tail --pid="$1" -f /dev/null +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Wait for a process to complete. + +Options: + -h, --help Show this help message + +Arguments: + pid Process ID to wait for + +EOF + exit 0 +fi + +if hash systemd-inhibit 2>/dev/null; then + systemd-inhibit \ + --who=waitfor \ + --why="Awaiting PID $1" \ + tail --pid="$1" -f /dev/null +else + tail --pid="$1" -f /dev/null fi diff --git a/utils/.local/bin/what b/utils/.local/bin/what index 04f00c8..ac4dbf8 100755 --- a/utils/.local/bin/what +++ b/utils/.local/bin/what @@ -1,4 +1,22 @@ #!/usr/bin/env bash +#set -eo pipefail + +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Show detailed information about a command. + +Options: + -h, --help Show this help message + +Arguments: + command Command to investigate + +EOF + exit 0 +fi + set -x type "$1" 2>/dev/null diff --git a/utils/.local/bin/ytmusic b/utils/.local/bin/ytmusic index 33a45ec..7201bb4 100755 --- a/utils/.local/bin/ytmusic +++ b/utils/.local/bin/ytmusic @@ -1,4 +1,5 @@ #!/usr/bin/env bash +set -eo pipefail # Download music from Youtube or Youtube Music # and save as top quality flac file without video @@ -6,6 +7,25 @@ # Usage: $ ytmusic https://www.youtube.com/watch?v=dQw4w9WgXcQ # More info: https://github.com/ytdl-org/youtube-dl +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + cat < + +Download music from YouTube or YouTube Music as FLAC. + +Options: + -h, --help Show this help message + +Arguments: + url YouTube or YouTube Music URL (playlist or video) + +Output: + $HOME/ytmusic// - .flac + +EOF + exit 0 +fi + DEST_PATH="${HOME}/ytmusic" mkdir -p "${DEST_PATH}"