1
- #! /bin/sh
1
+ #! /bin/bash
2
2
#
3
3
# [X] open section
4
4
# [X] one shot mode
23
23
# count words in text counter
24
24
# group elements list
25
25
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"
28
28
29
29
export LESSSECURE=1
30
30
STEALTH_MAX_SELECTION_LENGTH=5
31
31
32
- case ` uname -s` in
32
+ case " $( uname -s) " in
33
33
Darwin) is_macos=yes ;;
34
34
* ) is_macos=no ;;
35
35
esac
36
36
37
37
# 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
39
40
alias local=typeset
40
41
fi
41
42
@@ -61,7 +62,7 @@ do_query()
61
62
b_opts=" -b \"\$ HOME/.cht.sh/id\" "
62
63
fi
63
64
64
- eval curl $b_opts -s $uri > " $TMP1 "
65
+ eval curl " $b_opts " -s " $uri " > " $TMP1 "
65
66
66
67
if [ -z " $lines " ] || [ " $( wc -l " $TMP1 " | awk ' {print $1}' ) " -lt " $lines " ]; then
67
68
cat " $TMP1 "
@@ -96,23 +97,24 @@ gen_random_str()
96
97
(
97
98
len=$1
98
99
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=' ' //
100
101
else
101
102
rdev=/dev/urandom
102
103
for d in /dev/{srandom,random,arandom}; do
103
- test -r $d && rdev=$d
104
+ test -r " $d " && rdev=$d
104
105
done
105
106
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 "
107
108
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=' ' //
109
110
else
110
- cd /tmp
111
+ cd /tmp || { echo Cannot cd into /tmp >&2 ; exit 1 ; }
111
112
s=
112
- while [ $( echo " $s " | wc -c) -lt $len ]; do
113
+ # shellcheck disable=SC2000
114
+ while [ " $( echo " $s " | wc -c) " -lt " $len " ]; do
113
115
s=" $s $( mktemp -u XXXXXXXXXX) "
114
116
done
115
- printf %.${len} s " $s "
117
+ printf " %.${len} s" " $s "
116
118
fi
117
119
fi
118
120
)
129
131
[ -z " $CHTSH_URL " ] && CHTSH_URL=https://cht.sh
130
132
131
133
# 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
133
135
curl () {
134
136
local opt args=" -o -"
135
137
while getopts " b:s" opt; do
@@ -139,22 +141,23 @@ if [ `uname -s` = OpenBSD ] && [ -x /usr/bin/ftp ]; then
139
141
* ) echo " internal error: unsupported cURL option '$opt '" >&2 ; exit 1;;
140
142
esac
141
143
done
142
- shift $(( $ OPTIND - 1 ))
143
- /usr/bin/ftp $args " $@ "
144
+ shift $(( OPTIND - 1 ))
145
+ /usr/bin/ftp " $args " " $@ "
144
146
}
145
147
else
146
148
command -v curl > /dev/null || { echo ' DEPENDENCY: install "curl" to use cht.sh' >&2 ; exit 1; }
147
149
_CURL=$( command -v curl)
148
150
if [ x" $CHTSH_CURL_OPTIONS " != x ]; then
149
151
curl () {
150
- $_CURL ${CHTSH_CURL_OPTIONS} " $@ "
152
+ $_CURL " ${CHTSH_CURL_OPTIONS} " " $@ "
151
153
}
152
154
fi
153
155
fi
154
156
155
157
156
158
if [ " $1 " = --read ]; then
157
- read -r a || a=exit
159
+ read -r a || a=" exit"
160
+ # shellcheck disable=SC1117
158
161
printf " %s\n" " $a "
159
162
exit 0
160
163
elif [ x" $1 " = x--help ] || [ -z " $1 " ]; then
@@ -193,10 +196,13 @@ else
193
196
194
197
if [ " $valid " = yes ]; then
195
198
section=" $new_section "
199
+ # shellcheck disable=SC2001
196
200
this_query=" $( echo " $input " | sed ' s@ *[^ ]* *@@' ) "
201
+ # shellcheck disable=SC1117
197
202
this_prompt=" \033[0;32mcht.sh/$section >\033[0m "
198
203
else
199
204
this_query=" $input "
205
+ # shellcheck disable=SC1117
200
206
this_prompt=" \033[0;32mcht.sh>\033[0m "
201
207
fi
202
208
if [ -n " $this_query " ] && [ -z " $CHEATSH_RESTART " ]; then
@@ -216,9 +222,9 @@ lines=$(tput lines)
216
222
if command -v less > /dev/null; then
217
223
defpager=" less -R"
218
224
elif command -v more > /dev/null; then
219
- defpager=more
225
+ defpager=" more"
220
226
else
221
- defpager=cat
227
+ defpager=" cat"
222
228
fi
223
229
224
230
cmd_cd () {
@@ -234,7 +240,12 @@ cmd_cd() {
234
240
if [ " $valid " = no ]; then
235
241
echo " Invalid section: $new_section "
236
242
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/^/ /'
238
249
else
239
250
section=" $new_section "
240
251
fi
@@ -252,7 +263,7 @@ cmd_copy() {
252
263
if [ " $is_macos " != yes ]; then
253
264
xsel -bi < " $TMP1 "
254
265
else
255
- cat " $TMP1 " | pbcopy
266
+ pbcopy < " $TMP1 "
256
267
fi
257
268
echo " copy: $( wc -l " $TMP1 " | awk ' {print $1}' ) lines copied to the selection"
258
269
fi
@@ -268,7 +279,7 @@ cmd_ccopy() {
268
279
if [ " $is_macos " != yes ]; then
269
280
xsel -bi < " $TMP1 "
270
281
else
271
- cat " $TMP1 " | pbcopy
282
+ pbcopy < " $TMP1 "
272
283
fi
273
284
echo " copy: $( wc -l " $TMP1 " | awk ' {print $1}' ) lines copied to the selection"
274
285
fi
299
310
}
300
311
301
312
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"
303
314
}
304
315
305
316
cmd_id () {
@@ -318,7 +329,7 @@ cmd_id() {
318
329
fi
319
330
return
320
331
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
322
333
echo " ERROR: $new_id : Too short id. Minimal id length is 16. Use 'id reset' for a random id"
323
334
return
324
335
fi
@@ -327,7 +338,7 @@ cmd_id() {
327
338
# if yes, just show it
328
339
# if not, generate a new id
329
340
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
331
342
return
332
343
else
333
344
new_id=reset
@@ -379,7 +390,7 @@ cmd_stealth() {
379
390
if [ " $past " != " $current " ]; then
380
391
past=$current
381
392
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
383
394
echo " \033[0;31mstealth:\033[0m selection length is longer than $STEALTH_MAX_SELECTION_LENGTH words; ignoring"
384
395
continue
385
396
else
@@ -394,14 +405,14 @@ cmd_stealth() {
394
405
}
395
406
396
407
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 ; }
398
409
TMP2=$( mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
399
410
curl -s " ${CHTSH_URL} " /:cht.sh > " $TMP2 "
400
411
if ! cmp " $0 " " $TMP2 " > /dev/null 2>&1 ; then
401
412
if grep -q ^__CHTSH_VERSION= " $TMP2 " ; then
402
413
# 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[@]} "
405
416
else
406
417
echo " Something went wrong. Please update manually"
407
418
fi
@@ -450,8 +461,8 @@ while true; do
450
461
" " ) continue ;; # skip empty input lines
451
462
' ?' |h|help) cmd_name=help;;
452
463
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" ;;
455
466
copy|yank|c|y) cmd_name=copy;;
456
467
ccopy|cc|C|Y) cmd_name=ccopy;;
457
468
id) cmd_name=id;;
@@ -460,5 +471,5 @@ while true; do
460
471
version) cmd_name=version;;
461
472
* ) cmd_name=" query $cmd_name " ;;
462
473
esac
463
- cmd_$cmd_name $cmd_args
474
+ " cmd_$cmd_name " $cmd_args
464
475
done
0 commit comments