0

I was doing some bash scripting and adding some python for float calculations like divide, deductions

When I am executing this, all part is running fine but for some python part it is showing me an error, however, in the latter part, it is showing me correct calculations.

File "<string>", line 1
    print 0.05-
              ^
SyntaxError: invalid syntax
  File "<string>", line 1
    print /2
          ^
SyntaxError: invalid syntax

My script is like the following:

tx_fee=0.0001;
panda_txfee=$(python -c "print $check_t_balance-$tx_fee");
panda=$(python -c "print $panda_txfee/2");

What I am doing wrong?

8
  • 1
    Do those variables have actual values? An unassigned variable expands to the empty string, which this looks like. (I assume you have $check_t_balance set somewhere?) Commented Feb 7, 2019 at 12:31
  • yes, at the begining I declared all the variables with either int or float values
    – Rakib Fiha
    Commented Feb 7, 2019 at 12:32
  • 1
    If I set check_t_balance=1 and run your code, I get $panda as 0.49995. I think you're just not setting the check_t_balance variable. Also, Python is a bit heavy weight just for doing floating point arithmetics in the shell. Either use something less heavy, such as bc, or do it all in one single Python program.
    – Kusalananda
    Commented Feb 7, 2019 at 12:35
  • 2
    To answer questions about possible problems with your variables we would have to see how you set the variables, what you do with functions etc. You should try to create a minimal script that reproduces the problem.
    – Bodo
    Commented Feb 7, 2019 at 12:47
  • 1
    for your division problem see stackoverflow.com/questions/1267869/…
    – Bodo
    Commented Feb 7, 2019 at 13:08

1 Answer 1

2

That works:

> tx_fee=0.0001;
> panda_txfee=$(python -c "print $check_t_balance-$tx_fee");
> panda=$(python -c "print $panda_txfee/2");
>
> echo $panda
-5e-05

Your errors show that variable panda_txfree is empty:

  File "<string>", line 1
    print /2
          ^
SyntaxError: invalid syntax

and it is empty because of tx_fee is empty:

File "<string>", line 1
    print 0.05-
              ^
SyntaxError: invalid syntax

provide more details of your environment.

Also note, I'm using Python 2.7 (and Bash), for Python 3 you will need embrace print arguments in round parenthesis, like this: print($panda_txfee/2), because in Python 3 print is a function, not special keyword for printing out.

4
  • Tonight I am offline from that unix now, I will post it tomorrow.
    – Rakib Fiha
    Commented Feb 7, 2019 at 13:49
  • I am using Python 2.7 as well. I think, I found the mistake, I did a silly spelling mistake, Everywhere I used the variable tx_fee but only in one place I wrote tx_fees, I will test it tomorrow. I hope this was the main and only reason of this error. I was thinking, I have many floats so is it better to wrap it within float function like this? or its just useless? amt=$(python -c "print(float($amt_aft_txfee/2))");
    – Rakib Fiha
    Commented Feb 7, 2019 at 15:50
  • 1
    @RakibFiha, you can omit float() if value in your amt_aft_txfee variable goes with point, e.g. 2.5 or 2.0 also note, that in Python usually used Decimal (from module decimal) wrapper for precise calculations
    – rook
    Commented Feb 7, 2019 at 16:02
  • 1
    @RakibFiha, In python 2, you might want float(x)/2, since x/2 would round/truncate the result if x is an integer. float(x/2) doesn't help in that, the conversion happens too late there
    – ilkkachu
    Commented Feb 7, 2019 at 16:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .