-
Notifications
You must be signed in to change notification settings - Fork 1
/
.zsh-func
1249 lines (1122 loc) · 48.8 KB
/
.zsh-func
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/zsh
# Most w sounds v, replaced all w with v throughout my simple primitive dot files
function \?() {
echo «$(\date +"%Y%m%d %H%M%S.%N%::z%Z %A")»
prntenv
#https://www.youtube.com/playlist?list=PLypxmOPCOkHXL8TuVKiDChkTH32MfLr7S
#https://www.youtube.com/watch?v=fDoYwL90WUg&list=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n&index=8
#https://youtu.be/oYtGrCP_n0k?t=329
#https://www.youtube.com/watch?v=VPInIrd5Hqc#t=35s
#https://www.youtube.com/watch?v=M8Bd7uHH4Yg&feature=youtu.be
#https://codek.tv/v/6MaOPdQPvow
#http://video.ch9.ms/ch9/3306/0e92ae7f-7b06-4a08-a2a0-e80545733306/DefragTools179_high.mp4
#https://video.xx.fbcdn.net/v/t43.1792-2/14018597_941915082602721_1324838905_n.mp4?efg=eyJybHIiOjE1MDAsInJsYSI6MTAyNCwidmVuY29kZV90YWciOiJzdmVfaGQifQ%3D%3D&rl=1500&vabr=588&oh=37f5d4bd4e3b3de99d2f0093c17a69d0&oe=57CBD2F3
#https://www.facebook.com/ehbichig/videos/991876094204055/
#use variables to hold regxprs to avoid: parse error near `&'
#"dl+"(echo "$url" >> dllst for url in "$@") automatically escape ? and = in output file! e.g. https://www.youtube.com/playlist\?list\=PLKK11Ligqitg9MOX3-0tFT1Rmh3uJp7kA
#utvList_regxpr="^https?://(www\.)?youtu\.?be(\.com)?/playlist[\]?\?list[\]?=[-_a-zA-Z0-9]+$"
local utvList_regxpr="^https?://(www\.)?youtu\.?be(\.com)?/playlist\\\\?\?list\\\\?=[-_a-zA-Z0-9]+$"
#utvSingl_regxpr="^https?://(www\.)?youtu\.?be(\.com)?/(watch\?v=)?[-_a-zA-Z0-9]+([\&#]list=[-_.a-zA-Z0-9]+)?([-_.a-zA-Z0-9\&#=]+)*$"
local utvSingl_regxpr="^https?://(www\.)?youtu\.?be(\.com)?/(watch\?v=)?[-_a-zA-Z0-9]+([\&#]list=[-_.a-zA-Z0-9]+)?[-_.a-zA-Z0-9\&#=]*$"
local codek_regxpr="^https?://(www\.)?codek\.tv/v/[-_a-zA-Z0-9]+$"
local knl9_regxpr="^https?://video\.ch9\.ms/[-_a-zA-Z0-9/.]+[-_a-zA-Z0-9.]+$"
local vimeo_regxpr="^https?://(www\.)?vimeo\.com/[-_a-zA-Z0-9]+$"
local facebook_regxpr="^https?://video\.[.a-zA-Z0-9]*fbcdn\.net/v/[-_.a-zA-Z0-9]+/[-_.a-zA-Z0-9]+(mp4|webm|mkv)[-_.a-zA-Z0-9?%=&]*$"
local idx=0
local audiofrmt="(mp3|flac|m4a|opus|aac|wav)"
local videofrmt="(mp4|mkv|webm|mpg|mpeg|m2ts|MTS|CPI|clpi|MPL|mpls|BDM|bdmv|rmvb|avi|dvi|ogg)"
#1 input sanitization
#local uniq_args=$(gawk 'BEGIN {RS=" ";ORS="\n"} !seen[$1]++ {printf "%s\n", $1}' <<< "$@")
local uniq_args=$(gawk 'BEGIN {RS=" ";ORS="\n"} !seen[$1]++ {print $1}' <<< "$@")
#local uniq_args=$(gawk 'BEGIN {RS=ORS=" "} !seen[$1]++ {print $1}' <<< "$@")
echo 'uniq_args:['$uniq_args']'
local cmd='sudo find /mnt/0 ~ -xdev -type f \( '
#https://unix.stackexchange.com/questions/275794/iterating-over-multiple-line-string-stored-in-variable
#https://superuser.com/questions/284187/bash-iterating-over-lines-in-a-variable
local str=""
#IFS=' ' read -a str_array <<< "$@" #bad option: -a
#for str in "${str_array[@]}";do #unnecessary because of cmd="\? $urls_whitespaced" && eval " $cmd"
#http://www.refining-linux.org/archives/38/ZSH-Gem-3-No-automatic-word-split/
#https://unix.stackexchange.com/questions/128985/why-not-parse-ls
setopt sh_word_split
setopt SH_WORD_SPLIT
#local IFS='
#'
local IFS=$'\n'
#local IFS=' '
for str in $uniq_args;do
#todo: treat quoted string as single arg e.g. ? "Clean C++"
#for str in $@;do
#trim spaces at both ends
# str="${str##( |\t|\f|\v)}"
# str="${str%%( |\t|\f|\v)}"
# str=$(tr -s "[:blank:]" <<<$str)
str=$(tr -s " \t\f\v" <<< $str)
#https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable
str=$(trmsp $str)
echo "input arg after func trmsp() called:[$str]"
if [[ $str =~ $utvList_regxpr ]];then
echo "[$str] match [$utvList_regxpr]"
echo '$'$((++idx)):'['$str']'
echo "playlist (contained) item(s) search is currently not supported!"
continue
elif [[ $str =~ $utvSingl_regxpr ]];then
# echo "[$str] match [$utvSingl_regxpr]"
str="${str##*youtu*be*/}"
str="${str##watch\?v=}"
str="${str%% *}"
elif [[ $str =~ $codek_regxpr ]];then
echo "[$str] match [$codek_regxpr]"
str="${str##*codek.tv/v/}"
elif [[ $str =~ $knl9_regxpr ]];then
echo "[$str] match [$knl9_regxpr]"
str="${str##*video.ch9.ms/[-_a-zA-Z0-9/.]*/}"
elif [[ $str =~ $vimeo_regxpr ]];then
echo "[$str] match [$vimeo_regxpr]"
str="${str##*vimeo.com/}"
elif [[ $str =~ $facebook_regxpr ]];then
echo "[$str] match [$facebook_regxpr]"
# str="${str##*video.*fbcdn.net/v/[-_.a-zA-Z0-9]*/}"
str="${str##*video.*fbcdn.net/v/*/}"
else
echo '$'$((++idx)):'['$str']'
fi
str="${str%&index=*}"
str="${str%&list=*}"
str="${str%&t=*}"
str="${str%%\?*}"
str="${str%%&*}"
str="${str%%#*}"
#replace spaces inside with *
# str="${str// \t\f\v/*}"
str="${str// /*}"
#strip extension except .todo
#https://fileinfo.com/filetypes/common
# str="${str%?\.(mp4|mp3|mkv|mpg|mpeg|webm|m4a|aac|flac|rmvb|avi|opus|dvi|wav)}" #?:non greedy
str="${str%\.audiofrmt}"
str="${str%\.videofrmt}"
cmd+="-iname \"*$str*\" -o "
done
#http://www.refining-linux.org/archives/38/ZSH-Gem-3-No-automatic-word-split/
unsetopt sh_word_split
unsetopt SH_WORD_SPLIT
if [[ $cmd =~ -iname ]];then
cmd=${cmd% -o }
cmd+=' \) -exec ls -alFt {} +'
# cmd+=" \) -exec ls -alFt {} + | gawk -F'.' '{print substr($(NF-1),length($(NF-1))-4)}' > $dnldd_file" #zsh: NF-1: command not found...
echo '$cmd(\?(){}):['$cmd']'
#find $uniq_args in other 'download list files', save only when not downloaded and not listed in other download list files!
local url_NoEscapeBraces="[a-zA-Z0-9]+\.[a-zA-Z0-9]{2,3}/[a-zA-Z0-9]+"
local url_EscapeBraces="[a-zA-Z0-9]+\.[a-zA-Z0-9]\{2,3\}/[a-zA-Z0-9]+"
#todo: jump statement in shell script #unnecessary because of if(... && ...) || (... && ...)
#if [[ $uniq_args =~ $url_NoEscapeBraces ]];then
if ([[ $uniq_args =~ $url_EscapeBraces ]] && echo "match regxpr \`$url_EscapeBraces' in args") || ([[ $uniq_args =~ $url_NoEscapeBraces ]] && echo "match regxpr \`$url_NoEscapeBraces' in args");then
# echo "match regxpr \`$url_NoEscapeBraces' in args"
#todo: save and resume partially downloaded files, e.g. ".part" files
local dnldd_file="/tmp/_downloaded.find_avfiles_"
#todo: hard to extract ideal safe common substr
# eval " $cmd" | gawk -F'.' '{print substr($(NF-1),length($(NF-1))-4)}' > "$dnldd_file"
eval " $cmd" | tee "$dnldd_file" 2>&1
# echo -e 'ID(last 5 char of names\x27 main part) of already downloaded:'
# more "$dnldd_file"
echo '$(echo -n $uniq_args | wc -l)':$(echo -n $uniq_args | wc -l)
echo '$(echo $uniq_args | wc -l)':$(echo $uniq_args | wc -l)
echo '$(wc -l <<< $uniq_args)':$(wc -l <<< $uniq_args)
#https://unix.stackexchange.com/questions/343053/bash-test-wc-with-ge-division-by-0-error/343055
#echo '$(wc -l "$dnldd_file")':$(wc -l "$dnldd_file") #LnNum filename
echo '$(wc -l < "$dnldd_file")':$(wc -l < "$dnldd_file")
if (( $(wc -l < "$dnldd_file") < $(wc -l <<< $uniq_args) ));then
# if (( $(wc -l "$dnldd_file") < $(wc -l <<< $uniq_args) ));then #?:LnNum: division by zero
# if (( $(wc -l "$dnldd_file") < $(echo -n $uniq_args | wc -l) ));then #?:LnNum: division by zero
echo '$(wc -l < "$dnldd_file") < $(wc -l <<< $uniq_args):true'
local dnldd_ID="/tmp/_downloaded_ID.find_avfiles_"
local dllst_file="dllst"
#todo: hard to extract ideal safe common substr, may not work for URLs others than youtube's!
gawk -F'.' '{print substr($(NF-1),length($(NF-1))-4)}' "$dnldd_file" > "$dnldd_ID"
#todo: find other 'download list file', diff and save entries only in $uniq_args
local varNotDnldd=$(command grep -v -F -f "$dnldd_ID" <<< $uniq_args)
if [[ -n ${varNotDnldd// \t\v\n\u3000} ]];then
echo "$varNotDnldd" > /tmp/_not_downloaded.find_avfiles_
echo -e '$(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf \x27%p \x27):['$(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf '%p ')']'
#https://unix.stackexchange.com/questions/386499/how-to-check-if-file-is-empty-or-it-has-only-blank-characters
#command grep -q '[^ \t\v\n\u3000]' $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf '%p ') && { #(
local spaces_rexpr='^[ \t\v\n\u3000]*$'
local dllstfiles=$(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -print0) #find ... -print0 | xargs -0 cat
if [[ -n ${dllstfiles// \t\v\n\u3000} ]];then
echo '[[ -n ${dllstfiles// \t\v\n\u3000} ]]:true'
fi
if [[ ! $dllstfiles =~ $spaces_rexpr ]];then
echo '[[ ! $dllstfiles =~ $spaces_rexpr ]]:true'
fi
#if [[ -n $dllstfiles ]] && [[ ! $dllstfiles =~ $spaces_rexpr ]];then
#if [[ -n ${dllstfiles// \t\v\n\u3000} ]];then
if [[ ! $dllstfiles =~ $spaces_rexpr ]];then
#https://stackoverflow.com/questions/864316/how-to-pipe-list-of-files-returned-by-find-command-to-cat-to-view-all-the-files
echo -n $dllstfiles | xargs -0 cat -n | sort -k2 -k1n | uniq -f1 | sort -nk1,1 | cut -f2- > /tmp/_not_downloaded_but_listed_in_dllst_files.find_avfiles_
#those not downloaded may already be listded in PWD, so append not overwrite
local varDllstFoundDiff=$(command grep -v -F -f /tmp/_not_downloaded_but_listed_in_dllst_files.find_avfiles_ /tmp/_not_downloaded.find_avfiles_)
#https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
#https://unix.stackexchange.com/questions/146942/how-can-i-test-if-a-variable-is-empty-or-contains-only-spaces
if [[ -n ${varDllstFoundDiff// \t\v\n\u3000} ]];then #local spaces_rexpr='^[ \t\v\n\u3000]*$'
if [[ -f $dllst_file ]];then
echo "$varDllstFoundDiff" >> $dllst_file
cat -n $dllst_file | sort -k2 -k1n | uniq -f1 | sort -nk1,1 | cut -f2- > /tmp/_unik_dllst_file.find_avfiles_ && command mv -f /tmp/_unik_dllst_file.find_avfiles_ $dllst_file
else
echo -n $varDllstFoundDiff | cat -n | sort -k2 -k1n | uniq -f1 | sort -nk1,1 | cut -f2- > $dllst_file
fi
echo -e "download list:file \x22$dllst_file\x22:"
more "$dllst_file"
#https://stackoverflow.com/questions/32126653/how-does-end-work-in-bash-to-create-a-multi-line-comment-block?noredirect=1&lq=1
: <<'HEREDOC:BLOCK_COMMENT'
#command grep -v -F -f /tmp/_tmp2_ <<< $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf '%p ') > /tmp/_tmp3_
#command grep -v -F -f /tmp/_tmp2_ $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf '%p ') > /tmp/_tmp3_
#1 #erroneous: file name interpreted as literal string
echo 'eval $(command grep -v -F -f /tmp/_tmp2_ <<< $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf \x27%p \x27) > /tmp/_tmp31_) : /tmp/_tmp31_ :'
eval $(command grep -v -F -f /tmp/_tmp2_ <<< $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf '%p ') > /tmp/_tmp31_)
echo "[$(more /tmp/_tmp31_)]"
#2
echo 'eval $(command grep -v -F -f /tmp/_tmp2_ $(trmsp $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf \x27%p \x27)) > /tmp/_tmp32_) : /tmp/_tmp32_ :'
eval $(command grep -v -F -f /tmp/_tmp2_ $(trmsp $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf '%p ')) > /tmp/_tmp32_)
echo "[$(more /tmp/_tmp32_)]"
#3 #erroneous: file name interpreted as literal string
echo 'eval command grep -v -F -f /tmp/_tmp2_ <<< $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf \x27%p \x27) > /tmp/_tmp33_ : /tmp/_tmp33_ :'
eval command grep -v -F -f /tmp/_tmp2_ <<< $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf '%p ') > /tmp/_tmp33_
echo "[$(more /tmp/_tmp33_)]"
#4
echo 'eval command grep -v -F -f /tmp/_tmp2_ $(trmsp $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf \x27%p \x27)) > /tmp/_tmp34_ : /tmp/_tmp34_ :'
eval command grep -v -F -f /tmp/_tmp2_ $(trmsp $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf '%p ')) > /tmp/_tmp34_
echo "[$(more /tmp/_tmp34_)]"
#find: paths must precede expression: x27
#Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
#echo 'eval $(echo -en $(trmsp $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf \x27%p \x27)) | command grep -v -F -f /tmp/_tmp2_ > /tmp/_tmp3_) : /tmp/_tmp3_ :'
#eval $(echo -en $(trmsp $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file -printf \x27%p \x27)) | command grep -v -F -f /tmp/_tmp2_ > /tmp/_tmp3_)
#more /tmp/_tmp3_
#https://unix.stackexchange.com/questions/386499/how-to-check-if-file-is-empty-or-it-has-only-blank-characters
command grep -q '[^ \t\v\n\u3000]' /tmp/_tmp34_ && (
#command mv -f /tmp/_tmp34_ "$dllst_file"
#command cp -i /tmp/_tmp34_ "$dllst_file"
#cat /tmp/_tmp34_ >> "$dllst_file"
)
HEREDOC:BLOCK_COMMENT
fi
else
#unnecessary because gotten from $uniq_args
#cat -n /tmp/_not_downloaded.find_avfiles_ | sort -k2 -k1n | uniq -f1 | sort -nk1,1 | cut -f2- > "$dllst_file"
command mv -f /tmp/_not_downloaded.find_avfiles_ "$dllst_file"
echo -e "download list:file \x22$dllst_file\x22:"
more "$dllst_file"
fi
#} #)
fi
fi
else
echo '$cmd(\?(){}):['$cmd']'
#http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#eval
#The eval utility shall construct a command by concatenating arguments together, separating each with a <space> character. The constructed command shall be read and executed by the shell.
#SYNOPSIS:eval [argument...]
#Since eval is not required to recognize the "--" end of options delimiter, in cases where the argument(s) to eval might begin with '-' it is recommended that the first argument is prefixed by a string that will not alter the commands to be executed, such as a <space> character:
#eval " $commands"
#or:
#eval " $(some_command)"
eval " $cmd"
fi
echo '$?':$?
else
return 113 #can't be passed on to outer/upper levels!
fi
echo «$(\date +"%Y%m%d %H%M%S.%N%::z%Z %A")»
}
function dnldck() {
echo «$(\date +"%Y%m%d %H%M%S.%N%::z%Z %A")»
local urls_whitespaced=""
local errmsg="[fF]ind.*[pP]ermission[s ]*[dD]enied" #use variable to hold complex regxpr!
local dllst_file="dllst"
while getopts "t" opt; do
case "$opt" in
t) dllst_file="dllst*";;
\?) # unknown flag
#echo >&2 \
echo "usage: $0 [-t]"
return 1;;
*) echo "default case of getopts, nothing special.";;
esac
done
shift $((OPTIND-1))
#Note: For those dealing with CJK text (Chinese, Japanese, and Korean), the double-byte space (Unicode \u3000) is not included in \s for any implementation I've tried so far (Perl, .NET, PCRE, Python). You'll need to either normalize your strings first (such as by replacing all \u3000 with \u0020), or you'll have to use a character set that includes this codepoint in addition to whatever other whitespace you're targeting, such as [ \t\u3000].
#https://stackoverflow.com/questions/3583111/regular-expression-find-spaces-tabs-space-but-not-newlines
local blankline_rexpr="^[ \t\v\u3000]*$"
local file=""
for file in $(sudo find /mnt/0 ~ -xdev -type f -iname $dllst_file);do
# if [[ ! ( $file =~ [fF]ind.*[pP]ermission[s ]*[dD]enied ) ]];then #error: condition expected: $file #stderr inaccessible this way!
if [[ ! $file =~ $errmsg && -f $file ]];then #stderr inaccessible this way!
cat -n $file | sort -k2 -k1n | uniq -f1 | sort -nk1,1 | cut -f2- > _tmp.dnldck_ && command mv -f _tmp.dnldck_ $file
echo $file:
more $file
#while read -r line;do
# if [[ ! $line =~ $blankline_rexpr ]];then
# urls_whitespaced+="\"$line\" " #urls_whitespaced+=("$line"+" ")?
# #urls_whitespaced+="$line " #urls_whitespaced+=("$line"+" ")?
# fi
#done < "$file"
#echo '$urls_whitespaced:['$urls_whitespaced']'
#IFS=' ' read -a urls_whitespaced_arr <<<"$urls_whitespaced" #bad option: -a
#\? "$urls_whitespaced_arr" #wrong!
#\? $urls_whitespaced #wrong!
#cmd="\? $urls_whitespaced"
#cmd="\? $(gawk -v quot="'" '!seen[toupper($0)]++ {printf "%s%s%s ", quot, $0, quot} END {print ""}' $file)"
local cmd="\? $(gawk -v quot="'" '!seen[$0]++ {printf "%s%s%s ", quot, $0, quot}' $file)"
#https://unix.stackexchange.com/questions/102008/how-do-i-trim-leading-and-trailing-whitespace-from-each-line-of-some-output
#shopt -s extglob #setopt: no such option: extglob
#cmd=${cmd##+([[:space:]])}
#cmd=${cmd%%+([[:space:]])}
cmd=${cmd##[[:space:]]}
cmd=${cmd%%[[:space:]]}
echo 'cmd(dnldck(){}):['$cmd']'
eval " $cmd"
# urls_whitespaced=""
fi
done
echo '$?':$?
echo «$(\date +"%Y%m%d %H%M%S.%N%::z%Z %A")»
}
#append to todo file
function +() {
if (($# > 0));then
#Don't quote args
#WARNING: The url doesn't specify the protocol, trying with http
#[generic] watch?v=B2XtqVZcSdM': Requesting header
#WARNING: Could not send HEAD request to http://'https://www.youtube.com/watch?v=B2XtqVZcSdM': <urlopen error [Errno -2] Name or service not known>
#[generic] watch?v=B2XtqVZcSdM': Downloading webpage
#ERROR: Unable to download webpage: <urlopen error [Errno -2] Name or service not known> (caused by URLError(gaierror(-2, 'Name or service not known'),))
#local uniq_args=$(gawk -v quot="'" 'BEGIN {RS=ORS="\n"} !seen[$0]++ {printf "%s%s%s\n", quot, $0, quot}' <<< "$@")
local uniq_args=$(gawk 'BEGIN {RS=ORS=" "} !seen[$1]++ {print $1} END {print "\n"}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
local outfile="/mnt/0/gthb/note/.todo"
echo 'outfile:['$outfile']'
echo "$uniq_args" >> "$outfile"
#https://unix.stackexchange.com/questions/194780/remove-duplicate-lines-while-keeping-the-order-of-the-lines
cat -n < "$outfile" | sort -k2 -k1n | uniq -f1 | sort -nk1,1 | cut -f2- > _tmp.todo_ && command mv -f _tmp.todo_ "$outfile"
echo '$?':$?
fi
}
#append to youtube download file
function dl+() {
if (($# > 0));then
local dllst_file="dllst"
#Don't quote args
#WARNING: The url doesn't specify the protocol, trying with http
#[generic] watch?v=B2XtqVZcSdM': Requesting header
#WARNING: Could not send HEAD request to http://'https://www.youtube.com/watch?v=B2XtqVZcSdM': <urlopen error [Errno -2] Name or service not known>
#[generic] watch?v=B2XtqVZcSdM': Downloading webpage
#ERROR: Unable to download webpage: <urlopen error [Errno -2] Name or service not known> (caused by URLError(gaierror(-2, 'Name or service not known'),))
#local uniq_args=$(gawk -v quot="'" 'BEGIN {RS=" "} !seen[toupper($1)]++ {printf "%s%s%s\n", quot, $1, quot} END {print ""}' <<< "$@")
local uniq_args=$(gawk 'BEGIN {RS=" ";ORS="\n"} !seen[$1]++ {print $1}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
if [[ -f $dllst_file ]];then
# gawk '!seen[toupper($0)]++{exit 1}' $dllst_file || (
# echo 'dl+(){} found duplicate lines, remove duplicates now.'
# gawk '!seen[toupper($0)]++' $dllst_file > _tmp.dnld_append_ && command mv -f _tmp.dnld_append_ $dllst_file
# )
cat -n <<< $uniq_args | sort -k2 -k1n | uniq -f1 | sort -nk1,1 | cut -f2- > _tmp.dnld_append_ && command mv -f _tmp.dnld_append_ $dllst_file
#https://unix.stackexchange.com/questions/194780/remove-duplicate-lines-while-keeping-the-order-of-the-lines
#https://unix.stackexchange.com/questions/39291/run-a-command-that-is-shadowed-by-an-alias
# local IFS=$'\n'
# local url=""
# for url in "$uniq_args";do
# # url="${url%&list=*}"
# # url="${url%&index=*}"
# command grep -F "$url" $dllst_file || echo "$url" >> $dllst_file
# #command grep -qF "$url" $dllst_file || echo "$url" >> $dllst_file
# done
#https://stackoverflow.com/questions/46408707/fastest-way-appending-a-line-to-a-file-only-if-it-does-not-already-exist
# gawk 'FNR==NR {uniq_args[$0]; next}
# $0 in uniq_args{delete uniq_args[$0]}
# 1
# END{for (line in uniq_args) print line >> FILENAME}' <<< $uniq_args $dllst_file
else
# gawk -v quot="'" 'BEGIN {RS=" ";ORS="\n"} !seen[toupper($1)]++ {printf "%s%s%s\n", quot, $1, quot}' <<< $uniq_args >> $dllst_file
gawk 'BEGIN {RS=ORS="\n"} !seen[$0]++ {print $0}' <<< "$uniq_args" >> $dllst_file #already quoted in $uniq_args
fi
# gawk -v quot="'" 'BEGIN {RS=" ";ORS="\n"} !seen[toupper($1)]++ {printf "%s%s%s\n", quot, $1, quot}' <<< "$@" >> $dllst_file
# gawk -v quot="'" 'BEGIN {RS=" ";ORS="\n"} !seen[toupper($0)]++{printf "%s%s%s\n", quot, $0, quot}' <<< "$@" >> $dllst_file #last line won't be appended with spaces, so always unique, hence faulty!
# gawk 'BEGIN {IRS=" ";ORS="\n"} !seen[toupper($0)]++ END {print ""}' <<< "$@" >> $dllst_file
# ^ syntax error
echo '$?':$?
fi
}
function 0() {
if (($# > 0));then
#$(gawk -v quot="'" 'BEGIN {RS=" "} !seen[$1]++ {print quot$1quot}' <<< "$@")
local uniq_args=$(gawk -v quot="'" 'BEGIN {RS=" ";ORS="\n"} !seen[$1]++ {printf "%s%s%s\n", quot, $1, quot}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
local IFS=$'\n'
local file=""
for file in "$uniq_args";do
if [[ -f $file ]];then
command mv -f "$file" "$file.bk"
echo -n > "$file"
fi
done
echo '$?':$?
fi
}
function utlsfrm() {
if (($# > 0));then
# local uniq_args=$(gawk -v quot="'" 'BEGIN {RS=" ";ORS="\n"} !seen[toupper($1)]++ {printf "%s%s%s\n", quot, $1, quot} END {print ""}' <<< "$@")
local uniq_args=$(gawk 'BEGIN {RS=ORS=" "} !seen[$1]++ {print $1}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
setopt sh_word_split
setopt SH_WORD_SPLIT
local IFS=$'\n'
local url=""
for url in "$uniq_args";do
youtube-dl -v --youtube-skip-dash-manifest --prefer-ffmpeg --list-formats $url
done
unsetopt sh_word_split
unsetopt SH_WORD_SPLIT
# youtube-dl -v --youtube-skip-dash-manifest --list-formats $uniq_args #only first arg get handled
echo '$?':$?
fi
}
function dprx() {
if (($# > 0));then
local uniq_args=$(gawk -v quot="\"" 'BEGIN {RS=" ";ORS="\n"} !seen[$1]++ {printf "%s%s%s\n", quot, $1, quot}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
setopt sh_word_split
setopt SH_WORD_SPLIT
local arg=""
local IFS=$'\n'
for arg in "$uniq_args";do
#if [[ $arg =~ ^(http|HTTP)s?$ ]];then
if [[ $arg =~ (http|HTTP)s? ]];then
echo '$arg:['$arg'] hit regex =~ (http|HTTP)[s]?'
unset http_proxy
unset HTTP_PROXY
#elif [[ $arg =~ ^(ftp|FTP)$ ]];then
elif [[ $arg =~ (ftp|FTP) ]];then
echo '$arg:['$arg'] hit =~ (ftp|FTP)'
unset ftp_proxy
unset FTP_PROXY
#elif [[ $arg =~ ^(sock|SOCK).*$ ]];then
elif [[ $arg =~ (sock|SOCK).* ]];then
echo '$arg:['$arg'] hit =~ (sock|SOCK).*'
unset socks_proxy
unset SOCKS_PROXY
else
echo '$arg:['$arg'] unrecognized protocol!'
fi
done
unsetopt sh_word_split
unsetopt SH_WORD_SPLIT
fi
prprx
}
function prprx() {
echo "http_proxy:[$http_proxy]"
echo "https_proxy:[$https_proxy]"
echo "ftp_proxy:[$ftp_proxy]"
echo "socks_proxy:[$socks_proxy]"
echo "no_proxy:[$no_proxy]"
echo "HTTP_PROXY:[$HTTP_PROXY]"
echo "HTTPS_PROXY:[$HTTPS_PROXY]"
echo "FTP_PROXY:[$FTP_PROXY]"
echo "SOCKS_PROXY:[$SOCKS_PROXY]"
echo "NO_PROXY:[$NO_PROXY]"
}
function klnsys() {
sudo find /var/cache/PackageKit/ -name '*.rpm' -execdir rm {} + #probably Fedora only
rm -rf ~/.Trash ~/.thumbnails ~/.Thumbnails ~/.local/share/Trash ~/.local/share/trash
#sudo dnf autoremove #many times faultily prompt to remove kernel-modules-extra which will be reinstalled on next system upgrade!
#sudo apt autoremove -y; sudo apt autoclean; sudo apt clean
#E: encountered a section with no package: header
#E: problem with Mergelist/var/lib/apt/lists
#sudo rm -Rf /var/lib/apt/lists/* -vf
echo '$?':$?
}
function upgrlog() {
#todo:
#⓪ display systemd upgrade cmd/script log: failed to get non-admin user and root systemd service to run OS update cmd/script while SELinux is enforced!
#① display cron job upgrade cmd/script log: unfavorable and inflexible because system login event is undeterministic!
if [[ -f /var/log/sys_upgrade.log ]];then
less /var/log/sys_upgrade.log
elif [[ -f /var/log/youtube-dl_upgrade.log ]];then
less /var/log/youtube-dl_upgrade.log
fi
}
function vln() {
local uniq_args=$(gawk 'BEGIN {RS=" ";ORS="\n"} !seen[$1]++ {print $1}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
#todo: window position setting (minimize or maximize) based on file type or pass as args together with playback speed
local audio_only_flg=""
local wndw_pos=""
local audiofrmt="(mp3|flac|m4a|opus|aac|wav)"
local videofrmt="(mp4|mkv|webm|mpg|mpeg|m2ts|MTS|CPI|clpi|MPL|mpls|BDM|bdmv|rmvb|avi|dvi|ogg)"
#if [[ $* =~ mp3\|flac\|aac\|wav ]];then
if [[ $* =~ $audiofrmt ]];then
audio_only_flg="--qt-start-minimized"
echo "hit [[ $* =~ $audiofrmt ]]"
#elif [[ $* =~ mp4\|mkv\|webm\|mpg\|avi ]];then
elif [[ $* =~ $videofrmt ]];then
#wndw_pos="--video-x 0 --video-y 0" #place VLC in top left corner
echo "hit [[ $* =~ $videofrmt ]]"
#todo: return immediately when hit non-av file
#else
# echo "error: invalid input files, please specify valid media files, and try again."
# return 113
fi
nohup vlc --no-metadata-network-access --rate 1.3 --qt-start-minimized $uniq_args &> /dev/null & disown
#nohup vlc --no-metadata-network-access --rate 1.5 "$audio_only_flg" "$wndw_pos" $uniq_args &> /dev/null & disown
}
function vlopt() {
vlc --longhelp --advanced | less
}
function utup() {
setopt nocasematch
echo «$(\date +"%Y%m%d %H%M%S.%N%::z%Z %A")»
local return_code=113
local out_msg="error"
local err_counter=0
while (($return_code != 0)) || [[ $out_msg =~ error* ]];do #1.ERROR: can't find the current version. Please try again later. 2.Updating to version 2016.08.22 ...\nERROR: unable to download latest version (for error 2,better to detect network connection and return if connection lost)
#while (($return_code <> 0)) || [[ $out_msg =~ error ]];do #bad math expression: operand expected at `> 0'
if ((err_counter++ > 9));then
return 113
fi
out_msg=$(sudo youtube-dl -U)
return_code=$?
echo "err_counter:$err_counter"
echo 'regxpr:$match:['$match'],$MATCH:['$MATCH']'
echo "youtube-dl -U:RC:$return_code"
echo "youtube-dl -U:output message:$out_msg"
done
echo «$(\date +"%Y%m%d %H%M%S.%N%::z%Z %A")»
}
function fx() {
local uniq_args=$(gawk 'BEGIN {RS=ORS=" "} !seen[$1]++ {print $1}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
nohup firefox --ssl-version-min=tls1.1 -private $uniq_args &> /dev/null & disown
}
function krm() {
local uniq_args=$(gawk 'BEGIN {RS=ORS=" "} !seen[$1]++ {print $1}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
nohup google-chrome-stable --ssl-version-min=tls1.1 https://medium.com/@abhimuralidharan/functional-swift-all-about-closures-310bc8af31dd https://medium.com/@abhimuralidharan/higher-order-functions-in-swift-filter-map-reduce-flatmap-1837646a63e8 https://medium.com/@Dougly/higher-order-functions-in-swift-sorted-map-filter-reduce-dff60b5b6adf https://encrypted.google.com/search\?hl=en\&as_q=c%2B%2B+17+parse+command+line+arguments+as+float https://github.com/fffaraz/awesome-cpp https://gist.github.com/TSiege/cbb0507082bb18ff7e4b https://github.com/tommybennett/algorithm-mnemonics/blob/master/algorithm_mnemonics.xml https://encrypted.google.com/search\?hl=en\&q=c%2B%2B+input+sanitization https://encrypted.google.com/search\?hl=en\&q=c%2B%2B+macro https://github.com/ben-strasser/fast-cpp-csv-parser https://www.rosettacode.org/wiki/Web_scraping http://www.bfilipek.com/2014/12/top-5-beautiful-c-std-algorithms.html https://channel9.msdn.com/Events/GoingNative/2013/Cpp-Seasoning $uniq_args &> /dev/null & disown
# https://www.youtube.com/watch\?v=dN_gQYo9Uf8 https://www.youtube.com/watch\?v=5dJSZLmDsxk https://www.youtube.com/watch\?v=i-xqRDwpilM https://www.youtube.com/watch\?v=Az0J2UHbNCI https://www.youtube.com/watch\?v=WDn-htpBlnU
#https://encrypted.google.com/search\?hl=en\&as_q=bash+zsh+hashset+hashmap https://unix.stackexchange.com/questions/48505/how-to-make-sure-only-one-instance-of-a-bash-script-runs https://isocpp.org/faq https://encrypted.google.com/search\?hl=en\&as_q=python+3+book https://stackoverflow.com/questions/16807011/python-how-to-identify-if-a-variable-is-an-array-or-a-scalar http://bisqwit.iki.fi/ http://docs.python-guide.org/en/latest http://python-future.org/what_else.html#what-else https://dzone.com/articles/writing-a-web-service-using-python-flask https://www.reddit.com/r/learnpython/comments/264ffw/what_is_the_pythonic_way_of_storing_credentials https://docs.python.org/3/library/email.examples.html https://docs.python.org/2/library/email-examples.html https://stackoverflow.com/questions/34668240/email-an-attachment-with-non-ascii-filename-with-python-email https://developers.google.com/gmail/api/guides/sending https://encrypted.google.com/search\?hl=en\&as_q=programming+loop+unroll https://en.wikipedia.org/wiki/Loop_unrolling
#nohup google-chrome-stable --ssl-version-min=tls1.1 http://www.thechessdrum.net/chessacademy/index.html http://www.thechessdrum.net/65thSquare/65_janfeb04.html http://www.cppsamples.com http://docs.python-guide.org/en/latest https://github.com/line/line-bot-sdk-python http://python-future.org/automatic_conversion.html http://python-future.org/compatible_idioms.html#compatible-idioms http://python-future.org/what_else.html#what-else https://dzone.com/articles/writing-a-web-service-using-python-flask "$@" &> /dev/null & disown
}
function s0() {
if ((0 < $#));then
#local uniq_args=$(gawk 'BEGIN {RS=ORS=" "} !seen[$1]++ {print $1}' <<< "$@")
local uniq_args=$(gawk 'BEGIN {RS=" ";ORS="\n"} !seen[$1]++ {print $1}' <<< "$@")
#https://stackoverflow.com/questions/15108229/how-to-count-number-of-words-from-string-using-shell
#https://stackoverflow.com/questions/1469849/how-to-split-one-string-into-multiple-strings-separated-by-at-least-one-space-in
#https://stackoverflow.com/questions/1975849/how-to-split-a-line-into-words-separated-by-one-or-more-spaces-in-bash
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
echo '${uniq_args[@]}:['${uniq_args[@]}']'
echo '$uniq_args[@]:['$uniq_args[@]']'
echo '${#uniq_args[@]}:['${#uniq_args[@]}']'
echo '${uniq_args[@]:-1}:['${uniq_args[@]:-1}']'
echo '$#':$#
echo '$0:['$0']'
local noNumber_rexpr="^[^0-9]*$"
#https://stackoverflow.com/questions/1975849/how-to-split-a-line-into-words-separated-by-one-or-more-spaces-in-bash
#use for value in "${var[@]}" in this context instead of an index variable. While in this case the array may be contiguous, Bash supports sparse arrays and ${#var[@]} may not be the last entry (although ${var[@]: -1} will be and indices=(${!a[@]}); count=${#indices[@]} will give the list of indices and the correct count)
#local i=${#uniq_args[@]} #wrong! 90 -> [9] [0] -> take 0 and shutdown immediately!
#local i=$#
#for ((; 0 < i; --i));do
#todo: Failed to parse time specification: 99 50 a
#http://www.refining-linux.org/archives/38/ZSH-Gem-3-No-automatic-word-split/
setopt sh_word_split
setopt SH_WORD_SPLIT
#local IFS=' '
local IFS=$'\n'
#todo: backward for-each
for tm in $uniq_args;do
#if [[ $i =~ [0-9]+ ]];then
if [[ ! $tm =~ $noNumber_rexpr ]];then
#echo 'sudo shutdown -P -f ${uniq_args[i]}:['${uniq_args[i]}']'
#sudo shutdown -P -f ${uniq_args[i]}
#echo 'sudo shutdown -P -f $i:['$i']'
#sudo shutdown -P -f $i
echo 'sudo shutdown -P -f $tm:['$tm']'
sudo shutdown -P -f $tm
break
fi
done
unsetopt sh_word_split
unsetopt SH_WORD_SPLIT
else
sudo shutdown -P -f 0
fi
}
function ss() {
#trap on EXIT, that way, time(s) will be called whenever the shell exits and the exit status will be preserved.
#http://unix.stackexchange.com/questions/52313/how-to-get-execution-time-of-a-script-effectively
trap time EXIT
echo «$(\date +"%Y%m%d %H%M%S.%N%::z%Z %A")»
#todo: detect OS
#sudo apt update && sudo apt upgrade -y
#Skipping packages with conflicts: (add '--best --allowerasing' to command line to force their upgrade)
sudo dnf upgrade --best --allowerasing -y
upgrade_oh_my_zsh
echo '$?':$?
#youtube-dl upgrade
utup
echo '$?':$?
}
function detectOS() {
#todo: detect OS(Ubuntu, Debian, Mint, kali, RHEL, Centos, Fedora, Suse, etc.)
#https://github.com/dmytro/sherlock_os/blob/master/bin/sherlock
#https://danielgibbs.co.uk/2013/04/bash-how-to-detect-os/
local arch=$(uname -m)
local kernel=$(uname -r)
local distroname=""
if [ -n "$(command -v lsb_release)" ]; then
distroname=$(lsb_release -s -d)
elif [ -f "/etc/os-release" ]; then
distroname=$(grep PRETTY_NAME /etc/os-release | sed 's/PRETTY_NAME=//g' | tr -d '="')
elif [ -f "/etc/debian_version" ]; then
distroname="Debian $(cat /etc/debian_version)"
elif [ -f "/etc/redhat-release" ]; then
distroname=$(cat /etc/redhat-release)
else
distroname="$(uname -s) $(uname -r)"
fi
echo "distroname:${distroname}"
}
function putfio() {
if (($# == 1));then
curl -F "file=@$1" 'https://file.io/?expires=3d'
echo '$?':$?
elif (($# == 2)) && [[ $2 =~ [1-9][0-9]{0,2}[dmwDMW] ]];then
echo ' =~ [1-9][0-9]{0,2}[dmwDMW] :$MATCH:['$MATCH'],array $match:['$match']'
curl -F "file=@$1" "https://file.io/?expires=$2"
echo '$?':$?
else
echo "usage: putfio <id> [(expires=)<9d/3m/7w>]"
#define EX_USAGE 64 /* command line usage error */
return 64
fi
}
function getfio() {
if (($# == 2));then
curl -o "$2.7z" "https://file.io/$1"
echo '$?':$?
elif (($# == 1));then
curl -o "$1.7z" "https://file.io/$1"
echo '$?':$?
else
echo "usage: getfio <id> [output_file_name_without_suffix]"
#define EX_USAGE 64 /* command line usage error */
return 64
fi
}
function srv() {
#detect OS(Ubuntu, Debian, Mint, kali, RHEL, Centos, Fedora, Suse, etc.)
if (($# == 2));then
if [[ "$1" == status ]];then
sudo systemctl -al status "$2"
fi
sudo systemctl "$1" "$2"
fi
echo '$?':$?
#apache2, same for httpd
#alias apstt='sudo systemctl -l status apache2'
#alias apstp='sudo systemctl stop apache2'
#alias apstr='sudo systemctl start apache2'
#alias aprst='sudo systemctl restart apache2'
#alias aprld='sudo systemctl reload apache2'
#alias apstt='sudo /etc/init.d/apache2 status'
#alias apstp='sudo /etc/init.d/apache2 graceful-stop'
#alias apstr='sudo /etc/init.d/apache2 start'
#alias aprst='sudo /etc/init.d/apache2 restart'
#alias aprld='sudo /etc/init.d/apache2 reload'
#mysql, mariadb
#alias mystt='sudo systemctl -a status mysql'
#alias mystp='sudo systemctl stop mysql'
#alias mystr='sudo systemctl start mysql'
#alias myrst='sudo systemctl restart mysql'
}
function srvstr() {
local uniq_args=$(gawk -v quot="'" 'BEGIN {RS=" ";ORS="\n"} !seen[$1]++ {printf "%s%s%s\n", quot, $1, quot}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
local srv=""
for srv in $uniq_args;do
sudo service "$srv" start
echo '$?':$?
done
}
function srvstp() {
local uniq_args=$(gawk -v quot="'" 'BEGIN {RS=" ";ORS="\n"} !seen[$1]++ {printf "%s%s%s\n", quot, $1, quot}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
local srv=""
for srv in $uniq_args;do
sudo service "$srv" stop
echo '$?':$?
done
}
#weather
function wttr() {
if ((0 < $#));then
local uniq_args=$(gawk 'BEGIN {RS=" ";ORS="\n"} !seen[$1]++ {print $1}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
local arg=""
for arg in $uniq_args;do
if [[ $arg =~ [a-zA-Z0-9.]*[a-zA-Z]+ ]];then
curl wttr.in/@$arg
elif [[ $arg =~ [a-zA-Z0-9]* ]];then
curl wttr.in/$arg
elif [[ $arg =~ [/-]h ]];then
curl wttr.in/:help
fi
echo '$?':$?
done
else
curl wttr.in
echo '$?':$?
fi
}
#primitive ffmpeg av rewind fast-forward: w->v, "rvndff" may imply "rvn"+"dff"
function avrvnff() {
if (($# < 1 || $# > 3)) || (($# == 3)) && [[ ! ($2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$) ]];then # { (($# == 3)) && [[ ! ($2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$) ]] }? http://unix.stackexchange.com/questions/88850/precedence-of-the-shell-logical-operators
if (($# == 3)) && [[ ! ($2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$) ]];then
echo '※hit "...|| (($# == 3)) && [[ ! ($2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$) ]]" #better { (($# == 3)) && [[ ! ($2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$) ]] }?'
fi
echo "usage: avrvnff input_file [(optional)speed factor(2.0(default) 1.75 2.25 2.5...)] [(optional)output file name prefix]"
#define EX_USAGE 64 /* command line usage error */
return 64
fi
echo '$#':$#
local idx=0
local arg=""
for arg in "$@";do
echo '$'$((++idx)):'['$arg']'
done
local infile=""
local outfile=""
local factor=""
if [[ ! -f $1 ]];then
echo "please specify a valid local file name, and try again."
return 113
else
infile="$1"
if (($# == 1));then
factor="2" #default
outfile="${infile%.*}"
outfile+="_spdx"
outfile+=$factor
outfile+="."
suffix="${infile##*.}"
outfile+=$suffix
fi
fi
if (($# == 2));then
#if [[ $2 =~ (^[-+]?[0-9]?[.,][1-9]{1,2}$)|(^[-+]?[1-9]$) ]];then #parse error near `|'
if [[ $2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$ ]];then #http://www.regular-expressions.info/floatingpoint.html
echo 'hit [[ $2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$ ]]'
factor="$2"
outfile="${infile%.*}"
outfile+="_spdx"
outfile+=$factor
outfile+="."
suffix="${infile##*.}"
outfile+=$suffix
elif [[ -e $2 ]];then
echo "error: destination/output file already exist, please specify other name for output, and try again."
return 113
else
outfile="$2"
outfile+="_spdx"
factor="2" #default
outfile+=$factor
outfile+="."
suffix="${infile##*.}"
outfile+=$suffix
fi
fi
if (($# == 3));then
#if [[ $2 =~ (^[-+]?[0-9]?[.,][1-9]{1,2}$)|(^[-+]?[1-9]$) ]];then #parse error near `|'
if [[ $2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$ ]];then
if [[ -e $3 ]];then
echo "error: destination/output file already exist, please specify other name for output, and try again."
return 113
fi
echo 'hit [[ $2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$ ]]'
factor="$2"
outfile="$3"
outfile+="_spdx"
outfile+=$factor
outfile+="."
suffix="${infile##*.}"
outfile+=$suffix
else #? redundant as already checked at function start
echo "usage: avrvnff input_file [(optional)speed factor(2.0(default) 1.75 2.25 2.5...)] [(optional)output file name prefix]"
#define EX_USAGE 64 /* command line usage error */
return 64
fi
fi
echo "#test<"
if (($# == 2)) && [[ $2 == ^[-+]?[0-9]?[.,]?[1-9]{1,2}$ ]];then
echo 'hit (($# == 2)) && [[ $2 == ^[-+]?[0-9]?[.,]?[1-9]{1,2}$ ]]'
fi
# [[ != ]] literal string comparison?
if (($# == 2)) && [[ $2 != ^[-+]?[0-9]?[.,]?[1-9]{1,2}$ ]];then
echo 'ERRONEOUS/WRONG! hit (($# == 2)) && [[ $2 != ^[-+]?[0-9]?[.,]?[1-9]{1,2}$ ]], [[ != ]] literal string comparison?'
fi
if (($# == 2)) && [[ ! ($2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$) ]];then
echo 'hit (($# == 2)) && [[ ! ($2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$) ]] #"[[ ! (xpr) ]]'
fi
if (($# == 2)) && [[ ! ( $2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$ ) ]];then
echo 'hit (($# == 2)) && [[ ! ( $2 =~ ^[-+]?[0-9]?[.,]?[1-9]{1,2}$ ) ]] #"[[ ! ( xpr ) ]]'
fi
echo "#test>"
#http://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters
#infile_singlequoted=$(printf %s. "$infile" | sed "s/'/'\\\\''/g")
#outfile_singlequoted=$(printf %s. "$outfile" | sed "s/'/'\\\\''/g")
#'How'\''s Do One-Way Mirrors Work-4kKL32opewI.mp4' #output by ll on Fedora24+
local infile_singlequoted=$(printf \'%s\' "$(printf %s "$infile" | sed "s/'/'\\\\''/g")")
local outfile_singlequoted=$(printf \'%s\' "$(printf %s "$outfile" | sed "s/'/'\\\\''/g")")
echo '"${infile_singlequoted}"':"${infile_singlequoted}"
echo '"${outfile_singlequoted}"':"${outfile_singlequoted}"
if [[ -e ${outfile_singlequoted} ]];then
echo "error: destination/output file already exist, please specify other name for output, and try again."
return 113
fi
local cmd='ffmpeg -i '
cmd+="${infile_singlequoted}" #"$infile_singlequoted"
cmd+=' -filter:v "setpts=PTS/'
cmd+=$factor
cmd+='" -filter:a "atempo='
cmd+=$factor
cmd+='" '
cmd+="${outfile_singlequoted}" #"$outfile_singlequoted"
#cmd=${cmd//\'/\\\'} #eval: unmatched '
#cmd=${cmd//*/\*}
echo '$cmd:['$cmd']'
eval " $cmd"
echo '$?':$?
}
function appvr() {
uname -a
vim --version
nvim --version
gdb --version
gcc --version
g++ --version
git --version
as --version #GNU assembler
openssl version
ssh -V
libreoffice --version
ffmpeg -version
gimp --version
vlc --version
google-chrome-stable --version
xmllint --version
echo "node --version":$(node --version)
echo "youtube-dl --version":$(youtube-dl --version)
#echo "python --version":$(python --version) #python outputs its name automatically!
echo $(python --version) #'echo' spaces with an empty line
perl --version
}
# ≈ MS Word
function wrtr() {
local uniq_args=$(gawk 'BEGIN {RS=" ";ORS="\n"} !seen[$1]++ {print $1}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
local args=""
local file=""
for file in $uniq_args;do
if [[ -d $file ]];then
echo "error: directory with name \`$file' already exist, unable to edit directories, please specify other file name, and try again."
return 113
elif [[ ! -e $file ]];then
touch $file
fi
args+="$file "
done
args=${args%% }
nohup libreoffice --writer $args &> /dev/null & disown
}
function sprsht() {
#local uniq_args=$(gawk 'BEGIN {RS=ORS="\n"} !seen[$0]++ {print $0}' <<< "$@")
local uniq_args=$(gawk 'BEGIN {RS=ORS=" "} !seen[$1]++ {print $1}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
nohup libreoffice --calc $uniq_args &> /dev/null & disown
}
# ≈ MS Powerpoint
function slid() {
#local uniq_args=$(gawk 'BEGIN {RS=ORS="\n"} !seen[$0]++ {print $0}' <<< "$@")
local uniq_args=$(gawk 'BEGIN {RS=ORS=" "} !seen[$1]++ {print $1}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
nohup libreoffice --impress $uniq_args &> /dev/null & disown
}
#view pdf
function vns() {
local uniq_args=$(gawk 'BEGIN {RS=ORS=" "} !seen[$1]++ {print $1}' <<< "$@")
echo 'uniq_args before func trmsp() called:['$uniq_args']'
uniq_args=$(trmsp $uniq_args)
echo 'uniq_args after func trmsp() called:['$uniq_args']'
nohup evince $uniq_args &> /dev/null & disown
}
function mkcd() {
if [[ -z $1 ]] || [[ ! -n $1 ]];then
echo "error: please specify a valid directory name and try again."
return 113
elif [[ -d $1 ]];then
echo "error: destination directory \`$1' already exist, please specify other name and try again."
return 113
else
mkdir -p -- "$1" &&
cd -P -- "$1"
#BASH BUILTIN COMMANDS
#Unless otherwise noted, each builtin command documented in this section as accepting options preceded by - accepts -- to signify the end of the options.
#mkdir -p "$1" &&
#cd "$1" #cd: line number : no such file or directory