commit 311b2118ebc5a1c3802e26dc9dea12aaa79cefaa
parent b55e82087142c57ad186a2e4ce4e23682a423561
Author: Brian Mayer <bleemayer@gmail.com>
Date: Mon, 5 Jul 2021 12:00:55 -0300
Improved shellcheck results
Diffstat:
M | astro | | | 59 | +++++++++++++++++++++++++++-------------------------------- |
1 file changed, 27 insertions(+), 32 deletions(-)
diff --git a/astro b/astro
@@ -4,20 +4,15 @@
# Parameters: url
parseurl() {
# Credits: https://stackoverflow.com/a/6174447/7618649
- # extract the protocol
- proto="$(echo $1 | grep :// | sed -e's,^\(.*\)://.*,\1,g')"
- # remove the protocol
- url="$(echo ${1/"$proto://"/})"
- # extract the user (if any)
- user="$(echo $url | grep @ | cut -d@ -f1)"
- # extract the host and port
- hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
- # by request host without port
- host="$(echo $hostport | sed -e 's,:.*,,g')"
+ proto="$(echo "$1" | grep :// | sed -e's,^\(.*\)://.*,\1,g')" # extract the protocol
+ url="${1/"$proto://"/}" # remove the protocol
+ user="$(echo "$url" | grep @ | cut -d@ -f1)" # extract the user (if any)
+ hostport="$(echo "${url/$user@/}" | cut -d/ -f1)" # extract the host and port
+ host="$(echo "$hostport" | sed -e 's,:.*,,g')" # by request host without port
+
# by request - try to extract the port
- port="$(echo $hostport | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
- # extract the path (if any)
- path="$(echo $url | grep / | cut -d/ -f2-)"
+ port="$(echo "$hostport" | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
+ path="$(echo "$url" | grep / | cut -d/ -f2-)" # extract the path (if any)
echo "${proto:-gemini}" "$host" "${port:-1965}" "${path:-index.gmi}"
return 0
@@ -35,17 +30,17 @@ printbody() {
# Spec draft is here: https://gemini.circumlunar.space/docs/specification.html
parsegemini() {
# First line is status and meta information
- read status meta
+ read -r status meta
# Validate
case "$status" in
10)
echo "Input needed"
- return -1
+ return 1
;;
11)
echo "Sensitive input needed"
- return -2
+ return 2
;;
20)
# Success
@@ -59,42 +54,42 @@ parsegemini() {
;;
40)
echo "Temporary failure"
- return -3
+ return 3
;;
41)
- return -4
+ return 4
;;
42)
- return -5
+ return 5
;;
43)
- return -6
+ return 6
;;
44)
- return -7
+ return 7
;;
51)
echo "Page not found!"
- return -9
+ return 9
;;
52)
- return -10
+ return 10
;;
53)
- return -11
+ return 11
;;
59)
echo "Bad request"
- return -12
+ return 12
;;
60)
- return -13
+ return 13
;;
61)
- return -14
+ return 14
;;
62)
- return -15
+ return 15
;;
esac
}
@@ -111,10 +106,10 @@ fetch() {
trap "tput rmcup && exit" EXIT SIGINT SIGHUP
# Execution
-args="$@"
+args="$*"
# Default url
-[ -z $args ] && args="gemini.circumlunar.space/"
+[ -z "$args" ] && args="gemini.circumlunar.space/"
# Save terminal
tput smcup
@@ -126,10 +121,10 @@ while :
do
echo "Press q to quit or new url to navigate to"
read -r opt
- [ $opt = "q" ] && break
+ [ "$opt" = "q" ] && break
# Add a separator
- printf '\n%*s\n' $(tput cols) | tr ' ' '#'
+ printf '\n%*s\n' "$(tput cols)" "#" | tr ' ' '#'
fetch $(parseurl "$opt") | parsegemini
done