|
|
The text file barSet1.txt Listing 17.8 contains a list of numbers that represent the height of the rectangles in the bar set displayed in Figure 17.3.
Figure 17.3: A bar set.
Listing 17.8 barSet1.txt
112 224 333 188 255 272 350 312 224 133 288 155 312 224 133
The shell script barSet1.sh in Listing 17.9 demonstrates how to read the contents of a text file in order to generate an SVG document with a bar set.
Consider the bar set displayed in Figure 17.3.
The Unix shell script barSet1.sh in Listing 17.9 generates an SVG document that renders the bar set in Figure 17.3.
Listing 17.9 barSet1.sh
# STEP 1: initialize variables fileName="barSet1.txt" index=0 count=0 height=0 color="" barWidth=20 xPosition=0 yPosition=0 baseLine=400 # STEP 2: does input file exist? if [ ! -f $fileName ] then echo "Cannot open $fileName" else # STEP 3: print header information echo "<svg>" echo " <g transform=\"translate(50,50)\">" # STEP 4: generate bar-related information for height in `cat $fileName` do index=`expr $count % 4` if [ "$index" -eq "0" ] then color="red" elif [ "$index" -eq "1" ] then color="green" elif [ "$index" -eq "2" ] then color="blue" elif [ "$index" -eq "3" ] then color="yellow" fi yPosition=`expr $baseLine - $height` subline1=" <rect x=\"$xPosition\" y=\"$yPosition\" " subline2=" width=\"$barWidth\" height=\"$height\"" subline3=" style=\"fill:$color\"/>" fullline="${subline1} ${subline2}" echo $fullline echo $subline3 xPosition=`expr $xPosition + $barWidth` count=`expr $count + 1` done # STEP 5: print trailer information echo " </g>" echo "</svg>" fi
Listing 17.9 is a straightforward Unix shell script. The Unix echo statement is analogous to the Perl print statement, and the if/then/fi construct is analogous to the Perl if statement. You need to make the shell script executable by typing something like this:
chmod +x barSet1.sh
or you can explicitly set the octal values like this:
chmod 755 barSet.sh
Next, add the current directory to the PATH variables like this:
PATH=.:$PATH; export PATH (Bourne shell) export PATH=.:$PATH
and then you can redirect the output of the Unix shell script to an SVG file as follows:
barSet1.sh >unixBarSet1.svg
The SVG document unixBarSet1.svg in Listing 17.10 is generated by the Unix shell script barSet1.sh in Listing 17.9
Listing 17.10 unixBarSet1.svg
<svg> <g transform="translate(50,50)"> <rect x="0" y="288" width="20" height="112" style="fill:red"/> <rect x="20" y="176" width="20" height="224" style="fill:green"/> <rect x="40" y="67" width="20" height="333" style="fill:blue"/> <rect x="60" y="212" width="20" height="188" style="fill:yellow"/> <rect x="80" y="145" width="20" height="255" style="fill:red"/> <rect x="100" y="128" width="20" height="272" style="fill:green"/> <rect x="120" y="50" width="20" height="350" style="fill:blue"/> <rect x="140" y="88" width="20" height="312" style="fill:yellow"/> <rect x="160" y="176" width="20" height="224" style="fill:red"/> <rect x="180" y="267" width="20" height="133" style="fill:green"/> <rect x="200" y="112" width="20" height="288" style="fill:blue"/> <rect x="220" y="245" width="20" height="155" style="fill:yellow"/> <rect x="240" y="88" width="20" height="312" style="fill:red"/> <rect x="260" y="176" width="20" height="224" style="fill:green"/> <rect x="280" y="267" width="20" height="133" style="fill:blue"/> </g> </svg>
|
|