#!/bin/bash # count_numbers.bash # Written by Aditya and AJ # # This is a relatively simple bash script. We went through a couple # iterations - the comments are leftover from when I was trying to # make it work without calling out to `seq` (which outputs number # sequences to stdout). As it turns out, `{num..num2}` expansion # is (for the sake of performance) a "dumb" macro, and will not # expand variable references. However, even if it _did_ expand # variable references, there would still be the problem that this # would cause memory problems for large numbers because all numbers # would fully expand while building the commandline, and _then_ # they'd be piped into `fgrep`. The `seq` approach, while less # performant in the sense that it calls out to an external binary, # is better because numbers are streamed into `fgrep`, so memory # usage won't balloon. # # See also https://chat.stackexchange.com/transcript/26?m=35416093#35416093 # for a discussion of how piping into a shell function works. function count_numbers() { fgrep -v 5 | wc -l } # { while read data; do echo $data; } #echo {$0..$1} | seq $1 $2 | count_numbers