Skip to content

Commit 884161c

Browse files
committed
switched back to bash, because local is not posix anyway + cleanup
1 parent d11b215 commit 884161c

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

share/cht.sh.txt

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
#
33
# [X] open section
44
# [X] one shot mode
@@ -23,19 +23,20 @@
2323
# count words in text counter
2424
# group elements list
2525

26-
__CHTSH_VERSION=4
27-
__CHTSH_DATETIME="2018-07-08 22:26:46 +0200"
26+
__CHTSH_VERSION=5
27+
__CHTSH_DATETIME="2019-05-11 07:29:46 +0200"
2828

2929
export LESSSECURE=1
3030
STEALTH_MAX_SELECTION_LENGTH=5
3131

32-
case `uname -s` in
32+
case "$(uname -s)" in
3333
Darwin) is_macos=yes ;;
3434
*) is_macos=no ;;
3535
esac
3636

3737
# for KSH93
38-
if echo $KSH_VERSION | grep -q ' 93' && ! local foo 2>/dev/null; then
38+
# shellcheck disable=SC2034,SC2039,SC2168
39+
if echo "$KSH_VERSION" | grep -q ' 93' && ! local foo 2>/dev/null; then
3940
alias local=typeset
4041
fi
4142

@@ -61,7 +62,7 @@ do_query()
6162
b_opts="-b \"\$HOME/.cht.sh/id\""
6263
fi
6364

64-
eval curl $b_opts -s $uri > "$TMP1"
65+
eval curl "$b_opts" -s "$uri" > "$TMP1"
6566

6667
if [ -z "$lines" ] || [ "$(wc -l "$TMP1" | awk '{print $1}')" -lt "$lines" ]; then
6768
cat "$TMP1"
@@ -96,23 +97,24 @@ gen_random_str()
9697
(
9798
len=$1
9899
if command -v openssl >/dev/null; then
99-
openssl rand -base64 $(($len*3/4)) | awk -v ORS='' //
100+
openssl rand -base64 $((len*3/4)) | awk -v ORS='' //
100101
else
101102
rdev=/dev/urandom
102103
for d in /dev/{srandom,random,arandom}; do
103-
test -r $d && rdev=$d
104+
test -r "$d" && rdev=$d
104105
done
105106
if command -v hexdump >/dev/null; then
106-
hexdump -vn $(($len/2)) -e '1/1 "%02X" 1 ""' $rdev
107+
hexdump -vn $((len/2)) -e '1/1 "%02X" 1 ""' "$rdev"
107108
elif command -v xxd >/dev/null; then
108-
xxd -l $(($len/2)) -ps $dev | awk -v ORS='' //
109+
xxd -l $((len/2)) -ps "$rdev" | awk -v ORS='' //
109110
else
110-
cd /tmp
111+
cd /tmp || { echo Cannot cd into /tmp >&2; exit 1; }
111112
s=
112-
while [ $(echo "$s" | wc -c) -lt $len ]; do
113+
# shellcheck disable=SC2000
114+
while [ "$(echo "$s" | wc -c)" -lt "$len" ]; do
113115
s="$s$(mktemp -u XXXXXXXXXX)"
114116
done
115-
printf %.${len}s "$s"
117+
printf "%.${len}s" "$s"
116118
fi
117119
fi
118120
)
@@ -129,7 +131,7 @@ fi
129131
[ -z "$CHTSH_URL" ] && CHTSH_URL=https://cht.sh
130132

131133
# any better test not involving either OS matching or actual query?
132-
if [ `uname -s` = OpenBSD ] && [ -x /usr/bin/ftp ]; then
134+
if [ "$(uname -s)" = OpenBSD ] && [ -x /usr/bin/ftp ]; then
133135
curl() {
134136
local opt args="-o -"
135137
while getopts "b:s" opt; do
@@ -139,22 +141,23 @@ if [ `uname -s` = OpenBSD ] && [ -x /usr/bin/ftp ]; then
139141
*) echo "internal error: unsupported cURL option '$opt'" >&2; exit 1;;
140142
esac
141143
done
142-
shift $(($OPTIND - 1))
143-
/usr/bin/ftp $args "$@"
144+
shift $((OPTIND - 1))
145+
/usr/bin/ftp "$args" "$@"
144146
}
145147
else
146148
command -v curl >/dev/null || { echo 'DEPENDENCY: install "curl" to use cht.sh' >&2; exit 1; }
147149
_CURL=$(command -v curl)
148150
if [ x"$CHTSH_CURL_OPTIONS" != x ]; then
149151
curl() {
150-
$_CURL ${CHTSH_CURL_OPTIONS} "$@"
152+
$_CURL "${CHTSH_CURL_OPTIONS}" "$@"
151153
}
152154
fi
153155
fi
154156

155157

156158
if [ "$1" = --read ]; then
157-
read -r a || a=exit
159+
read -r a || a="exit"
160+
# shellcheck disable=SC1117
158161
printf "%s\n" "$a"
159162
exit 0
160163
elif [ x"$1" = x--help ] || [ -z "$1" ]; then
@@ -193,10 +196,13 @@ else
193196

194197
if [ "$valid" = yes ]; then
195198
section="$new_section"
199+
# shellcheck disable=SC2001
196200
this_query="$(echo "$input" | sed 's@ *[^ ]* *@@')"
201+
# shellcheck disable=SC1117
197202
this_prompt="\033[0;32mcht.sh/$section>\033[0m "
198203
else
199204
this_query="$input"
205+
# shellcheck disable=SC1117
200206
this_prompt="\033[0;32mcht.sh>\033[0m "
201207
fi
202208
if [ -n "$this_query" ] && [ -z "$CHEATSH_RESTART" ]; then
@@ -216,9 +222,9 @@ lines=$(tput lines)
216222
if command -v less >/dev/null; then
217223
defpager="less -R"
218224
elif command -v more >/dev/null; then
219-
defpager=more
225+
defpager="more"
220226
else
221-
defpager=cat
227+
defpager="cat"
222228
fi
223229

224230
cmd_cd() {
@@ -234,7 +240,12 @@ cmd_cd() {
234240
if [ "$valid" = no ]; then
235241
echo "Invalid section: $new_section"
236242
echo "Valid sections:"
237-
echo $valid_sections | xargs printf "%-10s\n" | tr ' ' . | xargs -n 10 | sed 's/\./ /g; s/^/ /'
243+
# shellcheck disable=SC1117
244+
echo "$valid_sections" \
245+
| xargs printf "%-10s\n" \
246+
| tr ' ' . \
247+
| xargs -n 10 \
248+
| sed 's/\./ /g; s/^/ /'
238249
else
239250
section="$new_section"
240251
fi
@@ -252,7 +263,7 @@ cmd_copy() {
252263
if [ "$is_macos" != yes ]; then
253264
xsel -bi < "$TMP1"
254265
else
255-
cat "$TMP1" | pbcopy
266+
pbcopy < "$TMP1"
256267
fi
257268
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
258269
fi
@@ -268,7 +279,7 @@ cmd_ccopy() {
268279
if [ "$is_macos" != yes ]; then
269280
xsel -bi < "$TMP1"
270281
else
271-
cat "$TMP1" | pbcopy
282+
pbcopy < "$TMP1"
272283
fi
273284
echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
274285
fi
@@ -299,7 +310,7 @@ EOF
299310
}
300311

301312
cmd_hush() {
302-
mkdir -p $HOME/.cht.sh/ && touch $HOME/.cht.sh/.hushlogin && echo "Initial 'use help' message was disabled"
313+
mkdir -p "$HOME/.cht.sh/" && touch "$HOME/.cht.sh/.hushlogin" && echo "Initial 'use help' message was disabled"
303314
}
304315

305316
cmd_id() {
@@ -318,7 +329,7 @@ cmd_id() {
318329
fi
319330
return
320331
fi
321-
if [ -n "$new_id" ] && [ reset != "$new_id" ] && [ $(/bin/echo -n "$new_id" | wc -c) -lt 16 ]; then
332+
if [ -n "$new_id" ] && [ reset != "$new_id" ] && [ "$(/bin/echo -n "$new_id" | wc -c)" -lt 16 ]; then
322333
echo "ERROR: $new_id: Too short id. Minimal id length is 16. Use 'id reset' for a random id"
323334
return
324335
fi
@@ -327,7 +338,7 @@ cmd_id() {
327338
# if yes, just show it
328339
# if not, generate a new id
329340
if [ -e "$id_file" ]; then
330-
echo $(awk '$6 == "id" {print $NF}' <"$id_file" | tail -n 1)
341+
awk '$6 == "id" {print $NF}' <"$id_file" | tail -n 1
331342
return
332343
else
333344
new_id=reset
@@ -379,7 +390,7 @@ cmd_stealth() {
379390
if [ "$past" != "$current" ]; then
380391
past=$current
381392
current_text="$(echo $current | tr -c '[a-zA-Z0-9]' ' ')"
382-
if [ $(echo $current_text | wc -w) -gt "$STEALTH_MAX_SELECTION_LENGTH" ]; then
393+
if [ "$(echo "$current_text" | wc -w)" -gt "$STEALTH_MAX_SELECTION_LENGTH" ]; then
383394
echo "\033[0;31mstealth:\033[0m selection length is longer than $STEALTH_MAX_SELECTION_LENGTH words; ignoring"
384395
continue
385396
else
@@ -394,14 +405,14 @@ cmd_stealth() {
394405
}
395406

396407
cmd_update() {
397-
[ -w "$0" ] || { echo "The script is readonly; please update manually: curl -s "${CHTSH_URL}"/:bash | sudo tee $0"; return; }
408+
[ -w "$0" ] || { echo "The script is readonly; please update manually: curl -s ${CHTSH_URL}/:cht.sh | sudo tee $0"; return; }
398409
TMP2=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
399410
curl -s "${CHTSH_URL}"/:cht.sh > "$TMP2"
400411
if ! cmp "$0" "$TMP2" > /dev/null 2>&1; then
401412
if grep -q ^__CHTSH_VERSION= "$TMP2"; then
402413
# section was vaildated by us already
403-
args="--shell $section"
404-
cp "$TMP2" "$0" && echo "Updated. Restarting..." && rm "$TMP2" && CHEATSH_RESTART=1 exec "$0" $args
414+
args=(--shell "$section")
415+
cp "$TMP2" "$0" && echo "Updated. Restarting..." && rm "$TMP2" && CHEATSH_RESTART=1 exec "$0" "${args[@]}"
405416
else
406417
echo "Something went wrong. Please update manually"
407418
fi
@@ -450,8 +461,8 @@ while true; do
450461
"") continue;; # skip empty input lines
451462
'?'|h|help) cmd_name=help;;
452463
hush) cmd_name=hush;;
453-
cd) cmd_name=cd;;
454-
exit|quit) cmd_name=exit;;
464+
cd) cmd_name="cd";;
465+
exit|quit) cmd_name="exit";;
455466
copy|yank|c|y) cmd_name=copy;;
456467
ccopy|cc|C|Y) cmd_name=ccopy;;
457468
id) cmd_name=id;;
@@ -460,5 +471,5 @@ while true; do
460471
version) cmd_name=version;;
461472
*) cmd_name="query $cmd_name";;
462473
esac
463-
cmd_$cmd_name $cmd_args
474+
"cmd_$cmd_name" $cmd_args
464475
done

0 commit comments

Comments
 (0)