2 @ online shell program check
3 https://www.shellcheck.net/#
4 # or use compile check in shell programmar.
9 # use "" around $envar, if there is ' ' in envar or not
10 # use ${envar} instead of $envar, when ${envar}_xxx.
14 ======================================================================
17 echo ${#aaa} ${aaa+123} $((1+2)) # it's in normal.
31 @ { } codeblock does not launch as a sub-proc
48 ======================================================================
58 ======================================================================
65 # strstr(), regexstr(), pfx of p/n.
68 # inner cmd of declare, implmented by eval in func.
70 + '#', '##', '%', '%%', '//', '//', '^', '^^', ',', ',,',
72 + array cnt, array content,
75 + defination(decl)/unset/access(@)
79 # subscript in/out re-dir
84 ======================================================================
88 # test $abc = 111 ==> test "$abc" = 111
92 # [[ $STR1" == "$STR2" ]]
93 test "$STR1" = "$STR2"
94 # [[ "$STR1" == "$STR2" || [[ "$STR1 == "$STR3" ]]
95 test "$STR1" = "$STR2" -o "$STR1" = "$STR3"
96 # [[ "$STR1" == "$STR2" && [[ "$STR1" == "$STR3" ]]
97 test "$STR1" = "$STR2" -a "$STR1" = "$STR3"
103 # [[ "$file1" -ot "$file2" ]]
107 # [[ ${ENVAR} =~ xxx ]]
108 IFS=$'\n' BASH_REMATCH=( `cat "${ENVAR}" | grep -E "([[:alnum:]]_)*_test\.c"` )
109 expr "${ENVAR}" : "([[:alnum:]]_)*_test\.c"
110 echo "$ENVAR " | grep -o -P "xxx" >/dev/null 2>&1
111 # [[ "$CONTENT" =~ $FMT ]]
112 FMT="[([[:alnum:]_]*)]"
114 cat $CONTENT | grep -E $FMT
115 cat $CONTENT | sed -E "s/$FMT/$OUTFMT/p"
116 eg: code in args.shlib==>str_dispatch()
120 # echo ${#aaa} (valid)
123 expr substr "${aaa}" 1 2
125 echo "${aaa}" | sed -E "s/a/b/g"
126 # echo ${aaa#*a} (valid)
127 echo "${aaa}" | sed -E "s/^.*a//g"
128 # echo ${aaa##*a} (valid)
129 echo "${aaa}" | sed -E "s/^(.*a)+//g"
131 echo "${aaa}" | tr [[:upper:]] [[:lower:]]
133 echo "${aaa}" | tr [[:lower:]] [[:upper:]]
139 # echo abc > file1.txt
140 # echo abc >> file1.txt
146 echo "string" | cat -
148 # echo abc >&/tmp/file.tmp
149 # eval exec $fd<>$file
155 # diff <(echo abc) <(echo abcd)
163 for (( i=$((i-1)); i>0; i-- )); do
164 [[ ! -e /proc/self/fd/$i ]] && break
171 diff $(redir_out foo1) $(redir_out foo2)
173 @ declare implmented by eval in func.
175 @ ${XXX} '#', '##', '%', '%%'
176 # ${abc%%xxx*} ==> echo "${abc}" | sed -E "s/xxx.*$//g"
177 # ${abc%xxx*} ==> echo "${abc}" | sed -E "s/(.*)xxx.*$/\1/g"
178 # ${abc##*xxx} ==> echo "${abc}" | sed -E "s/.*xxx(.*)$/\1/g"
179 # ${abc#*xxx} ==> echo "${abc}" | sed -E "s/.*xxx(.*)$/\1/g"
180 # ${!abc} ==> eval echo "${abc}"
182 @ array in defination
183 # use grep to get defination string
186 : [=][(](.*(['][^']['])*|(["][^"]["])*|([`][^'][`])*.*)*[)]
211 [=][(](.*(['][^']['])*|(['][^']['])*.*)[)]
213 str="SRCPKG_LICENSE=( AAA
215 'MIT' 'Apache-2.0' 'BSD-3-Clause'
216 'GPL-3.0' 'LGPL-3.0' 'MPL-2.0'
217 'GPL' 'GPL-2.0' 'ISC' 'PMIT')abc"
218 devenkong@ubuntu:~$ echo "$str" | grep -zoE "[\=][\(]([^']*['][^']*['])*\)"
221 'MIT' 'Apache-2.0' 'BSD-3-Clause'
222 'GPL-3.0' 'LGPL-3.0' 'MPL-2.0'
223 'GPL' 'GPL-2.0' 'ISC' 'PMIT')abc
228 # an easier and limited way to use array
229 https://www.baeldung.com/linux/posix-shell-array
233 $ set -- "$@" a # add item
234 $ shift # Remove item
236 abc[1]=string # report error
237 set abc[1]=string # it's ok
238 set abc[1]=string # it's ok
245 [[ -z "$1" ]] && return
247 for ((i=2; i<$cnt; i++)); do
248 eval set ${1}_${i}="\$$i"
253 [[ $2 != '@' ]] && eval echo -ne \"\${${1}_${2}}\" && return
259 [[ -z "$1" ]] && return
261 for ((i=2; i<$cnt; i++)); do
262 eval tmp=\"\${${1}_${i}}\"
263 [[ -z $tmp ]] && tmp="''"
264 echo "$tmp" | grep -e " " >/dev/null 2>&1
265 [[ $? == 0 ]] && tmp="'$tmp'"
279 [[ -z "$1" ]] && return
281 for ((i=2; i<$cnt; i++)); do
291 [[ -z "$1" ]] && return
293 for ((i=2; i<$cnt; i++)); do