To help you get started, here's an annotated, syntactically correct version of your code that should work in all POSIX-compatible shells (e.g., bash
):
#!/bin/sh
# Note that '/' (not '\') must be used as the path separator.
cd location/address
# Capture command output using *command substitution*, `$(...)`
# Note: NO spaces around `=` are allowed.
var1=$(grep -w -c exception server.log)
var2=$(grep -w -c exception server.log.1)
# Use *arithmetic expansion*, `$((...))`, to perform integer calculations.
# Note: Inside `$((...))`, the prefix `$` is optional when referencing variables.
var3=$(( var1 + var2 ))
# Generally, it's better to *double*-quote variable references to
# make sure they're output unmodified - unquoted, they are subject to
# interpretation by the shell (so-called *shell expansions*).
echo "$var1"
echo "$var2"
# Output the result of the calculation. Note the use of just one, double-quoted
# string with an embedded variable reference.
# (By contrast, a *single*-quoted string would be used verbatim - no expansions.)
echo "sum of exceptions: $var3"
General tips:
- http://shellcheck.net is an excellent tool for syntax-checking shell code.
- http://explainshell.com is a great site that explains a given command line based on
man
pages. - Resources for learning
bash
, probably the most widely used POSIX-compatible shell:- Introduction: http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html
- Guide: http://mywiki.wooledge.org/BashGuide
- Cheat sheet: http://mywiki.wooledge.org/BashSheet