LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash - new to scripting and variables (https://www.linuxquestions.org/questions/programming-9/bash-new-to-scripting-and-variables-4175626779/)

scottmusician 04-01-2018 09:34 AM

bash - new to scripting and variables
 
Hi everyone,

I'm just starting to learn bash script programming, and I'm having trouble making one of my first shell scripts to function correctly. There's probably just some syntax errors - so I'm hoping that you can help.

It's a really simple script (actually it's one of the workshop quizes, as a part of the Linux Foundation's LS101 course) - all it does is ask for a few variables, and then calculate some simple arithmetic based on them. Whilst I could simply look at the lab answer provided in my course, I'd much prefer to learn about *why* my code isn't functioning 100%!

The script is below. Whilst it executes, there's a few things that it doesn't do, which I want it to:

(1) so I'm not getting an answer (ie. $total isn't being calculated). Is there something wrong about passing the variables to the functions (or maybe the if/elif/fi loop) ?

(2) in order to loop the error checking, maybe I could use a while $operator !== -style statement?

(3) is there a way that I can use a regular expression to simplify the error checking? I've tried using
Code:

if [[ $operator !== {aAmMsSdD} ]]
but that did not seem to work.
thank you again for your thoughts!

Cheers,

Scott

Code:

# functions:

adding() {
total = $(( $num1 + $num2 ))
}

subtraction() {
total = $(( $num1 - $num2 ))
}

multiply() { 
total = $(( $num1 * $num2 ))
}

divide() {
total = $(( $num1 / $num2 ))
}

echo "would you like to [a]dd, [s]ubtract, [m]ultiply, or [d]ivide?"
echo "Enter a, s, m, or d:"
read operator

# check for input validation (ie. give an error if %operator != a,m,s, or d)

if [[ $operator != a ]] &&  [[ $operator != s ]] && [[ $operator != d ]] && [[ $operator != m ]] ; then  echo "operator must be a, s, m, or d" ; echo "enter a, s, m, d:" ; read operator
fi

echo "enter number 1"
read num1

echo "enter number 2"
read num2

# calculations

if [[ $operator == a ]] ; then adding $num1 $num2
elif [[ $operator == s ]] ; then subtraction $num1 $num2
elif [[ $operator == m ]] ; then multiply $num1 $num2
elif [[ $operator == d ]] ; then divide $num1 $num2
fi
# show answers

echo $num1 $operator $num2 :
echo "The answer is: $total"


pan64 04-01-2018 09:56 AM

first of all I would suggest you to use shellcheck to check the syntax of your script (see www.shellcheck.net if not installed).
And there is a site www.tldp.org to check, for example: http://tldp.org/LDP/Bash-Beginners-Guide/html/

Regarding your questions:
1: yes, you pass variables not by name, but by position, see positional parameters ($1, $2 ...)
2: !== is invalid, you need to check (for example man bash) about the valid comparators and tests.
3: yes, you may try regexp, but I would rather suggest you to use case:
Code:

case $op in
  a) ...
  b) ...
...
esac


BW-userx 04-01-2018 04:28 PM

http://tldp.org/LDP/abs/html/comparison-ops.html
http://tldp.org/LDP/abs/html/ops.html
http://tldp.org/LDP/abs/html/arithexp.html
http://tldp.org/LDP/abs/html/functions.html

that should keep you busy for a bit.

jlinkels 04-01-2018 07:45 PM

Look at my signature. Especially the bash -x The pointers to documentation were already provided.

jlinkels

MadeInGermany 04-03-2018 11:59 AM

Functions do have local parameters.
Code:

adding() {
total = $(( $1 + $2 ))
}

case $operator in
(a)
  ...
;;
(s)
  ...
;;
esac

I suggest ( ) not the old imbalanced ) notation.
And indention only for the
...
shell code.
Use a while loop, testing the input with the == or != operator within [[ ]] (where the 2nd operand is a glob, just like the operand in ( ) in a case-esac).
Code:

echo "Would you like to [a]dd, [s]ubtract, [m]ultiply, or [d]ivide?"
while
  echo "Enter a, s, m, or d:"
  read operator
  [[ $operator != a|s|m|d ]]
do
  echo "operator must be a, s, m, or d"
done


secomax 05-01-2018 05:28 AM

I would like to add another useful resource that may help https://likegeeks.com/bash-functions/
It discusses how to pass arrays to functions and some other advanced topics about Bash functions.

Regards,

ntubski 05-01-2018 06:27 AM

secomax, are you affiliated with that website? Because reviving an old thread with what amounts to just a link looks pretty close to spam.

secomax 05-01-2018 09:00 AM

I was searching for Bash functions and I thought that would be an addition to anyone searches the forum for it like me.


All times are GMT -5. The time now is 07:09 AM.