0

I'm using a new language, I think it's linux but to be honest I have no clue; we're connecting to some server through a program called putty to do this programming. As part of an assignment I have to create a calculator program that takes two numbers and an operator as arugments but I'm getting a bunch of unexpected token errors. I'm very lost and neither my text no my professor is helpful so far

#!/bin/bash

function add {
        echo $(( $1 + $2 ))
}

function subtract {
        echo $(( $1 - $2 ))
}

function multiply {
        echo $(( $1 x $2 ))
}

function divide {
        echo $(( $1 / $2 ))
}

if [ $3 = '+' ] then add

so far I'm just trying to get it so I can get 2 from the command ./calc.sh 1 1 + but I keep getting unexpected token error on line 20 syntax error near unexpected token 'elif' [ $3 = '-' ] then subtract

jww
  • 97,681
  • 90
  • 411
  • 885
  • 3
    You didn't include the code that the error is referencing. – Carcigenicate Mar 25 '19 at 16:33
  • 1
    The language you are mentioning is not "linux", it's "bash". – Jona Mar 25 '19 at 16:36
  • 1
    Please show the relevant code. Please state where the error is encountered. Also see [if, elif, else statement issues in Bash](https://stackoverflow.com/q/16034749/608639) and [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – jww Mar 25 '19 at 16:41
  • 1
    Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Mar 25 '19 at 16:41
  • 1
    Your error messages gives `elif` but there is no `elif` in the code you show. – cdarke Mar 25 '19 at 16:45

2 Answers2

1

This will do what you want:

#!/bin/bash

function add {
        echo $(( $1 + $2 ))
}

if [ $3 = '+' ]; then add $1 $2
fi
Jona
  • 1,218
  • 1
  • 10
  • 20
  • @emma-brain If any of the answers solved your question, it's also a good practice to upvote. ;-) – Jona Mar 26 '19 at 09:18
0

There are a number of issues in just what we can see, in the last visible line.

You need either a line break or a semi colon after if [ $3 = '+' ] And while it is possible that it is just where it chose to stop pasting, your functions need to be passed arguments, so "add $1 $2" vs "add"

Corvar
  • 1