Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-01-2018, 09:34 AM   #1
Registered: Jul 2011
Location: Melbourne, AU
Distribution: Centos 5
Posts: 45

Rep: Reputation: Disabled
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
if [[ $operator !== {aAmMsSdD} ]]
but that did not seem to work.
thank you again for your thoughts!



# 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

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 
# show answers

echo $num1 $operator $num2 :
echo "The answer is: $total"
Old 04-01-2018, 09:56 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,534

Rep: Reputation: 3131Reputation: 3131Reputation: 3131Reputation: 3131Reputation: 3131Reputation: 3131Reputation: 3131Reputation: 3131Reputation: 3131Reputation: 3131Reputation: 3131
first of all I would suggest you to use shellcheck to check the syntax of your script (see if not installed).
And there is a site to check, for example:

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:
case $op in
   a) ...
   b) ...
Old 04-01-2018, 04:28 PM   #3
LQ Guru
Registered: Sep 2013
Distribution: Slackware 14.2 / Slackware 14.2 current / Void Linux / Peppermint Eight
Posts: 5,945

Rep: Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109

that should keep you busy for a bit.

Last edited by BW-userx; 04-01-2018 at 04:29 PM.
1 members found this post helpful.
Old 04-01-2018, 07:45 PM   #4
Senior Member
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,927

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

Old 04-03-2018, 11:59 AM   #5
Registered: Dec 2011
Location: Simplicity
Posts: 695

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Functions do have local parameters.
adding() {
total = $(( $1 + $2 )) 

case $operator in
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).
echo "Would you like to [a]dd, [s]ubtract, [m]ultiply, or [d]ivide?"
  echo "Enter a, s, m, or d:"
  read operator
  [[ $operator != a|s|m|d ]]
  echo "operator must be a, s, m, or d" 

Last edited by MadeInGermany; 04-03-2018 at 12:06 PM.


bash, beginner, regex, scripting, variables

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Scripting : Quoting Variables Catch 22!! Bill Gates 666 Linux - General 3 04-05-2012 11:22 AM
bash scripting and integer variables. stf92 Linux - Software 1 06-06-2010 06:30 PM
shell scripting/bash/variables mayaabboud Linux - Newbie 13 01-06-2008 12:16 PM
shell scripting/bash/variables mayaabboud Linux - General 4 01-06-2008 08:17 AM
server variables in bash scripting basher400 Linux - Newbie 4 04-11-2005 06:04 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:25 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration