You are not logged in.
Hi,
I'm trying to write a simple calculator script in BASH (as a method of learning) and everything went fine until now. I'm able to go through arguments passed to my script, I can copy all the numbers, and check if they're really numbers (regexp). However I've got the problem with operators. When I try to copy operator sign + or - or * or / into variable
line 82: let: ELEMS[1]=+: syntax error: argument expected (error token is "+")
Here's the code which causes the error:
let ELEMS[$J]=${ARGS[$I]}
I and J are counters used inside my loop. The above instruction works fine for numbers, etc., but not for operators. What am I doing wrong?
I'm sorry if it's a stupid question but I can't find any answer neither on forum nor google.
Peace,
xartii
Last edited by xartii (2012-10-25 14:36:40)
Offline
If you just leave out the 'let' it should work. Let "carries out arithmetic operations on variables" and "cannot be used for setting string variables". Those quotes are directly from http://tldp.org/LDP/abs/html/internal.html. There's a lot of info there, so search the page for "The let command" to jump right to the appropriate section.
Also, you could have figured this out on your own if you had tried. Googling 'bash let' returned the page I linked as the first result.
Last edited by alphaniner (2012-10-25 14:19:27)
But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner
Offline
"let" evaluates arithmetic expressions, so things like "let x=+" don't make sense.
If you just want to have "+" in a variable, do so without using let. e.g. "x=+". You can then use that in a let statement:
$ x=+
$ echo $x
+
$ let answer="1${x}2"
$ echo $answer
3
Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD
Making lemonade from lemons since 2015.
Online
Thanks,
Sorry, I feel so stupid now. I must have missed this part somehow.
Thanks again,
xartii
Offline