How can I feed a string to awk for the purpose of mathematical calculation?












1















#!/bin/sh --

expression_to_evaluate='12 + 3'

printf '%sn' "Arithemtic Expansion: $(( $expression_to_evaluate ))"

printf '%s' 'bc: '
printf '%sn' "$expression_to_evaluate" | bc

printf '%s' 'awk: '
awk -v expression_to_evaluate="$expression_to_evaluate" -- 'BEGIN{printf "%dn", expression_to_evaluate}'


Output:



Arithemtic Expansion: 15
bc: 15
awk: 12


awk is returning 12 instead of 15. How can I feed a string to awk for the purpose of mathematical calculation?










share|improve this question




















  • 1





    Should it also be able to handle expressions like 1 + system("date +%s") or ++var or ++a[1] or ENVIRON["SHLVL"] + 3. In other words, how do you define mathematical calculation?

    – Stéphane Chazelas
    8 hours ago


















1















#!/bin/sh --

expression_to_evaluate='12 + 3'

printf '%sn' "Arithemtic Expansion: $(( $expression_to_evaluate ))"

printf '%s' 'bc: '
printf '%sn' "$expression_to_evaluate" | bc

printf '%s' 'awk: '
awk -v expression_to_evaluate="$expression_to_evaluate" -- 'BEGIN{printf "%dn", expression_to_evaluate}'


Output:



Arithemtic Expansion: 15
bc: 15
awk: 12


awk is returning 12 instead of 15. How can I feed a string to awk for the purpose of mathematical calculation?










share|improve this question




















  • 1





    Should it also be able to handle expressions like 1 + system("date +%s") or ++var or ++a[1] or ENVIRON["SHLVL"] + 3. In other words, how do you define mathematical calculation?

    – Stéphane Chazelas
    8 hours ago
















1












1








1








#!/bin/sh --

expression_to_evaluate='12 + 3'

printf '%sn' "Arithemtic Expansion: $(( $expression_to_evaluate ))"

printf '%s' 'bc: '
printf '%sn' "$expression_to_evaluate" | bc

printf '%s' 'awk: '
awk -v expression_to_evaluate="$expression_to_evaluate" -- 'BEGIN{printf "%dn", expression_to_evaluate}'


Output:



Arithemtic Expansion: 15
bc: 15
awk: 12


awk is returning 12 instead of 15. How can I feed a string to awk for the purpose of mathematical calculation?










share|improve this question
















#!/bin/sh --

expression_to_evaluate='12 + 3'

printf '%sn' "Arithemtic Expansion: $(( $expression_to_evaluate ))"

printf '%s' 'bc: '
printf '%sn' "$expression_to_evaluate" | bc

printf '%s' 'awk: '
awk -v expression_to_evaluate="$expression_to_evaluate" -- 'BEGIN{printf "%dn", expression_to_evaluate}'


Output:



Arithemtic Expansion: 15
bc: 15
awk: 12


awk is returning 12 instead of 15. How can I feed a string to awk for the purpose of mathematical calculation?







awk posix math






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago







Harold Fischer

















asked 20 hours ago









Harold FischerHarold Fischer

668415




668415








  • 1





    Should it also be able to handle expressions like 1 + system("date +%s") or ++var or ++a[1] or ENVIRON["SHLVL"] + 3. In other words, how do you define mathematical calculation?

    – Stéphane Chazelas
    8 hours ago
















  • 1





    Should it also be able to handle expressions like 1 + system("date +%s") or ++var or ++a[1] or ENVIRON["SHLVL"] + 3. In other words, how do you define mathematical calculation?

    – Stéphane Chazelas
    8 hours ago










1




1





Should it also be able to handle expressions like 1 + system("date +%s") or ++var or ++a[1] or ENVIRON["SHLVL"] + 3. In other words, how do you define mathematical calculation?

– Stéphane Chazelas
8 hours ago







Should it also be able to handle expressions like 1 + system("date +%s") or ++var or ++a[1] or ENVIRON["SHLVL"] + 3. In other words, how do you define mathematical calculation?

– Stéphane Chazelas
8 hours ago












4 Answers
4






active

oldest

votes


















2














While this works, I recommend against it because, unless you control the source of expression_to_evaluate, it is a security risk:



$ expression_to_evaluate='12 + 3'
$ awk "BEGIN{printf "%dn", $expression_to_evaluate}"
15





share|improve this answer



















  • 1





    Note that echo "$(($expression_to_evaluate))" is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even in dash or yash can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.

    – Stéphane Chazelas
    7 hours ago



















1














awk does not have a way to evaluate a general arithmetic expression given to it as a string. bc, on the other hand, takes an expression and evaluates it, as does the shell.



Would you want to evaluate the string as a mathematical expression in awk, you would either have to inject the expression as code into the awk program (potentially creating a code injection vulnerability), or simply write your own parser for the types of expressions you'd like to evaluate (this is a common computer science homework assignment).



You get 12 from your awk code because the value of expression_to_evaluate, when converted into an integer, can be converted up until the space character in the string. This is how the strtol() C library function works when it converts a string into an integer, and awk is likely using this under the hood.






share|improve this answer
























  • "You would either have to inject the expression as code". How is that different from bc? bc can't even take external input, all you can do is inject code to it.

    – Stéphane Chazelas
    8 hours ago











  • @StéphaneChazelas The difference is that it's the normal mode of operation for bc to read expressions from standard input. It is not the normal mode of operation of awk to insert shell code (user input) into the string that serves as the actual code.

    – Kusalananda
    6 hours ago





















0














echo $expression_to_evaluate | awk -F "+" '{print ($1+$2)}'





share|improve this answer
























  • They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

    – Jeff Schaller
    6 hours ago



















0














if You want to add 2 numbers using awk use below command



awk '{print 12 + 3;exit}'

output
15





share|improve this answer
























  • They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

    – Jeff Schaller
    6 hours ago











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%2f503691%2fhow-can-i-feed-a-string-to-awk-for-the-purpose-of-mathematical-calculation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














While this works, I recommend against it because, unless you control the source of expression_to_evaluate, it is a security risk:



$ expression_to_evaluate='12 + 3'
$ awk "BEGIN{printf "%dn", $expression_to_evaluate}"
15





share|improve this answer



















  • 1





    Note that echo "$(($expression_to_evaluate))" is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even in dash or yash can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.

    – Stéphane Chazelas
    7 hours ago
















2














While this works, I recommend against it because, unless you control the source of expression_to_evaluate, it is a security risk:



$ expression_to_evaluate='12 + 3'
$ awk "BEGIN{printf "%dn", $expression_to_evaluate}"
15





share|improve this answer



















  • 1





    Note that echo "$(($expression_to_evaluate))" is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even in dash or yash can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.

    – Stéphane Chazelas
    7 hours ago














2












2








2







While this works, I recommend against it because, unless you control the source of expression_to_evaluate, it is a security risk:



$ expression_to_evaluate='12 + 3'
$ awk "BEGIN{printf "%dn", $expression_to_evaluate}"
15





share|improve this answer













While this works, I recommend against it because, unless you control the source of expression_to_evaluate, it is a security risk:



$ expression_to_evaluate='12 + 3'
$ awk "BEGIN{printf "%dn", $expression_to_evaluate}"
15






share|improve this answer












share|improve this answer



share|improve this answer










answered 20 hours ago









John1024John1024

47.3k5110125




47.3k5110125








  • 1





    Note that echo "$(($expression_to_evaluate))" is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even in dash or yash can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.

    – Stéphane Chazelas
    7 hours ago














  • 1





    Note that echo "$(($expression_to_evaluate))" is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even in dash or yash can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.

    – Stéphane Chazelas
    7 hours ago








1




1





Note that echo "$(($expression_to_evaluate))" is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even in dash or yash can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.

– Stéphane Chazelas
7 hours ago





Note that echo "$(($expression_to_evaluate))" is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even in dash or yash can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.

– Stéphane Chazelas
7 hours ago













1














awk does not have a way to evaluate a general arithmetic expression given to it as a string. bc, on the other hand, takes an expression and evaluates it, as does the shell.



Would you want to evaluate the string as a mathematical expression in awk, you would either have to inject the expression as code into the awk program (potentially creating a code injection vulnerability), or simply write your own parser for the types of expressions you'd like to evaluate (this is a common computer science homework assignment).



You get 12 from your awk code because the value of expression_to_evaluate, when converted into an integer, can be converted up until the space character in the string. This is how the strtol() C library function works when it converts a string into an integer, and awk is likely using this under the hood.






share|improve this answer
























  • "You would either have to inject the expression as code". How is that different from bc? bc can't even take external input, all you can do is inject code to it.

    – Stéphane Chazelas
    8 hours ago











  • @StéphaneChazelas The difference is that it's the normal mode of operation for bc to read expressions from standard input. It is not the normal mode of operation of awk to insert shell code (user input) into the string that serves as the actual code.

    – Kusalananda
    6 hours ago


















1














awk does not have a way to evaluate a general arithmetic expression given to it as a string. bc, on the other hand, takes an expression and evaluates it, as does the shell.



Would you want to evaluate the string as a mathematical expression in awk, you would either have to inject the expression as code into the awk program (potentially creating a code injection vulnerability), or simply write your own parser for the types of expressions you'd like to evaluate (this is a common computer science homework assignment).



You get 12 from your awk code because the value of expression_to_evaluate, when converted into an integer, can be converted up until the space character in the string. This is how the strtol() C library function works when it converts a string into an integer, and awk is likely using this under the hood.






share|improve this answer
























  • "You would either have to inject the expression as code". How is that different from bc? bc can't even take external input, all you can do is inject code to it.

    – Stéphane Chazelas
    8 hours ago











  • @StéphaneChazelas The difference is that it's the normal mode of operation for bc to read expressions from standard input. It is not the normal mode of operation of awk to insert shell code (user input) into the string that serves as the actual code.

    – Kusalananda
    6 hours ago
















1












1








1







awk does not have a way to evaluate a general arithmetic expression given to it as a string. bc, on the other hand, takes an expression and evaluates it, as does the shell.



Would you want to evaluate the string as a mathematical expression in awk, you would either have to inject the expression as code into the awk program (potentially creating a code injection vulnerability), or simply write your own parser for the types of expressions you'd like to evaluate (this is a common computer science homework assignment).



You get 12 from your awk code because the value of expression_to_evaluate, when converted into an integer, can be converted up until the space character in the string. This is how the strtol() C library function works when it converts a string into an integer, and awk is likely using this under the hood.






share|improve this answer













awk does not have a way to evaluate a general arithmetic expression given to it as a string. bc, on the other hand, takes an expression and evaluates it, as does the shell.



Would you want to evaluate the string as a mathematical expression in awk, you would either have to inject the expression as code into the awk program (potentially creating a code injection vulnerability), or simply write your own parser for the types of expressions you'd like to evaluate (this is a common computer science homework assignment).



You get 12 from your awk code because the value of expression_to_evaluate, when converted into an integer, can be converted up until the space character in the string. This is how the strtol() C library function works when it converts a string into an integer, and awk is likely using this under the hood.







share|improve this answer












share|improve this answer



share|improve this answer










answered 19 hours ago









KusalanandaKusalananda

133k17254417




133k17254417













  • "You would either have to inject the expression as code". How is that different from bc? bc can't even take external input, all you can do is inject code to it.

    – Stéphane Chazelas
    8 hours ago











  • @StéphaneChazelas The difference is that it's the normal mode of operation for bc to read expressions from standard input. It is not the normal mode of operation of awk to insert shell code (user input) into the string that serves as the actual code.

    – Kusalananda
    6 hours ago





















  • "You would either have to inject the expression as code". How is that different from bc? bc can't even take external input, all you can do is inject code to it.

    – Stéphane Chazelas
    8 hours ago











  • @StéphaneChazelas The difference is that it's the normal mode of operation for bc to read expressions from standard input. It is not the normal mode of operation of awk to insert shell code (user input) into the string that serves as the actual code.

    – Kusalananda
    6 hours ago



















"You would either have to inject the expression as code". How is that different from bc? bc can't even take external input, all you can do is inject code to it.

– Stéphane Chazelas
8 hours ago





"You would either have to inject the expression as code". How is that different from bc? bc can't even take external input, all you can do is inject code to it.

– Stéphane Chazelas
8 hours ago













@StéphaneChazelas The difference is that it's the normal mode of operation for bc to read expressions from standard input. It is not the normal mode of operation of awk to insert shell code (user input) into the string that serves as the actual code.

– Kusalananda
6 hours ago







@StéphaneChazelas The difference is that it's the normal mode of operation for bc to read expressions from standard input. It is not the normal mode of operation of awk to insert shell code (user input) into the string that serves as the actual code.

– Kusalananda
6 hours ago













0














echo $expression_to_evaluate | awk -F "+" '{print ($1+$2)}'





share|improve this answer
























  • They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

    – Jeff Schaller
    6 hours ago
















0














echo $expression_to_evaluate | awk -F "+" '{print ($1+$2)}'





share|improve this answer
























  • They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

    – Jeff Schaller
    6 hours ago














0












0








0







echo $expression_to_evaluate | awk -F "+" '{print ($1+$2)}'





share|improve this answer













echo $expression_to_evaluate | awk -F "+" '{print ($1+$2)}'






share|improve this answer












share|improve this answer



share|improve this answer










answered 19 hours ago









editiniteditinit

1236




1236













  • They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

    – Jeff Schaller
    6 hours ago



















  • They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

    – Jeff Schaller
    6 hours ago

















They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

– Jeff Schaller
6 hours ago





They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

– Jeff Schaller
6 hours ago











0














if You want to add 2 numbers using awk use below command



awk '{print 12 + 3;exit}'

output
15





share|improve this answer
























  • They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

    – Jeff Schaller
    6 hours ago
















0














if You want to add 2 numbers using awk use below command



awk '{print 12 + 3;exit}'

output
15





share|improve this answer
























  • They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

    – Jeff Schaller
    6 hours ago














0












0








0







if You want to add 2 numbers using awk use below command



awk '{print 12 + 3;exit}'

output
15





share|improve this answer













if You want to add 2 numbers using awk use below command



awk '{print 12 + 3;exit}'

output
15






share|improve this answer












share|improve this answer



share|improve this answer










answered 8 hours ago









Praveen Kumar BSPraveen Kumar BS

1,5041310




1,5041310













  • They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

    – Jeff Schaller
    6 hours ago



















  • They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

    – Jeff Schaller
    6 hours ago

















They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

– Jeff Schaller
6 hours ago





They didn't ask for static addition, they asked How can I feed a string to awk for the purpose of mathematical calculation?

– Jeff Schaller
6 hours ago


















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%2f503691%2fhow-can-i-feed-a-string-to-awk-for-the-purpose-of-mathematical-calculation%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 rewrite equation of hyperbola in standard form

No ethernet ip address in my vocore2