Debian Codegraph @

resistor-inventory.sh

raw symbols SCIP
1 #!/bin/bash
2 # resistor-inventory.sh
3 # Simple database / table-lookup application.
4
5 # ============================================================== #
6 # Data
7
8 B1723_value=470 # Ohms
9 B1723_powerdissip=.25 # Watts
10 B1723_colorcode="yellow-violet-brown" # Color bands
11 B1723_loc=173 # Where they are
12 B1723_inventory=78 # How many
13
14 B1724_value=1000
15 B1724_powerdissip=.25
16 B1724_colorcode="brown-black-red"
17 B1724_loc=24N
18 B1724_inventory=243
19
20 B1725_value=10000
21 B1725_powerdissip=.125
22 B1725_colorcode="brown-black-orange"
23 B1725_loc=24N
24 B1725_inventory=89
25
26 # ============================================================== #
27
28
29 echo
30
31 PS3='Enter catalog number: '
32
33 echo
34
35 select catalog_number in "B1723" "B1724" "B1725"
36 do
37 Inv=${catalog_number}_inventory
38 Val=${catalog_number}_value
39 Pdissip=${catalog_number}_powerdissip
40 Loc=${catalog_number}_loc
41 Ccode=${catalog_number}_colorcode
42
43 echo
44 echo "Catalog number $catalog_number:"
45 # Now, retrieve value, using indirect referencing.
46 echo "There are ${!Inv} of [${!Val} ohm / ${!Pdissip} watt]\
47 resistors in stock." # ^ ^
48 # As of Bash 4.2, you can replace "ohm" with \u2126 (using echo -e).
49 echo "These are located in bin # ${!Loc}."
50 echo "Their color code is \"${!Ccode}\"."
51
52 break
53 done
54
55 echo; echo
56
57 # Exercises:
58 # ---------
59 # 1) Rewrite this script to read its data from an external file.
60 # 2) Rewrite this script to use arrays,
61 #+ rather than indirect variable referencing.
62 # Which method is more straightforward and intuitive?
63 # Which method is easier to code?
64
65
66 # Notes:
67 # -----
68 # Shell scripts are inappropriate for anything except the most simple
69 #+ database applications, and even then it involves workarounds and kludges.
70 # Much better is to use a language with native support for data structures,
71 #+ such as C++ or Java (or even Perl).
72
73 exit 0