Debian Codegraph @

ex67.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 declare -a colors
4 # All subsequent commands in this script will treat
5 #+ the variable "colors" as an array.
6
7 echoscip-shell system . . echo. "Enter your favorite colors (separated from each other by a space)."
8
9 read -a colors # Enter at least 3 colors to demonstrate features below.
10 # Special option to 'read' command,
11 #+ allowing assignment of elements in an array.
12
13 echoscip-shell system . . echo.
14
15 element_count=${#colors[@]}
16 # Special syntax to extract number of elements in array.
17 # element_count=${#colors[*]} works also.
18 #
19 # The "@" variable allows word splitting within quotes
20 #+ (extracts variables separated by whitespace).
21 #
22 # This corresponds to the behavior of "$@" and "$*"
23 #+ in positional parameters.
24
25 index=0
26
27 while [scip-shell system . . `[`. "$index" -lt "$element_count" ]
28 do # List all the elements in the array.
29 echoscip-shell system . . echo. ${colors[$index]}
30 # ${colors[index]} also works because it's within ${ ... } brackets.
31 let "index = $index + 1"
32 # Or:
33 # ((index++))
34 done
35 # Each array element listed on a separate line.
36 # If this is not desired, use echo -n "${colors[$index]} "
37 #
38 # Doing it with a "for" loop instead:
39 # for i in "${colors[@]}"
40 # do
41 # echo "$i"
42 # done
43 # (Thanks, S.C.)
44
45 echoscip-shell system . . echo.
46
47 # Again, list all the elements in the array, but using a more elegant method.
48 echoscip-shell system . . echo. ${colors[@]} # echo ${colors[*]} also works.
49
50 echoscip-shell system . . echo.
51
52 # The "unset" command deletes elements of an array, or entire array.
53 unset colors[1] # Remove 2nd element of array.
54 # Same effect as colors[1]=
55 echoscip-shell system . . echo. ${colors[@]} # List array again, missing 2nd element.
56
57 unset colors # Delete entire array.
58 # unset colors[*] and
59 #+ unset colors[@] also work.
60 echoscip-shell system . . echo.; echoscip-shell system . . echo. -n "Colors gone."
61 echoscip-shell system . . echo. ${colors[@]} # List array again, now empty.
62
63 exit 0