I have a problem for which I need to write a program in shell script so I opened a book on linux (Linux System Administration Black Book” by Dee-Ann LeBlanc published by Coriolis ISBN 1-57610-419-2) that has a chapter on bash scripts. It seemed like a good idea to write a testscript to test the commands as I encountered them but the first script does not work and altering it in other obvious ways does not fix the problem.
The script as shown in the example is:-
#!/bin/bash
count=10
nextcount=$count + 1
echo $count
echo $nextcount
What the result of running this script should be
[Carl@localhost bin]$ testscript
10
11
However what actually results is
[Carl@localhost bin]$ testscript
/home/Carl/bin/testscript: line 3: +: command not found
10
with the error message indicating that the “+” sign is interpreted as a command not an addition operator.
If I remove the spaces surrounding the “+” sign so that the script becomes
#!/bin/bash
count=10
nextcount=$count+1
echo $count
echo $nextcount
[Carl@localhost bin]$ testscript
10
10+1
The line 3 statement instead of adding 1 to 10 to get 11 concatenates “10” “+” & “1”
Altering line3 to read nextcount=$count +1
or nextcount=$count+ 1
does not work either.
Finding insoluble problems with the first example in a text book on scripting should not happen. Am I going crazy?