Skip to content

Commit

Permalink
test to [[ where available
Browse files Browse the repository at this point in the history
fixes:

- init.sh: fix a missing `-d`
- github-download: fix a missing `-n`
- mount-helper: fix a missing `-n`
- setup-git: fix a missing `test`/`[[`
- waiter: fix missing `-n`

todos:

- note this change of convention in the docs
- see if there is an alternative for the sudo calls
- review the PR to check if there were any errors
  • Loading branch information
balupton committed Oct 28, 2024
1 parent f557548 commit 4627b75
Show file tree
Hide file tree
Showing 513 changed files with 4,438 additions and 4,383 deletions.
40 changes: 20 additions & 20 deletions commands.beta/convert-helper
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ function convert_helper() (
EXAMPLE:
convert-helper --podaudio -- *.wav
EOF
if test "$#" -ne 0; then
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}

# process
local item files=() action='' option_stereo='' option_mono='' option_timestamp='' option_delete='no' option_transcribe='no' option_trim_trailing_silence='no'
while test "$#" -ne 0; do
while [[ $# -ne 0 ]]; do
item="$1"
shift
case "$item" in
Expand Down Expand Up @@ -134,77 +134,77 @@ function convert_helper() (
cd "$dir" || return
filename="$(fs-filename -- "$path")"
extension="$(fs-extension -- "$path")"
if test -n "$option_timestamp"; then
if [[ -n $option_timestamp ]]; then
# prefix filename with timestamp
if test "$option_timestamp" = 'yes'; then
if [[ $option_timestamp == 'yes' ]]; then
filename="$(date-helper --8601 | echo-regexp -g ':' '-') $filename"
else
filename="$(date-helper --8601 | echo-regexp -g ':' '-') $option_timestamp"
fi
cp "$path" "$filename.$extension"
path="$filename.$extension"
fi
if test "$option_trim_trailing_silence" = 'yes'; then
if [[ $option_trim_trailing_silence == 'yes' ]]; then
outfile="$filename [trimmed].m4a"
run ffmpeg -y -i "$path" -af 'silenceremove=stop_periods=-1:stop_duration=0.5:stop_threshold=-60dB' "$outfile"
if test "$option_delete" = 'yes'; then
if [[ $option_delete == 'yes' ]]; then
fs-rm --no-confirm --quiet -- "$path"
fi
filename="$filename [trimmed]"
path="$outfile"
fi
if test "$action" = 'podaudio'; then
if test -z "$option_stereo" -a -z "$option_mono"; then
if [[ $action == 'podaudio' ]]; then
if [[ -z "$option_stereo" && -z "$option_mono" ]]; then
if is-audio-stereo -- "$path"; then
use_stereo='yes'
else
use_mono='yes'
fi
fi
if test "$use_stereo" = 'yes'; then
if [[ $use_stereo == 'yes' ]]; then
outfile="$filename [aac_he_v2] [48k] [stereo].m4a"
run ffmpeg -y -i "$path" -c:a libfdk_aac -profile:a aac_he_v2 -b:a 48k "$outfile"
fi
if test "$use_mono" = 'yes'; then
if [[ $use_mono == 'yes' ]]; then
outfile="$filename [aac_he_v1] [48k] [mono].m4a"
run ffmpeg -y -i "$path" -ac 1 -c:a libfdk_aac -profile:a aac_he -b:a 48k "$outfile"
fi
elif test "$action" = 'podvideo'; then
elif [[ $action == 'podvideo' ]]; then
outfile="$filename [h264].mp4"
run ffmpeg -y -i "$path" -c:v libx264 -c:a libfdk_aac "$outfile"
elif test "$action" = 'alac'; then
elif [[ $action == 'alac' ]]; then
outfile="$filename.m4a"
run ffmpeg -y -vn -i "$path" -c:a alac "$outfile"
elif test "$action" = 'split'; then
elif [[ $action == 'split' ]]; then
run ffmpeg -y -i "$path" -map 0:0 -map 0:2 -c copy "$filename [00+02].mov"
run ffmpeg -y -i "$path" -map 0:1 -map 0:3 -c copy "$filename [01+03].mov"
# elif test "$action" = 'trim'; then
# elif [[ "$action" = 'trim' ]]; then
# # Trim superfluous audio streams from a video file
# help 'trim action not yet finished'
# # run ffmpeg -i "$input" -c copy -an "$output"
# # run ffmpeg -i "$input" -c copy -map 0:v -map "0:a:$stream" "$output"
# elif test "$action" = 'thumbnail'; then
# elif [[ "$action" = 'thumbnail' ]]; then
# help 'thumbnail action not yet finished'
# # outfile="$filename.jpg"
# # run ffmpeg -y -i "$path" -vf "thumbnail" -frames:v 1 "$outfile"
# # run ffmpeg -i "$1" -i "$2" -map 0 -map 1 -c copy -c:v:1 avif -disposition:v:1 attached_pic out.webm
elif test "$action" = 'png'; then
elif [[ $action == 'png' ]]; then
outfile="$filename.png"
run sips -s format png "$path" --out "$outfile"
elif test -n "$action"; then
elif [[ -n $action ]]; then
help 'Unknown action'
fi
if test "$option_transcribe" = 'yes'; then
if [[ $option_transcribe == 'yes' ]]; then
outfile="$filename.srt"
whisper --model base --language English --output_format srt "$path"
fi
if test "$option_delete" = 'yes'; then
if [[ $option_delete == 'yes' ]]; then
fs-rm --no-confirm --quiet -- "$path"
fi
done
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
convert_helper "$@"
fi
4 changes: 2 additions & 2 deletions commands.beta/echo-escape-special
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function echo_escape_special() (
# exit status: 0
EOF
if test "$#" -ne 0; then
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
Expand All @@ -42,6 +42,6 @@ function echo_escape_special() (
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
echo_escape_special "$@"
fi
14 changes: 7 additions & 7 deletions commands.beta/echo-exit-affirmative
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function echo_exit_affirmative() (
echo-exit-affirmative --fallback=no -- waiter 0 --status=2
outputs: no
EOF
if test "$#" -ne 0; then
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
Expand All @@ -37,7 +37,7 @@ function echo_exit_affirmative() (
# process
local rand="$RANDOM"
local item option_cmd=() option_fallback="$rand"
while test "$#" -ne 0; do
while [[ $# -ne 0 ]]; do
item="$1"
shift
case "$item" in
Expand All @@ -53,7 +53,7 @@ function echo_exit_affirmative() (
done

# check
if test "${#option_cmd[@]}" -eq 0; then
if [[ "${#option_cmd[@]}" -eq 0 ]]; then
help "No <command> was provided."
fi

Expand All @@ -62,18 +62,18 @@ function echo_exit_affirmative() (

local cmd_status
eval_capture --statusvar=cmd_status -- "${option_cmd[@]}"
if test "$cmd_status" -eq 0; then
if [[ "$cmd_status" -eq 0 ]]; then
__print_lines 'yes'
elif test "$cmd_status" -eq 1; then
elif [[ "$cmd_status" -eq 1 ]]; then
__print_lines 'no'
elif test "$option_fallback" != "$rand"; then
elif [[ "$option_fallback" != "$rand" ]]; then
__print_lines "$option_fallback"
else
return "$cmd_status"
fi
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
echo_exit_affirmative "$@"
fi
6 changes: 3 additions & 3 deletions commands.beta/echo-exit-status
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function echo_exit_status() (

# process
local item cmd=()
while test "$#" -ne 0; do
while [[ $# -ne 0 ]]; do
item="$1"
shift
case "$item" in
Expand All @@ -39,7 +39,7 @@ function echo_exit_status() (
done

# check
if test "${#cmd[@]}" -eq 0; then
if [[ "${#cmd[@]}" -eq 0 ]]; then
help 'No <command> was provided.'
fi

Expand All @@ -52,6 +52,6 @@ function echo_exit_status() (
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
echo_exit_status "$@"
fi
2 changes: 1 addition & 1 deletion commands.beta/echo-html-coder.recode
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# recode decoding fails with: recode: Untranslatable input in step `ISO-10646-UCS-2..ANSI_X3.4-1968'
if test "$1" = 'decode'; then
if [[ $1 == 'decode' ]]; then
recode html..ascii <<<"$2"
else
recode ascii..html <<<"$2"
Expand Down
2 changes: 1 addition & 1 deletion commands.beta/echo-html-coder.textutil
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# this doesn't work as expected
if test "$1" = 'decode'; then
if [[ $1 == 'decode' ]]; then
textutil -convert html -format txt -inputencoding UTF-8 -stdin -stdout <<<"$2"
else
textutil -convert txt -format html -inputencoding UTF-8 -stdin -stdout <<<"$2"
Expand Down
2 changes: 1 addition & 1 deletion commands.beta/echo-html-coder.xmlstarlet
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# xmlstarlet is too old to be available
if test "$1" = 'decode'; then
if [[ $1 == 'decode' ]]; then
xmlstarlet unesc "$2"
else
xmlstarlet esc "$1"
Expand Down
6 changes: 3 additions & 3 deletions commands.beta/echo-if-directory
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ function echo_if_directory() (
# exit status: 0
EOF
if test "$#" -ne 0; then
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}

function on_input {
if test -n "$1" -a -d "$1"; then
if [[ -n "$1" && -d "$1" ]]; then
__print_lines "$1"
fi
}
Expand All @@ -44,6 +44,6 @@ function echo_if_directory() (
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
echo_if_directory "$@"
fi
10 changes: 5 additions & 5 deletions commands.beta/echo-if-empty
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function echo_if_empty() (
ALTERNATIVES:
Use [ifne] from [moreutils], which is what we use in [eval-on-empty-stdin] and [eval-on-not-empty-stdin].
EOF
if test "$#" -ne 0; then
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
Expand All @@ -47,7 +47,7 @@ function echo_if_empty() (
# process our own arguments, delegate everything else to stdinargs
local rand="$RANDOM"
local item option_fallback="$rand" option_args=()
while test "$#" -ne 0; do
while [[ $# -ne 0 ]]; do
item="$1"
shift
case "$item" in
Expand All @@ -61,7 +61,7 @@ function echo_if_empty() (
;;
'--'*) option_args+=("$item") ;;
*)
if test "$option_fallback" = "$rand"; then
if [[ $option_fallback == "$rand" ]]; then
option_fallback="$item"
else
option_args+=("$item")
Expand All @@ -71,7 +71,7 @@ function echo_if_empty() (
done

# checck for expected
if test "$option_fallback" = "$rand"; then
if [[ $option_fallback == "$rand" ]]; then
help "Missing required argument: <expected>"
fi

Expand All @@ -94,6 +94,6 @@ function echo_if_empty() (
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
echo_if_empty "$@"
fi
6 changes: 3 additions & 3 deletions commands.beta/echo-if-executable
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ function echo_if_executable() (
# exit status: 0
EOF
if test "$#" -ne 0; then
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}

function on_input {
if test -n "$1" -a -x "$1"; then
if [[ -n "$1" && -x "$1" ]]; then
__print_lines "$1"
fi
}
Expand All @@ -48,6 +48,6 @@ function echo_if_executable() (
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
echo_if_executable "$@"
fi
4 changes: 2 additions & 2 deletions commands.beta/echo-if-path
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function echo_if_path() (
# exit status: 0
EOF
if test "$#" -ne 0; then
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
Expand All @@ -44,6 +44,6 @@ function echo_if_path() (
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
echo_if_path "$@"
fi
2 changes: 1 addition & 1 deletion commands.beta/echo-last-line
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ function echo_last_line() (
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
echo_last_line "$@"
fi
12 changes: 6 additions & 6 deletions commands.beta/echo-mkdir
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ function echo_mkdir() (
# exit status: 0
EOF
if test "$#" -ne 0; then
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}

# process our own arguments, delegate everything else to stdinargs
local item option_quiet='' option_sudo='no' option_args=()
while test "$#" -ne 0; do
while [[ $# -ne 0 ]]; do
item="$1"
shift
case "$item" in
Expand All @@ -62,7 +62,7 @@ function echo_mkdir() (
done

# construct command
if test "$option_sudo" = 'yes'; then
if [[ $option_sudo == 'yes' ]]; then
function __mkdir {
sudo-helper --reason='Your sudo/root/login password is required to ensure the directory exists:' -- mkdir -p "$@"
}
Expand All @@ -76,8 +76,8 @@ function echo_mkdir() (
# Action

function on_line {
if test -d "$1" || __mkdir "$1"; then
if test "$option_quiet" != 'yes'; then
if [[ -d "$1" ]] || __mkdir "$1"; then
if [[ "$option_quiet" != 'yes' ]]; then
fs-absolute -- "$1"
fi
return 0
Expand All @@ -90,6 +90,6 @@ function echo_mkdir() (
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
echo_mkdir "$@"
fi
Loading

0 comments on commit 4627b75

Please sign in to comment.