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} ]] thank you again for your thoughts! Cheers, Scott Code:
# functions: |
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 |
|
Look at my signature. Especially the bash -x The pointers to documentation were already provided.
jlinkels |
Functions do have local parameters.
Code:
adding() { 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?" |
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, |
secomax, are you affiliated with that website? Because reviving an old thread with what amounts to just a link looks pretty close to spam.
|
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. |