Calculate from the command line with Spigot

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Variables within Variables

A very useful feature is including the symbols for operations (+, -, *, /, etc.) within a variable. As you may get negative numbers in random operations, remember to enclose variables inside brackets (Listing 3). Figure 6 shows the output enclosed within commas.

Listing 3

calculator.sh

#!/bin/bash
# calculator.sh
read -p "First number: " a
read -p "Operation: " op
read -p "Second number: " b
echo -n "$a $op $b returns"; spigot --rn -d50 \($a$op$b\) --printf %10.2f
Figure 6: You can save operation symbols within a variable.

Plotting Results

You can modify the circuit script to output results and plot a graph. For example, You can measure tuneable resonant circuits (frequency variation using a variable capacitor). In the example in Listing 4, both Pi (pi ) and the square root function (sqrt ) are used too.

Listing 4

vfo.sh

#!/bin/bash
# vfo.sh
# Delete any existing results table
rm resultstab.txt
clear
echo "Calculation of tuning range LC oscillating circuit"
echo "------------------------------------------------- "
read -p "                   coil:inductance in ?H: " l
read -p "   Rotary capacitor: highest value in pF: " cmax
read -p "     Rotary capacitor:lowest value in pF: " cmin
# cmin decrease by 1 in a for loop
cmin=$(echo $cmin | spigot 'base10fd:0-1')
for ((c=cmax; c>cmin; c--))
do
  qrg=$(spigot --rn -d50 "$l"e-6*"$c"e-12 | spigot --rn -d50 '(1/(2pi*sqrt(base10fd:0))/1000000)' --printf %10.3f)
  echo "$c $qrg" >> resultstab.txt
done
gnuplot -p -e "plot 'resultstab.txt' using 1:2"

The for loop specifies the maximum value for capacitance (cmax ) of the variable capacitor, which is reduced by one, until the minimum value (Cmin ) is reached. The results (values for capacity and frequency) are placed in a table. Using gnuplot [4], the script then creates a diagram, which is displayed on the screen. You will need to install the gnuplot and plotutils packages for this to work. Figure 7 shows the script's output.

Figure 7: Spigot not only calculates data, but also displays it as a graph if necessary.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content