Bash operations
3
0
Entering edit mode
3.5 years ago
FadyNabil ▴ 20

How to write a bash script that receives ANY integer numbers and print their mean with 2 decimal digit accuracy?

Bash • 2.0k views
ADD COMMENT
4
Entering edit mode

Please explain what you have tried and the issues you are facing. Biostars is not a code writing service - you must show some effort for folks to help you. Additionally, this has nothing to do with bioinformatics as it stands. Can you edit your question to make it more relevant?

ADD REPLY
4
Entering edit mode
3.5 years ago

Here's one such script:

#!/usr/bin/env bash

echo ${1} | awk -v RS=" " '{ n++; s+=$0 } END { printf("%.2f\n", s/n) }'

To run it:

$ ./mean.sh "10 20 60"
30.00

Don't forget quotation marks!

ADD COMMENT
0
Entering edit mode

can i type mean.sh, then press enter, and write my numbers, then press enter then it gives me the mean of these numbers?

ADD REPLY
1
Entering edit mode

It is best to run it as advertised:

./mean.sh "10 20 60"

...or:

bash mean.sh "10 20 60"
ADD REPLY
0
Entering edit mode

To answer your question, you'll need to specify numbers up front with the script as written.

If you want to enter numbers on a separate line, you could use the following:

#!/usr/bin/env bash

awk -v RS=" " '{ n++; s+=$0 } END { printf("%.2f\n", s/n) }' < /dev/stdin

You would then run ./mean.sh, press enter, type in your numbers (no quotation marks required), press enter, and press Control-D to send an end-of-file character (this is required, however). Then you'll get back the mean of the numbers you entered.

With this second script, you could also use echo to pipe numbers to the script's standard input:

$ echo "10 20 60" | ./mean.sh
30.00

But that's basically the same thing as the first script, moving the echo statement outside.

ADD REPLY
0
Entering edit mode
3.5 years ago

I got bored. Good luck in the coding exam / interview

mean.sh

#!/bin/bash
intarray=( "$@" ) ;
sum=0 ;
for i in ${intarray[@]} ;
do
  let sum+=$i ;
done ;
echo -e "--Sum is:\t$sum"
mean=$(bc <<< "scale=2; $sum/${#intarray[@]}") ;
echo -e "--Mean is:\t${mean}" ;

Then:

numbers=(1 2 3 4)
bash mean.sh "${numbers[@]}"
--Sum is:   10
--Mean is:  2.50

numbers=(37 8 20 6 29)    
bash mean.sh "${numbers[@]}"
--Sum is:   100
--Mean is:  20.00
ADD COMMENT
0
Entering edit mode
3.5 years ago
#! /usr/bin/env bash
printf "%s\n" ${1} | awk '{sum+=$0} END {printf("%.2f\n", sum/NR)}'

to run it:

$ ./mean.sh "10 20 60"
30.00

if you are okay with datamash instead of awk, you can try this:

$  cat mean_dm.sh 

#! /usr/bin/env bash
printf "%s\n" ${1} | datamash mean 1 median 1 -R 2

%  ./mean_dm.sh "10 20 60"

30.00

Advantage of having datamash is that you can print multiple statistics without writing much of the additional code:

$ cat ./mean_dm.sh 

#! /usr/bin/env bash
printf "%s\n" ${1} | datamash mean 1 median 1 min 1 max 1 sstdev 1 -R 2

 $ ./mean_dm.sh "10 20 60"

30.00   20.00   10.00   60.00   26.46
ADD COMMENT
0
Entering edit mode

Thank for informing me of datamash. Does it also spit out 1Q 2Q 3Q 4Q stats? How/where do i learn more about it?

ADD REPLY
0
Entering edit mode

Found an answer to my question at https://www.gnu.org/software/datamash/examples

ADD REPLY

Login before adding your answer.

Traffic: 1879 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6