python -c “print calculation” gives syntax error












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?










share|improve this question




















  • 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?)

    – Ulrich Schwarz
    Feb 7 at 12:31











  • yes, at the begining I declared all the variables with either int or float values

    – Rakib Fiha
    Feb 7 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
    Feb 7 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
    Feb 7 at 12:47






  • 1





    for your division problem see stackoverflow.com/questions/1267869/…

    – Bodo
    Feb 7 at 13:08
















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?










share|improve this question




















  • 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?)

    – Ulrich Schwarz
    Feb 7 at 12:31











  • yes, at the begining I declared all the variables with either int or float values

    – Rakib Fiha
    Feb 7 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
    Feb 7 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
    Feb 7 at 12:47






  • 1





    for your division problem see stackoverflow.com/questions/1267869/…

    – Bodo
    Feb 7 at 13:08














0












0








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?










share|improve this question
















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?







shell-script python variable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 7 at 13:19









Jeff Schaller

41.7k1156133




41.7k1156133










asked Feb 7 at 12:29









Rakib FihaRakib Fiha

217




217








  • 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?)

    – Ulrich Schwarz
    Feb 7 at 12:31











  • yes, at the begining I declared all the variables with either int or float values

    – Rakib Fiha
    Feb 7 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
    Feb 7 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
    Feb 7 at 12:47






  • 1





    for your division problem see stackoverflow.com/questions/1267869/…

    – Bodo
    Feb 7 at 13:08














  • 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?)

    – Ulrich Schwarz
    Feb 7 at 12:31











  • yes, at the begining I declared all the variables with either int or float values

    – Rakib Fiha
    Feb 7 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
    Feb 7 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
    Feb 7 at 12:47






  • 1





    for your division problem see stackoverflow.com/questions/1267869/…

    – Bodo
    Feb 7 at 13:08








1




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?)

– Ulrich Schwarz
Feb 7 at 12:31





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?)

– Ulrich Schwarz
Feb 7 at 12:31













yes, at the begining I declared all the variables with either int or float values

– Rakib Fiha
Feb 7 at 12:32







yes, at the begining I declared all the variables with either int or float values

– Rakib Fiha
Feb 7 at 12:32






1




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
Feb 7 at 12:35







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
Feb 7 at 12:35






2




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
Feb 7 at 12:47





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
Feb 7 at 12:47




1




1





for your division problem see stackoverflow.com/questions/1267869/…

– Bodo
Feb 7 at 13:08





for your division problem see stackoverflow.com/questions/1267869/…

– Bodo
Feb 7 at 13:08










1 Answer
1






active

oldest

votes


















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.






share|improve this answer


























  • Tonight I am offline from that unix now, I will post it tomorrow.

    – Rakib Fiha
    Feb 7 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
    Feb 7 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
    Feb 7 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
    Feb 7 at 16:28











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499268%2fpython-c-print-calculation-gives-syntax-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer


























  • Tonight I am offline from that unix now, I will post it tomorrow.

    – Rakib Fiha
    Feb 7 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
    Feb 7 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
    Feb 7 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
    Feb 7 at 16:28
















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.






share|improve this answer


























  • Tonight I am offline from that unix now, I will post it tomorrow.

    – Rakib Fiha
    Feb 7 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
    Feb 7 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
    Feb 7 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
    Feb 7 at 16:28














2












2








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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 7 at 13:47

























answered Feb 7 at 13:40









rookrook

4771714




4771714













  • Tonight I am offline from that unix now, I will post it tomorrow.

    – Rakib Fiha
    Feb 7 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
    Feb 7 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
    Feb 7 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
    Feb 7 at 16:28



















  • Tonight I am offline from that unix now, I will post it tomorrow.

    – Rakib Fiha
    Feb 7 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
    Feb 7 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
    Feb 7 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
    Feb 7 at 16:28

















Tonight I am offline from that unix now, I will post it tomorrow.

– Rakib Fiha
Feb 7 at 13:49





Tonight I am offline from that unix now, I will post it tomorrow.

– Rakib Fiha
Feb 7 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
Feb 7 at 15:50







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
Feb 7 at 15:50






1




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
Feb 7 at 16:02





@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
Feb 7 at 16:02




1




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
Feb 7 at 16:28





@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
Feb 7 at 16:28


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499268%2fpython-c-print-calculation-gives-syntax-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀