You are not logged in.

#1 2012-10-25 13:50:20

xartii
Member
Registered: 2012-01-18
Posts: 19

[SOLVED] bash, assign a "+" to a variable

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

#2 2012-10-25 14:14:20

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,754

Re: [SOLVED] bash, assign a "+" to a variable

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

#3 2012-10-25 14:21:07

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 5,537

Re: [SOLVED] bash, assign a "+" to a variable

"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: ASUS P8Z77-V PRO // Processor: Intel Core i7-3770K 3.4GHz // GFX: nVidia GeForce GTX 970 Ti // RAM: 32GB (4x 8GB) Corsair DDR3 (@ 2133MHz) // Storage: 1x 3TB Seagate SATAII 5x 1TB Samsung SATAII, 2x 120GB Corsair SSD

Offline

#4 2012-10-25 14:36:26

xartii
Member
Registered: 2012-01-18
Posts: 19

Re: [SOLVED] bash, assign a "+" to a variable

Thanks,

Sorry, I feel so stupid now. I must have missed this part somehow.

Thanks again,
xartii

Offline

Board footer

Powered by FluxBB