Sum of command line arguments?
up vote
0
down vote
favorite
How would I create a bash script that the user can use to sum any amount of command line arguments, but it will also ignore incompatible arguments? For example, say my script is called sum:
sum 10 -argument1 -argument2 -argument3 20
30
sum 7 -argument1 2 -argument2 1 -argument3
10
shell scripting arithmetic numeric-data
New contributor
add a comment |
up vote
0
down vote
favorite
How would I create a bash script that the user can use to sum any amount of command line arguments, but it will also ignore incompatible arguments? For example, say my script is called sum:
sum 10 -argument1 -argument2 -argument3 20
30
sum 7 -argument1 2 -argument2 1 -argument3
10
shell scripting arithmetic numeric-data
New contributor
2
What qualifies as an incompatible argument? What about -12, 1.2, 1,200,000, 1e2, 0x2, 0x4p5, nan, infinity, 2#1100, 0b1001, VII? Which of those qualify? Is 010 to be treated as octal or decimal? Should the locale be honoured when parsing radix and thousand separators?
– Stéphane Chazelas
Nov 22 at 13:08
(010), or binary @StéphaneChazelas :)
– Romeo Ninov
Nov 22 at 13:09
integers should qualify as compatible arguments
– Calin Chaly
Nov 22 at 13:15
Very similar to bash script for work
– Kusalananda
Nov 22 at 13:15
2
0xff, 010, 0b101, 1,200,000, -12, +12, 1.1e2, 0x4p5, 2#1100, VII are all some representation of some integer, should they be considered?
– Stéphane Chazelas
Nov 22 at 13:18
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How would I create a bash script that the user can use to sum any amount of command line arguments, but it will also ignore incompatible arguments? For example, say my script is called sum:
sum 10 -argument1 -argument2 -argument3 20
30
sum 7 -argument1 2 -argument2 1 -argument3
10
shell scripting arithmetic numeric-data
New contributor
How would I create a bash script that the user can use to sum any amount of command line arguments, but it will also ignore incompatible arguments? For example, say my script is called sum:
sum 10 -argument1 -argument2 -argument3 20
30
sum 7 -argument1 2 -argument2 1 -argument3
10
shell scripting arithmetic numeric-data
shell scripting arithmetic numeric-data
New contributor
New contributor
edited Nov 22 at 14:08
Jeff Schaller
36.8k1052121
36.8k1052121
New contributor
asked Nov 22 at 13:04
Calin Chaly
61
61
New contributor
New contributor
2
What qualifies as an incompatible argument? What about -12, 1.2, 1,200,000, 1e2, 0x2, 0x4p5, nan, infinity, 2#1100, 0b1001, VII? Which of those qualify? Is 010 to be treated as octal or decimal? Should the locale be honoured when parsing radix and thousand separators?
– Stéphane Chazelas
Nov 22 at 13:08
(010), or binary @StéphaneChazelas :)
– Romeo Ninov
Nov 22 at 13:09
integers should qualify as compatible arguments
– Calin Chaly
Nov 22 at 13:15
Very similar to bash script for work
– Kusalananda
Nov 22 at 13:15
2
0xff, 010, 0b101, 1,200,000, -12, +12, 1.1e2, 0x4p5, 2#1100, VII are all some representation of some integer, should they be considered?
– Stéphane Chazelas
Nov 22 at 13:18
add a comment |
2
What qualifies as an incompatible argument? What about -12, 1.2, 1,200,000, 1e2, 0x2, 0x4p5, nan, infinity, 2#1100, 0b1001, VII? Which of those qualify? Is 010 to be treated as octal or decimal? Should the locale be honoured when parsing radix and thousand separators?
– Stéphane Chazelas
Nov 22 at 13:08
(010), or binary @StéphaneChazelas :)
– Romeo Ninov
Nov 22 at 13:09
integers should qualify as compatible arguments
– Calin Chaly
Nov 22 at 13:15
Very similar to bash script for work
– Kusalananda
Nov 22 at 13:15
2
0xff, 010, 0b101, 1,200,000, -12, +12, 1.1e2, 0x4p5, 2#1100, VII are all some representation of some integer, should they be considered?
– Stéphane Chazelas
Nov 22 at 13:18
2
2
What qualifies as an incompatible argument? What about -12, 1.2, 1,200,000, 1e2, 0x2, 0x4p5, nan, infinity, 2#1100, 0b1001, VII? Which of those qualify? Is 010 to be treated as octal or decimal? Should the locale be honoured when parsing radix and thousand separators?
– Stéphane Chazelas
Nov 22 at 13:08
What qualifies as an incompatible argument? What about -12, 1.2, 1,200,000, 1e2, 0x2, 0x4p5, nan, infinity, 2#1100, 0b1001, VII? Which of those qualify? Is 010 to be treated as octal or decimal? Should the locale be honoured when parsing radix and thousand separators?
– Stéphane Chazelas
Nov 22 at 13:08
(010), or binary @StéphaneChazelas :)
– Romeo Ninov
Nov 22 at 13:09
(010), or binary @StéphaneChazelas :)
– Romeo Ninov
Nov 22 at 13:09
integers should qualify as compatible arguments
– Calin Chaly
Nov 22 at 13:15
integers should qualify as compatible arguments
– Calin Chaly
Nov 22 at 13:15
Very similar to bash script for work
– Kusalananda
Nov 22 at 13:15
Very similar to bash script for work
– Kusalananda
Nov 22 at 13:15
2
2
0xff, 010, 0b101, 1,200,000, -12, +12, 1.1e2, 0x4p5, 2#1100, VII are all some representation of some integer, should they be considered?
– Stéphane Chazelas
Nov 22 at 13:18
0xff, 010, 0b101, 1,200,000, -12, +12, 1.1e2, 0x4p5, 2#1100, VII are all some representation of some integer, should they be considered?
– Stéphane Chazelas
Nov 22 at 13:18
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
With awk
, assuming the sum and each individual number argument can fit in your system's long
type, and only considering sequences of decimal digits with an optional leading -
sign:
#! /bin/sh -
awk 'BEGIN{
sum = 0
for (i = 1; i < ARGC; i++)
if (ARGV[i] ~ /^-?[0123456789]+$/)
sum += ARGV[i]
print sum}' "$@"
For arbitrary precision, you can use bc
:
#! /bin/sh -
awk 'BEGIN{
for (i = 1; i < ARGC; i++)
if (ARGV[i] ~ /^-?[0123456789]+$/)
print "s+="ARGV[i]
print "s"}' "$@" | bc
Example:
$ ./sum1 999999999999999999999999 1
999999999999999983222784
$ ./sum2 999999999999999999999999 1
1000000000000000000000000
add a comment |
up vote
0
down vote
One (nonoptimal) example to do the sum:
echo "$@" |awk '{for (i = 1; i <= NF; i++) j+=$i} END {print j}'
(assuming number is everything awk
accept as such)
Example:
./ff 1 2 3.1 0x10 0100
86.1
And warning: any word which start with number will be interpreted as number
Forawk
, in that context,1WHATEVER2
would be understood as1
.
– Stéphane Chazelas
Nov 22 at 13:20
@StéphaneChazelasm true, but this accept integer, float, with exponent, hex, octal.....
– Romeo Ninov
Nov 22 at 13:22
Unquoted$@
might as well be$*
– roaima
Nov 22 at 14:11
... and so you should use"$@"
so the parameter"*"
does not expand to a list of files.
– glenn jackman
Nov 22 at 14:18
Answer edited :)
– Romeo Ninov
Nov 22 at 14:19
add a comment |
up vote
0
down vote
Here's another possibility
printf "%sn" "$@" | awk '$1 ~ /^[[:digit:]]+$/ {s+=$1} END {print s}'
It takes each argument from the command line and prints it, one per line. The awk
script accepts this data and adds up non-zero positive values, printing the result when it's received all the data
Example
#!/bin/sh
printf "%sn" "$@" | awk '$1 ~ /^[[:digit:]]+$/ {s+=$1} END {print s}'
./sum 1 -argument2 2 -argument4 3
6
If you could guarantee that your numeric arguments contained only digits you could simplify the check expression from $1 ~ /^[[:digit:]]+$/
to $1+0 > 0
.
That fails to exclude arguments like$'foon1234'
.
– Stéphane Chazelas
Nov 22 at 17:40
@StéphaneChazelas indeed. Let's get a handle on what constitutes a number, first?
– roaima
Nov 22 at 18:04
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
With awk
, assuming the sum and each individual number argument can fit in your system's long
type, and only considering sequences of decimal digits with an optional leading -
sign:
#! /bin/sh -
awk 'BEGIN{
sum = 0
for (i = 1; i < ARGC; i++)
if (ARGV[i] ~ /^-?[0123456789]+$/)
sum += ARGV[i]
print sum}' "$@"
For arbitrary precision, you can use bc
:
#! /bin/sh -
awk 'BEGIN{
for (i = 1; i < ARGC; i++)
if (ARGV[i] ~ /^-?[0123456789]+$/)
print "s+="ARGV[i]
print "s"}' "$@" | bc
Example:
$ ./sum1 999999999999999999999999 1
999999999999999983222784
$ ./sum2 999999999999999999999999 1
1000000000000000000000000
add a comment |
up vote
1
down vote
With awk
, assuming the sum and each individual number argument can fit in your system's long
type, and only considering sequences of decimal digits with an optional leading -
sign:
#! /bin/sh -
awk 'BEGIN{
sum = 0
for (i = 1; i < ARGC; i++)
if (ARGV[i] ~ /^-?[0123456789]+$/)
sum += ARGV[i]
print sum}' "$@"
For arbitrary precision, you can use bc
:
#! /bin/sh -
awk 'BEGIN{
for (i = 1; i < ARGC; i++)
if (ARGV[i] ~ /^-?[0123456789]+$/)
print "s+="ARGV[i]
print "s"}' "$@" | bc
Example:
$ ./sum1 999999999999999999999999 1
999999999999999983222784
$ ./sum2 999999999999999999999999 1
1000000000000000000000000
add a comment |
up vote
1
down vote
up vote
1
down vote
With awk
, assuming the sum and each individual number argument can fit in your system's long
type, and only considering sequences of decimal digits with an optional leading -
sign:
#! /bin/sh -
awk 'BEGIN{
sum = 0
for (i = 1; i < ARGC; i++)
if (ARGV[i] ~ /^-?[0123456789]+$/)
sum += ARGV[i]
print sum}' "$@"
For arbitrary precision, you can use bc
:
#! /bin/sh -
awk 'BEGIN{
for (i = 1; i < ARGC; i++)
if (ARGV[i] ~ /^-?[0123456789]+$/)
print "s+="ARGV[i]
print "s"}' "$@" | bc
Example:
$ ./sum1 999999999999999999999999 1
999999999999999983222784
$ ./sum2 999999999999999999999999 1
1000000000000000000000000
With awk
, assuming the sum and each individual number argument can fit in your system's long
type, and only considering sequences of decimal digits with an optional leading -
sign:
#! /bin/sh -
awk 'BEGIN{
sum = 0
for (i = 1; i < ARGC; i++)
if (ARGV[i] ~ /^-?[0123456789]+$/)
sum += ARGV[i]
print sum}' "$@"
For arbitrary precision, you can use bc
:
#! /bin/sh -
awk 'BEGIN{
for (i = 1; i < ARGC; i++)
if (ARGV[i] ~ /^-?[0123456789]+$/)
print "s+="ARGV[i]
print "s"}' "$@" | bc
Example:
$ ./sum1 999999999999999999999999 1
999999999999999983222784
$ ./sum2 999999999999999999999999 1
1000000000000000000000000
edited Nov 22 at 18:43
answered Nov 22 at 13:16
Stéphane Chazelas
295k54556898
295k54556898
add a comment |
add a comment |
up vote
0
down vote
One (nonoptimal) example to do the sum:
echo "$@" |awk '{for (i = 1; i <= NF; i++) j+=$i} END {print j}'
(assuming number is everything awk
accept as such)
Example:
./ff 1 2 3.1 0x10 0100
86.1
And warning: any word which start with number will be interpreted as number
Forawk
, in that context,1WHATEVER2
would be understood as1
.
– Stéphane Chazelas
Nov 22 at 13:20
@StéphaneChazelasm true, but this accept integer, float, with exponent, hex, octal.....
– Romeo Ninov
Nov 22 at 13:22
Unquoted$@
might as well be$*
– roaima
Nov 22 at 14:11
... and so you should use"$@"
so the parameter"*"
does not expand to a list of files.
– glenn jackman
Nov 22 at 14:18
Answer edited :)
– Romeo Ninov
Nov 22 at 14:19
add a comment |
up vote
0
down vote
One (nonoptimal) example to do the sum:
echo "$@" |awk '{for (i = 1; i <= NF; i++) j+=$i} END {print j}'
(assuming number is everything awk
accept as such)
Example:
./ff 1 2 3.1 0x10 0100
86.1
And warning: any word which start with number will be interpreted as number
Forawk
, in that context,1WHATEVER2
would be understood as1
.
– Stéphane Chazelas
Nov 22 at 13:20
@StéphaneChazelasm true, but this accept integer, float, with exponent, hex, octal.....
– Romeo Ninov
Nov 22 at 13:22
Unquoted$@
might as well be$*
– roaima
Nov 22 at 14:11
... and so you should use"$@"
so the parameter"*"
does not expand to a list of files.
– glenn jackman
Nov 22 at 14:18
Answer edited :)
– Romeo Ninov
Nov 22 at 14:19
add a comment |
up vote
0
down vote
up vote
0
down vote
One (nonoptimal) example to do the sum:
echo "$@" |awk '{for (i = 1; i <= NF; i++) j+=$i} END {print j}'
(assuming number is everything awk
accept as such)
Example:
./ff 1 2 3.1 0x10 0100
86.1
And warning: any word which start with number will be interpreted as number
One (nonoptimal) example to do the sum:
echo "$@" |awk '{for (i = 1; i <= NF; i++) j+=$i} END {print j}'
(assuming number is everything awk
accept as such)
Example:
./ff 1 2 3.1 0x10 0100
86.1
And warning: any word which start with number will be interpreted as number
edited Nov 22 at 14:19
answered Nov 22 at 13:19
Romeo Ninov
4,98431627
4,98431627
Forawk
, in that context,1WHATEVER2
would be understood as1
.
– Stéphane Chazelas
Nov 22 at 13:20
@StéphaneChazelasm true, but this accept integer, float, with exponent, hex, octal.....
– Romeo Ninov
Nov 22 at 13:22
Unquoted$@
might as well be$*
– roaima
Nov 22 at 14:11
... and so you should use"$@"
so the parameter"*"
does not expand to a list of files.
– glenn jackman
Nov 22 at 14:18
Answer edited :)
– Romeo Ninov
Nov 22 at 14:19
add a comment |
Forawk
, in that context,1WHATEVER2
would be understood as1
.
– Stéphane Chazelas
Nov 22 at 13:20
@StéphaneChazelasm true, but this accept integer, float, with exponent, hex, octal.....
– Romeo Ninov
Nov 22 at 13:22
Unquoted$@
might as well be$*
– roaima
Nov 22 at 14:11
... and so you should use"$@"
so the parameter"*"
does not expand to a list of files.
– glenn jackman
Nov 22 at 14:18
Answer edited :)
– Romeo Ninov
Nov 22 at 14:19
For
awk
, in that context, 1WHATEVER2
would be understood as 1
.– Stéphane Chazelas
Nov 22 at 13:20
For
awk
, in that context, 1WHATEVER2
would be understood as 1
.– Stéphane Chazelas
Nov 22 at 13:20
@StéphaneChazelasm true, but this accept integer, float, with exponent, hex, octal.....
– Romeo Ninov
Nov 22 at 13:22
@StéphaneChazelasm true, but this accept integer, float, with exponent, hex, octal.....
– Romeo Ninov
Nov 22 at 13:22
Unquoted
$@
might as well be $*
– roaima
Nov 22 at 14:11
Unquoted
$@
might as well be $*
– roaima
Nov 22 at 14:11
... and so you should use
"$@"
so the parameter "*"
does not expand to a list of files.– glenn jackman
Nov 22 at 14:18
... and so you should use
"$@"
so the parameter "*"
does not expand to a list of files.– glenn jackman
Nov 22 at 14:18
Answer edited :)
– Romeo Ninov
Nov 22 at 14:19
Answer edited :)
– Romeo Ninov
Nov 22 at 14:19
add a comment |
up vote
0
down vote
Here's another possibility
printf "%sn" "$@" | awk '$1 ~ /^[[:digit:]]+$/ {s+=$1} END {print s}'
It takes each argument from the command line and prints it, one per line. The awk
script accepts this data and adds up non-zero positive values, printing the result when it's received all the data
Example
#!/bin/sh
printf "%sn" "$@" | awk '$1 ~ /^[[:digit:]]+$/ {s+=$1} END {print s}'
./sum 1 -argument2 2 -argument4 3
6
If you could guarantee that your numeric arguments contained only digits you could simplify the check expression from $1 ~ /^[[:digit:]]+$/
to $1+0 > 0
.
That fails to exclude arguments like$'foon1234'
.
– Stéphane Chazelas
Nov 22 at 17:40
@StéphaneChazelas indeed. Let's get a handle on what constitutes a number, first?
– roaima
Nov 22 at 18:04
add a comment |
up vote
0
down vote
Here's another possibility
printf "%sn" "$@" | awk '$1 ~ /^[[:digit:]]+$/ {s+=$1} END {print s}'
It takes each argument from the command line and prints it, one per line. The awk
script accepts this data and adds up non-zero positive values, printing the result when it's received all the data
Example
#!/bin/sh
printf "%sn" "$@" | awk '$1 ~ /^[[:digit:]]+$/ {s+=$1} END {print s}'
./sum 1 -argument2 2 -argument4 3
6
If you could guarantee that your numeric arguments contained only digits you could simplify the check expression from $1 ~ /^[[:digit:]]+$/
to $1+0 > 0
.
That fails to exclude arguments like$'foon1234'
.
– Stéphane Chazelas
Nov 22 at 17:40
@StéphaneChazelas indeed. Let's get a handle on what constitutes a number, first?
– roaima
Nov 22 at 18:04
add a comment |
up vote
0
down vote
up vote
0
down vote
Here's another possibility
printf "%sn" "$@" | awk '$1 ~ /^[[:digit:]]+$/ {s+=$1} END {print s}'
It takes each argument from the command line and prints it, one per line. The awk
script accepts this data and adds up non-zero positive values, printing the result when it's received all the data
Example
#!/bin/sh
printf "%sn" "$@" | awk '$1 ~ /^[[:digit:]]+$/ {s+=$1} END {print s}'
./sum 1 -argument2 2 -argument4 3
6
If you could guarantee that your numeric arguments contained only digits you could simplify the check expression from $1 ~ /^[[:digit:]]+$/
to $1+0 > 0
.
Here's another possibility
printf "%sn" "$@" | awk '$1 ~ /^[[:digit:]]+$/ {s+=$1} END {print s}'
It takes each argument from the command line and prints it, one per line. The awk
script accepts this data and adds up non-zero positive values, printing the result when it's received all the data
Example
#!/bin/sh
printf "%sn" "$@" | awk '$1 ~ /^[[:digit:]]+$/ {s+=$1} END {print s}'
./sum 1 -argument2 2 -argument4 3
6
If you could guarantee that your numeric arguments contained only digits you could simplify the check expression from $1 ~ /^[[:digit:]]+$/
to $1+0 > 0
.
edited Nov 22 at 14:21
answered Nov 22 at 14:14
roaima
42.1k550115
42.1k550115
That fails to exclude arguments like$'foon1234'
.
– Stéphane Chazelas
Nov 22 at 17:40
@StéphaneChazelas indeed. Let's get a handle on what constitutes a number, first?
– roaima
Nov 22 at 18:04
add a comment |
That fails to exclude arguments like$'foon1234'
.
– Stéphane Chazelas
Nov 22 at 17:40
@StéphaneChazelas indeed. Let's get a handle on what constitutes a number, first?
– roaima
Nov 22 at 18:04
That fails to exclude arguments like
$'foon1234'
.– Stéphane Chazelas
Nov 22 at 17:40
That fails to exclude arguments like
$'foon1234'
.– Stéphane Chazelas
Nov 22 at 17:40
@StéphaneChazelas indeed. Let's get a handle on what constitutes a number, first?
– roaima
Nov 22 at 18:04
@StéphaneChazelas indeed. Let's get a handle on what constitutes a number, first?
– roaima
Nov 22 at 18:04
add a comment |
Calin Chaly is a new contributor. Be nice, and check out our Code of Conduct.
Calin Chaly is a new contributor. Be nice, and check out our Code of Conduct.
Calin Chaly is a new contributor. Be nice, and check out our Code of Conduct.
Calin Chaly is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483446%2fsum-of-command-line-arguments%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
What qualifies as an incompatible argument? What about -12, 1.2, 1,200,000, 1e2, 0x2, 0x4p5, nan, infinity, 2#1100, 0b1001, VII? Which of those qualify? Is 010 to be treated as octal or decimal? Should the locale be honoured when parsing radix and thousand separators?
– Stéphane Chazelas
Nov 22 at 13:08
(010), or binary @StéphaneChazelas :)
– Romeo Ninov
Nov 22 at 13:09
integers should qualify as compatible arguments
– Calin Chaly
Nov 22 at 13:15
Very similar to bash script for work
– Kusalananda
Nov 22 at 13:15
2
0xff, 010, 0b101, 1,200,000, -12, +12, 1.1e2, 0x4p5, 2#1100, VII are all some representation of some integer, should they be considered?
– Stéphane Chazelas
Nov 22 at 13:18