File indexing completed on 2024-05-12 04:02:11

0001 #! /bin/sh
0002 # This is a test script for the Katepart Bash Syntax Highlighting by
0003 #       Wilbert Berendsen.  This is not runnable !!!
0004 
0005 
0006 # The highlighting recognizes basic types of input, and has a few special cases that
0007 # are all in FindCommands.  The main objective is to create really proper nesting of
0008 # (multiline) strings, variables, expressions, etc.
0009 
0010 
0011 
0012 # ============== Tests: ===============
0013 
0014 # basic types:
0015 echo 'single quoted string'
0016 echo "double quoted string"
0017 echo $'string with esc\apes\x0din it'
0018 echo $"string meant to be translated"
0019 echo "$"
0020 
0021 
0022 # comments:
0023 # this is a comment
0024 #this too
0025 echo this is#nt a comment
0026 dcop kate EditInterface#1 #this is
0027 grep -e "^default/linux/amd64/" |\ #this is not a comment but #this is
0028 mkdir this\ isnt\ #a\ comment
0029 mkdir this\ isnt\\\;#a\ comment
0030 mkdir this\\ #is a comment
0031 
0032 <<'#BLOCK-COMMENT'
0033 NOTE This is the "miltiline" comment.
0034 
0035 [===[.rst:
0036 
0037 Docs
0038 ----
0039 
0040 Documentation block in ``RST`` format **starts** *here*.
0041 
0042 ]===]
0043 #BLOCK-COMMENT
0044 
0045 : << '#SC2188'
0046 [====[.rst:
0047 The *multiline* comment does not trigger the SC2188_ warning of `shellcheck`.
0048 
0049 .. _SC2188: https://www.shellcheck.net/wiki/SC2188
0050 ]====]
0051 
0052 ... continue with _normal_ Bash comment.
0053 #SC2188
0054 
0055 # brace expansion
0056 mv my_file.{JPG,jpg}
0057 echo f.{01..100..3} f.{Z..a..-3}
0058 echo f.{01..100} f.{a..Z}
0059 # no brace expansion
0060 echo f.{..100} f.{1..Z} f.{a..Z..}
0061 
0062 
0063 # special characters are escaped:
0064 echo \(output\) \&\| \> \< \" \' \*
0065 
0066 
0067 # variable substitution:
0068 echo $filename.ext
0069 echo $filename_ext
0070 echo ${filename}_ext
0071 echo text${array[$subscript]}.text
0072 echo text${array["string"]}.text
0073 echo ${!prefix*}
0074 echo ${!redir} ${!3} ${!#} ${!##} ${!@###} ${!}
0075 echo short are $_, $$, $?, ${@}, etc.
0076 echo ${variable/a/d}
0077 echo ${1:-default}
0078 echo ${10} $10a # 9 is max
0079 
0080 
0081 # expression subst:
0082 echo $(( cd << ed + 1 ))
0083 
0084 
0085 # command subst:
0086 echo $(ls -l)
0087 echo `cat myfile`
0088 
0089 
0090 # file subst:
0091 echo $(<$filename)
0092 echo $(</path/to/myfile)
0093 
0094 # process subst:
0095 sort <(show_labels) | sed 's/a/bg' > my_file.txt 2>&1
0096 
0097 
0098 # All substitutions also work in strings:
0099 echo "subst ${in}side string"  'not $inside this ofcourse'
0100 echo "The result is $(( $a + $b )). Thanks!"
0101 echo "Your homedir contains `ls $HOME |wc -l` files."
0102 
0103 
0104 # Escapes in strings:
0105 p="String \` with \$ escapes \" ";
0106 
0107 
0108 # keywords are black, builtins dark purple and common commands lighter purple
0109 set
0110 exit
0111 login
0112 
0113 
0114 # Other colorings:
0115 error() {
0116         cat /usr/bin/lesspipe.sh
0117         runscript >& redir.bak
0118         exec 3>&4
0119 }
0120 
0121 
0122 # do - done make code blocks
0123 while [ $p -lt $q ]
0124 do
0125         chown 0644 $file.$p
0126 done
0127 
0128 
0129 # braces as well
0130 run_prog | sort -u |
0131 {
0132         echo Header
0133         while read a b d
0134         do
0135                 echo $a/$b/$c
0136         done
0137         echo Footer
0138 }
0139 
0140 
0141 # Any constructions can be nested:
0142 echo "A long string with $(
0143         if [ $count -gt 100 ] ; then
0144                 echo "much"
0145         else
0146                 echo "not much"
0147         fi ) substitutions." ;
0148 
0149 
0150 # Even the case construct is correctly folded:
0151 test -f blaat &&
0152 (       do_something
0153         case $p in
0154                 *bak)
0155                         do_bak $p
0156                         ;;
0157                 *)
0158                         dont_bak $p
0159                         ;;
0160         esac
0161 ) # despite the extra parentheses in the case construction.
0162 
0163 
0164 # more control flow
0165 while :;
0166   break
0167   continue
0168   return
0169 done
0170 
0171 
0172 # variable assignments:
0173 DIR=/dev
0174 p=`ls`
0175 LC_ALL="nl" dcop 'kate*'
0176 _VAR=val
0177 ARR=(this is an array)
0178 ARR2=([this]=too [and]="this too")
0179 usage="$0 -- version $VERSION
0180 Multiple lines of output
0181 can be possible."
0182 ANSWER=yes      # here 'yes' isn't highlighed as command
0183 
0184 
0185 # Some commands expect variable names, these are colored correctly:
0186 export PATH=/my/bin:$PATH BLAAT
0187 export A B D
0188 local p=3  x  y='\'
0189 read x y z <<< $hallo
0190 unset B
0191 declare -a VAR1 VAR2 && exit
0192 declare less a && b
0193 declare a=(1 2)
0194 getopts :h:l::d arg
0195 read #comment
0196 let a=4+4 3+a b=c+3 d+3 d*4 # * is a glob
0197 
0198 # options are recoqnized:
0199 zip -f=file.zip
0200 ./configure  --destdir=/usr
0201 make  destdir=/usr/
0202 
0203 
0204 # [[ and [ correctly need spaces to be regarded as structure,
0205 # otherwise they are patterns (currently treated as normal text)
0206 if [ "$p" == "" ] ; then
0207         ls /usr/bin/[a-z]*
0208 elif [[ $p == 0 ]] ; then
0209         ls /usr/share/$p
0210 fi
0211 
0212 # Fixed:
0213 ls a[ab]*               # dont try to interprete as assignment with subscript (fixed)
0214 a[ab]
0215 a[ab]=sa
0216 
0217 
0218 # Here documents are difficult to catch:
0219 cat > myfile << __EOF__
0220 You're right, this is definitely no bash code
0221 But ls more $parameters should be expanded.
0222 __EOF__
0223 
0224 
0225 # quoted:
0226 cat << "EOF" | egrep "this" >&4   # the rest of the line is still considered bash source
0227 You're right, this is definitely no bash code
0228 But ls more $parameters should be expanded. :->
0229 EOF
0230 
0231 cat <<bla || exit
0232 bla bla
0233 bla
0234 
0235 
0236 # indented:
0237 if true
0238 then
0239         cat <<- EOF
0240                 Indented text with a $dollar or \$two
0241         EOF
0242 elif [ -d $file ]; then
0243         cat <<- "EOF"
0244                 Indented text without a $dollar
0245         EOF
0246 fi
0247 
0248 if ! { cmd1 && cmd2 ; }; then echo ok ; fi
0249 if ! cmd1 arg; then echo ok ; fi
0250 
0251 case 1 in
0252 2) echo xxx;
0253 ;;
0254 ?) foo || yyy ; foo abc || echo abc ;;
0255 1) echo yyy;
0256 esac
0257 
0258 ls #should be outside of case 1 folding block
0259 
0260 for i in `ls tests/auto/output/*.html`; do
0261     refFile=`echo $i | sed -e s,build,src, | sed -e s,output,reference, | sed -e s,.html,.ref.html,`
0262     cp -v $i $refFile
0263 done
0264 
0265 ## >Settings >Configure Kate >Fonts & Colors >Highlitghing Text Styles >Scripts/Bash >Option >Change colors to some distinct color
0266 ## 1- In following line the -ucode should not be colored as option
0267 
0268 pacman -Syu --needed intel-ucode grub
0269 pacman -syu --needed intel-ucode grub
0270 
0271 # Braces (bug ##387915)
0272 [[ $line_name =~ \{([0-9]{1,})\}\{([0-9]{1,})\}(.*) ]]
0273 [[ $name =~ (.*)_(S[0-9]{2})(E[0-9]{2,3}[a-z]{0,1})_(.*) ]]
0274 # Comments in Braces (bug 450878)
0275 [[ # comment 1
0276    1 == 1 # comment 2
0277    # comment 3
0278 ]]
0279 
0280 rm /data/{hello1,hello2}/input/{bye1,$bye2}/si{a,${b},c{k,p{e,a}}}/*.non
0281 rm /data/{aa,{e,i}t{b,c} # Not closed
0282 rm /data/{aa,{e,i}t{b,c}}
0283 rm /data/{aa,{i}}
0284 rm /data{aa{bb{cc{dd}}}}
0285 rm /data{aaa`aaa}aa`aaa}a
0286 
0287 ${array[0]: -7 : +  22  }  ${array[1]: num  }
0288 ${parameter##word} ${parameter%%word} ${parameter^^pattern} ${parameter,,pattern} ${parameter@operator}
0289 
0290 # TODO `
0291 
0292 # commands
0293 abc
0294 cp
0295 :
0296 .
0297 :#nokeyword
0298 path/cmd
0299 ec\
0300 ho
0301 2
0302 {ab}c
0303 {a,b}c
0304 'a'c
0305 $ab
0306 ${ab}c
0307 \ a
0308 !a
0309 {ab}[
0310 {a,b}[
0311 'a'[
0312 \ [
0313 !a[
0314 a{}d
0315 a{bc}d
0316 a{b,c}d
0317 a'b'c
0318 a$bc
0319 a${bc}d
0320 a\ b
0321 a!b
0322 a{}[
0323 a{bc}[
0324 a{b,c}[
0325 a'b'[
0326 a\ [
0327 a!b[
0328 
0329 # commands + params
0330 shortopt -ol -f/fd/fd -hfd/fds - -ol'a'b -f'a'/fd/fd -h'a'fd/fds
0331 longopt --long-a --long-b=value --file=* --file=file* --file=dir/file
0332 longopt --long-a'a'b --long'a'-b=value --fi'a'le=*
0333 noopt 3 3d -f -- -f --xx dir/file
0334 opt param#nocomment ab'a'cd [[ param ]] } { ~a .a #comments
0335 path path/file dir/ / // 3/f a@/ 'a'/b d/'a'b a\ d/f f/f\
0336 ile
0337 path ~ ~/ ~a/ . .. ./a ../a
0338 path /path/* /path/f* /path/f@ /path/f@(|) {a/b} a{b}/c a/b{c} a/{b} a/{b}c
0339 glob ? * ?f *f f* f? **/ ~/* ~* /path/f* 'a'* 'a'f/?
0340 extglob @ @(*) @(f*|f??(f)) f!(+(?(@(*(f)f)f)f)f)f @'a'@(|) a@(?)
0341 subs f! f!! f!s 'a'!s \( $v {a,b} {a} {a}/d {a\,} {a,} {a,\},b} ds/{a,b}sa/s
0342 2 - f -f
0343 !a -f
0344 'a' -f
0345 $a -f
0346 ! cmd
0347 
0348 # coproc command (#460301)
0349 coproc ls thisfiledoesntexist 2>&1
0350 coproc { ls thisfiledoesntexist; read; } 2>&1
0351 coproc mycoproc { awk '{print "foo" $0;fflush()}'; } >&3
0352 
0353 # redirections (prefix)
0354 <<<s cat
0355 <<<'s' cat
0356 <<<'s's cat
0357 <<<s's's cat
0358 <<<s${s}s cat
0359 <<< s${s}s cat
0360 >&2 cat
0361 <f cat
0362 < f cat
0363 2>3 cat
0364 2>&3 cat
0365 2>& 3 cat
0366 2>f cat
0367 &>f cat
0368 
0369 <<<# comment
0370 <# comment
0371 ># comment
0372 <<< # comment
0373 < # comment
0374 > # comment
0375 
0376 # redirections
0377 cat f>2
0378 cat d/f>2
0379 cat d/f >2
0380 cat d/f >& 2
0381 cat >2 d/f
0382 cat > 2
0383 cat <(echo) <(echo a) <(echo a/f) <(echo ) <(echo a ) <(echo a/f )
0384 cat 2>&1 &>f &>>f 2<&1- 2<>f 2<<heredoc
0385 bla bla
0386 heredoc
0387 <<-'h' cat
0388 bla
0389 h
0390 <<"'" cat
0391 bla
0392 '
0393 cat <<heredoc
0394 bla bla
0395 heredoc
0396 cat <<heredoc -a
0397 bla bla
0398 heredoc
0399 r=$(xxx $@ 2>&1)
0400 
0401 cat <<<# comment
0402 cat <# comment
0403 cat ># comment
0404 cat <<< # comment
0405 cat < # comment
0406 cat > # comment
0407 
0408 # branches
0409 cat a|cat
0410 cat a&cat
0411 cat a||cat
0412 cat a&&cat
0413 cat a;cat
0414 cat a | cat
0415 cat a & cat
0416 cat a || cat
0417 cat a && cat
0418 cat a ; cat
0419 cat a'a';cat
0420 
0421 # substitutions
0422 echo '' 'a' '\' "" "a" "\\" "$a" "a""a"'a''a' a'b'c a"b"c a$'\n'c
0423 echo a!bc a{a}b a{b,c}d a{b,{d,e}}d a\ b
0424 echo a$bc a$b/c a${b}c a$((b-3))c a$(b)c a$(a b c)c
0425 echo ${a[*]} ${a[@]} ${a[${b}]} ${a:-x$z} ${a-x} ${a/g} ${a//f/f} ${a//f*/f*}
0426 echo ${a^^l*} ${a,} ${!a} ${#a[1]} ${a:1:$b} $((++i,i--))
0427 echo "${var#lo+(r)em}" x "${var#+(r)em}" x
0428 echo "${var#refs/heads}" x "${var#refs}" x
0429 
0430 [ a ]
0431 [ -f f'f'f ]
0432 [ -f f]'f'f] ]
0433 [ -t 13 ]
0434 [ -t 13] ]
0435 [ -t 13] ]
0436 [ -v abc ]
0437 [ -z abc ]
0438 [ abc -ef abc ]
0439 [ abc -ef abc ]
0440 [ abc-ef -ef abc-ef ]
0441 [ abc == abc ]
0442 [ abc < abc ]
0443 [ abc -eq abc ]
0444 [[ abc -eq abc ]]
0445 [ 1+2 -eq 1+2 ]
0446 [[ 1+2 -eq 1+2 ]]
0447 [ a = b c ]
0448 [[ a = b c ]]
0449 [ -z 1 -a 1 -eq 1 ]
0450 [ 2 -eq 1 -o 1 -eq 1 ]
0451 [[ x =~ a(b c|$)' '{1,}[a[.digit.]] ]]
0452 [[ x =~ [ ] ]]
0453 [[ x =~ ([ ]) ]]
0454 [[ x =~ [ ]]
0455 [[ x =~ ([) ]]
0456 [[ (a =~ a) ]]
0457 [[ a =~ a || a -eq 2 ]]
0458 [[ (a =~ a) || a -eq 2 ]]
0459 [[ (0 -le $b) ]]
0460 [[ ( 0 -le $b ) ]]
0461 [[ ( 0 -le $b || $b -le 100 ) ]]
0462 [[ a<b ]]
0463 [[ a <b ]]
0464 [[ a< b ]]
0465 [[ a < b ]]
0466 [[(! -d .)]]
0467 [[ ! -d . ]]
0468 [[ !(-d .) ]]
0469 [[ -f a || -f b ]]
0470 [[ -f a||-f b ]]
0471 [[ ! (a -eq b) ]]
0472 [ -d `echo .`] ]
0473 [[ -d `echo .`]] ]]
0474 [[ a != b && ${a}a = b${b} ]]
0475 [[
0476   1 -eq 2
0477 ]]
0478 [[ -&&- ]]
0479 [[ ]]
0480 [[ -f ]]
0481 [[ -f [0-9a] ]]
0482 [[ ?*[0-9] = [^0-9] ]]
0483 [[ -f = ?*[0-9] ]]
0484 [[ ?*[0-9] = ?*[0-9] ]]
0485 [[ a/sa[s = dsad?*[0-9]dsa$ds ]]
0486 [[ a/sa[s = dsad?*[0-9]ds/a$ds ]]
0487 [[ a =~ [12]a([!d]a?s[x[:alnum:]]|d?)p ]]
0488 
0489 [[ #comm1
0490  #comm2
0491  p[1] == p[2]
0492  #comm3
0493  #comm4
0494 ]]
0495 
0496 [[ #comm1
0497  #comm2
0498  -f p[2]
0499  #comm3
0500  #comm4
0501 ]]
0502 
0503 ((3+1+a+$c*(x) & 0x43422fd+03-085/23#D9a@_^8))
0504 ((1/(2-(a-4))))
0505 
0506 # they are not arithmetic evaluations...
0507 ((cmd && cmd) || cmd)
0508 $((cmd && cmd) || cmd)
0509 ((cmd &&
0510 cmd) || cmd)
0511 $((cmd &&
0512 cmd) || cmd)
0513 
0514 { echo
0515     echo
0516 }
0517 { echo ; }
0518 (echo ; echo)
0519 (echo
0520     echo)
0521 (echo a)
0522 ({ echo plop;})
0523 [ a -eq 2 ] || [ a -eq 2] ] && [[ a -eq 2 ]] || [[ a != b ]];
0524 [ a -eq 2 ]||[ a -eq 2] ]&&[[ a -eq 2 ]]||[[ a != b ]];
0525 test a -eq b
0526 
0527 # functions
0528 a() { echo x; }
0529 a  () { echo x; }
0530 function f { echo x; }
0531 kde.org() { echo x; }
0532 --func() { echo x; }
0533 
0534 # variables
0535 a=(a b c)
0536 a='a'
0537 a+=b
0538 a[1]='a'
0539 a[$i]='x'
0540 a[$((
0541     2+4
0542 ))]='x'
0543 a=([a]=2 `echo` -s > 'ds')
0544 a=(#comment
0545 value#nocomment #comment)
0546 )
0547 a=a cat
0548 a=`ls` cat
0549 
0550 # errors
0551 a a(s) a
0552 
0553 # control structure
0554 for name in a b c {d,e} ; do echo ; done
0555 for name; do echo ; done
0556 for name do echo ; done
0557 for ((i=0;i<5;++i)) ; do echo $i ; done
0558 select name in a ; do echo ; done
0559 select name; do echo ; done
0560 if : ; then echo ; elif [[ : ]] ; then echo ; else echo ; fi
0561 while : || : ; do echo ; done
0562 until : ; : ; do echo ; done
0563 case a in a) esac
0564 case a in a) echo ; esac
0565 case pwd in (patt1) echo ; echo ;; (patt*) echo ;;& patt?|patt) echo ;&
0566 patt) echo ;; esac
0567 
0568 for name in a
0569  b c ;
0570 do
0571 echo
0572 done
0573 
0574 case a in
0575   a\( | b*c? ) echo
0576   (b$c) # no pattern
0577   ;;
0578   (b$c) ;;
0579   # no pattern
0580   (b$c)
0581 esac
0582 
0583 case "$1" in
0584  "a") run_a|&a;;
0585  "b") run_b;;
0586  "c") run_c;;
0587  *) echo "Plase choose between 'a', 'b' or 'c'" && exit 1;;
0588 esac