ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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"
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:
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
Last edited by MadeInGermany; 04-03-2018 at 12:06 PM.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.