-4

Im having trouble formulating the following: Im trying to use

echo "$var1 - $var2"|bc -l

inside an if statement like

if [$(`echo "$var1 - $var2"|bc -l`)=0]
then 
echo "bloop"
fi

however the statement comes back with the numerical result plus and error I tried echoing the result before entering the loop and I get

./script.sh: [2.00000: not found

Any Ideas? Thanks in advance.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
Gio
  • 1
  • 1
  • 1
    http://shellcheck.net/ would have told you some of what's wrong on this one before you came here. – Charles Duffy Jun 16 '15 at 20:22
  • Spaces matter to the shell. You wouldn't write `echo"$var1-$var2"|bc-l` and expect it to work would you? You need spaces around the `[`, the `=`, and the `]` in that `if` statement. `[` is a command and it requires certain arguments (the last of which must be `]`. – Etan Reisner Jun 16 '15 at 20:22
  • You need a space after the `[`. In bash (and most shells) `[` is a callable program and/or built-in command. Without the space, bash doesn't recognize it. – Boldewyn Jun 16 '15 at 20:23
  • You can also obviate the pipe through `bc` by getting bash to do the arithmetic itself, by wrapping it in $[ ], a la `$[ $var1 - $var2 ]` – Jon Carter Jun 16 '15 at 20:29
  • @JonCarter, that's the antiquated Bourne syntax. Modern POSIX sh (and thus bash) math syntax is `$(( var1 - var2 ))`. – Charles Duffy Jun 16 '15 at 20:30
  • Question: Do you really need to be able to handle floating-point values here, or is integer math enough? – Charles Duffy Jun 16 '15 at 20:32
  • @CharlesDuffy, var1 and var2 are floating-point values in my problem, I just need to check if the sum is zero. The BC command is working now but I think its rounding the numbers, -1.42 - (-1.42) is not equal to 2. – Gio Jun 16 '15 at 20:38
  • @Gio, if they're both printed with the same amount of precision, why not just use string comparison? `[ "$var1" = "$var2" ]`; no floating-point awareness required. – Charles Duffy Jun 16 '15 at 20:42
  • BTW, if your real question is how to correctly do floating-point comparison in bash, then that makes this a dupe of https://stackoverflow.com/questions/8654051/how-to-compare-two-floating-point-numbers-in-a-bash-script – Charles Duffy Jun 16 '15 at 20:44
  • @CharlesDuffy Thanks, I think that would work better and Ill check the floating-point comparison, I might need it later. – Gio Jun 16 '15 at 20:48

2 Answers2

0

Your problem is here:

if [$(`echo "$var1 - $var2"|bc -l`)=0]

you need to make sure you have a space betweeen the [ and the ] like this:

if [ $(($var1 - $var2)) -eq 0 ]

[ is just an alias for the command test, so not using a whitespace after it will make the shell not interpret it as a [ command.

Also the calculation you are doing is wrong (or better how you do it). A simple approach would be using

if [ $(($var1 - $var2)) -eq 0 ]

Note here that you do not write =, instead you use the -eq parameter for equals. The $(()) does the calculation for you.

Have a look at the manpage for the test command: http://unixhelp.ed.ac.uk/CGI/man-cgi?test

Nidhoegger
  • 4,973
  • 4
  • 36
  • 81
  • You're only solving half the problem. Running the output of `bc` as a command isn't right either. – Charles Duffy Jun 16 '15 at 20:25
  • you are right. just thought about the obvious. Ill edit the post accordingly! – Nidhoegger Jun 16 '15 at 20:25
  • `[` is *almost* just an alias for `test`. They differ in that `[` requires a `]` as the last argument, and `test` doesn't allow it. – Keith Thompson Jun 16 '15 at 20:25
  • You could also write `if (( var1 - var2 == 0 ))`, if targeting only bash (and only integer math). – Charles Duffy Jun 16 '15 at 20:31
  • @KeithThompson: You are right. I thought that `[` was just a symlink to `test`. Thanks – Nidhoegger Jun 16 '15 at 20:31
  • @Nidhoegger, ...well, they usually _are_ backed by the same implementation (though as a shell built-in, there's no calling through to the filesystem or launching a subprocess involved), but that implementation checks whether its name is `[` and requires a closing `]` if so. – Charles Duffy Jun 16 '15 at 20:34
  • @CharlesDuffy: On my system (Linux Mint 17.1, GNU Coreutils 8.21), `/usr/bin/test` and `/usr/bin/[` are two distinct executables, with `[` slightly larger for some reason. But yes, they certainly could be links or symlinks to the same file, with the program itself looking at `argv[0]` to decide how to act. – Keith Thompson Jun 16 '15 at 20:39
  • @KeithThompson, in both dash and bash the builtins are provided by the same function. – Charles Duffy Jun 16 '15 at 20:41
0

Try:

if [ `echo "$var1 - $var2"|bc -l` -eq 0 ]
then 
echo "bloop"
fi

As @Nidhoegger explained in his answer about the error of [ and ] having space in after and before starting and closing brackets.

Also some error in your was:

$(`echo "$var1 - $var2"|bc -l`)

You were taking the output as variable name and trying to use that value $(...).

Also by doing:

$(`echo "$var1 - $var2"|bc -l`)=0

you are assigning the value instead of comparing it.

Rakholiya Jenish
  • 3,165
  • 18
  • 28