4 There are several ways calc may be used in shell scripts. The
5 syntax for these varies widely for different shells and systems,
6 but common to most are commands like echo, if, for, goto, shift,
7 and exit, as well as the accessing of environment parameters, shell
8 variables, and command-line arguments.
10 As a simple example, assuming a C or Bourne shell, let add be a
11 file containing just one line:
19 should respond with the display of:
23 The "-q" was included in the command to avoid reading of any
24 start-up calc files which could contain commands not wanted
25 here. The "--" indicates that there are no more options;
26 without it, if $1 began with '-', calc would interpret it as
27 the first character of another option. To execute the file,
28 the strings "1.23" and "4.56" were assigned to $1 and $2, so
29 calc was in effect asked to evaluate the string "1.23 + 4.56".
31 By making add executable by a command like:
35 the command used here may be simplified to:
39 Here we shall assume that any script we refer to has been made
40 executable in this way.
42 Because $1 and $2, and instructions in the script, are to read
43 by calc as expressions or commands, they may be much more
44 complicated than in the above example, but if they involve
45 characters with special interpretations by the shell (spaces
46 for word separation, * or ? or [ ...] for file-name expansion,
47 ! (without immediately following space) for history expansion,
48 ( ... ) for shell-function arguments, { ... } for brace
49 expansion, $ for parameter or variable expansion, <, <<, >, >>
50 for redirection of input or output, etc.) it will usually be
51 necessary to quote or escape tho characters, or usually more
52 conveniently, quote whole expressions with single or double
55 For example, the add script should have no problem with
58 ./add "sqrt(2)" "3 * 4"
60 ./add "mat A[2,2] = {1,2,3,4}" "A^2"
62 ./add "2 + 3i" "(3 + 4i)^2"
64 If the shell arguments are to be integers, one could use
65 scripts like the following with arithmetic expansion
78 Specifying the shell for a script may be done by including
79 in the script a first line with the "magic number" "#!" and
80 the full file path for the shell as in:
87 For a script to multiply rather than add two expressions, one
88 could have a file mul with the one line:
94 which will work so long as $1 and $2 are literal numbers, but
101 both of which calc interprets as evaluating 2 + 3 * 4. What should
102 work for most shells is:
104 calc -q -- "($1) * ($2)"
106 For adding an arbitrary number of expressions that evaluate to
107 rational numbers expressible with at most 20 decimal places,
108 simple shell script could be used:
112 s=`calc -q -- $s + $i`
116 This is not particularly efficient since it calls calc once for
117 each argument. Also, a more serious script would permit more
120 Another way of handling a sum of several expressions is with
121 the script addall2 with a here document:
126 for (i = 0; i < n; i++)
131 In executing the command:
135 the $* in ths script expands to 2 3 4, and because of the "-s"
136 in the options, calc starts with argv(0) = "2", argv(1) = "3",
137 argv(2)= "4". As there is only one calc process involved and
138 the eval() function accepts as argument any string that
139 represents the body of a calc function, the strings argv(0),
140 argv(1), ... could evaluate to any value types for which the
141 additions to be performed are defined, and variables defined in
142 one argv() can be used in later arguments.
144 For systems that support interpreter files, essentially the
145 same thing may be done more efficiently by using calc as an
146 interpreter. Assuming the full path for calc is
147 /usr/local/bin/calc, one could use the file addall3 with contents
149 #!/usr/bin/calc -q -s -f
152 for (i = 1; i <= n; i++)
158 The -f flag must be at the very end of the #! line.
159 The #! line must be the first line of the exeuctable file.
160 The path after the #! must be the full path to the calc executable.
166 the arguments calc receives are argv(0) = "addall3", argv(1) =
167 "2", argv(3) = "3", argv(4) = "4".
169 Another kind of script that can be useful is sqrts1:
171 calc -q 'global s; while (scanf("%s", s) == 1) print sqrt(eval(s));'
173 or what is essentially an interpreter equivalent sqrts2:
175 #!/usr/local/bin/calc -q -f
177 while (scanf('%s', s) == 1)
180 If sqrts is either of these scripts, the command:
184 or, if datafile contains the one line:
195 cat datafile | ./sqrts
199 should display the square-roots of 27 and 2+3i. The output could
200 be piped to another command by | or directed to a file by use of
203 With no specified input, either sqrts1 or sqrts2 will wait
204 without any prompt for input from the keyboard and as each line
205 is completed display the square-roots of the expressions
206 entered. Exit can be achieved by entering exit or entering
207 ctrl-D (interpreted as EOF) rather than a line of input.
209 One advantage of an interpreter file like sqrts2 (which has only
210 options, but neither "-s" nor "--" in its first line) is that it
211 can be invoked with further options as in
213 echo 2 3 4 | ./sqrts2 -i -D 32
215 An advantage of non-interpreter files is that they can use shell
216 features. For example, for unquoted arguments or arguments in
217 double quotes parameter expansion (indicated by unquoted '$') and
218 command substitution (using backquotes) occur before lines are
219 compiled by calc. For example, if doit is an executable
224 it may be used as in:
230 to display the values of sqrt(7) and exp(7). The "--" prevents a
231 leading '-' in the $1 argument as indicating one or more additional
232 options. E.g., without the "--" in doit,
236 would be interpreted as:
240 in which the dash in the quoted part would be taken as indicating a
241 list of options -s, -q, -r, etc.; this would give an "illegal option"
242 error as calc has no -r option.
244 In invoking the doit script it is not necessary that $1 expand to a
245 calc function name and $2 to an expression; all that is required is
250 expands to a string that calc will recognize as a command. E.g.:
252 ./doit "define f(x) = x^2; 2 + mod" "f(7), 6"
256 calc -q -- "define f(x) = x^2; 2 + mod(f(7), 6)"
258 Essentially the same is achieved by the contents of doit is changed to:
264 The "-p" stops calc going interactive; without it the effect would be
265 be the same as that of a script with the one line:
267 calc -q -i -- "$1($2)"
269 For more information use the following calc commands:
276 ## Copyright (C) 2000 Landon Curt Noll and Ernest Bowen
278 ## Calc is open software; you can redistribute it and/or modify it under
279 ## the terms of the version 2.1 of the GNU Lesser General Public License
280 ## as published by the Free Software Foundation.
282 ## Calc is distributed in the hope that it will be useful, but WITHOUT
283 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
284 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
285 ## Public License for more details.
287 ## A copy of version 2.1 of the GNU Lesser General Public License is
288 ## distributed with calc under the filename COPYING-LGPL. You should have
289 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
290 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
292 ## @(#) $Revision: 30.1 $
293 ## @(#) $Id: script,v 30.1 2007/03/16 11:10:42 chongo Exp $
294 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/script,v $
296 ## Under source code control: 1999/11/30 05:29:48
297 ## File existed as early as: 1999
299 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
300 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/