LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 04-01-2018, 09:34 AM   #1
scottmusician
Member
 
Registered: Jul 2011
Location: Melbourne, AU
Distribution: CentOS
Posts: 58

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
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"
 
Old 04-01-2018, 09:56 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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
 
Old 04-01-2018, 04:28 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

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
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

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

jlinkels
 
Old 04-03-2018, 11:59 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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

Last edited by MadeInGermany; 04-03-2018 at 12:06 PM.
 
Old 05-01-2018, 05:28 AM   #6
secomax
LQ Newbie
 
Registered: Feb 2017
Posts: 15

Rep: Reputation: 0
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,
 
Old 05-01-2018, 06:27 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
secomax, are you affiliated with that website? Because reviving an old thread with what amounts to just a link looks pretty close to spam.
 
Old 05-01-2018, 09:00 AM   #8
secomax
LQ Newbie
 
Registered: Feb 2017
Posts: 15

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


Reply

Tags
bash, beginner, regex, scripting, variables



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:33 PM.

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