I need median in one of my bash scripts.
A friend of mine is encouraging to leave bash alone and start with perl.
On the inet I found this little perl implementation which I now want to use within my bash scripts. A little executable file in /usr/local/sbin/median
Code:
#!/usr/bin/perl
@nums = sort{$a <=> $b} @ARGV;
$med = $nums[($#nums / 2)];
print $med . "\n";
The problem now is that I don't want to pass the parameters on the command line, but I want to pipe them like this.
Code:
# echo '3
> 1
> 3
> 4
> 5
> 2' | median
#
should give the same output as:
Code:
# median 3 1 3 4 5 2
3
#
I would like to have the command-line to take precedence.
If no command line is given it should take STDIN as the parameter.
This could be a (very belated) start with perl for me...
I would really appreciate any input