Skip to content

Instantly share code, notes, and snippets.

@strugee
Created March 3, 2017 22:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save strugee/6816df8b02a50970e5d513c2ed60935a to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment