Created
February 22, 2016 00:33
-
-
Save n-johnson/aaab28f4db9d8d443bb3 to your computer and use it in GitHub Desktop.
Bash utility to output a range of bytes from a stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# byte_range $start $end | |
# - Outputs the bytes between the starting and ending values from the stdin stream | |
# | |
# - $start - Starting byte to display | |
# - $end - Ending byte to display | |
isInteger() { | |
local regex_numeric='^[0-9]+$' | |
[[ "$1" =~ $regex_numeric ]] | |
} | |
byteRange() { | |
local start="$1" | |
local length="$2" | |
tail -c +"$start" | head -c "$length" | |
} | |
checkInputs() { | |
local start="$1" | |
local end="$2" | |
if ! isInteger "$start" || ! isInteger "$end"; then | |
echo "Non-integer provided as input. Exiting" | |
exit 1 | |
fi | |
if [ $start -ge $end ]; then | |
echo "Starting value must be less than ending value" | |
exit 1 | |
fi | |
} | |
diff() { | |
echo "$1 - $2" | bc | |
} | |
checkInputs "$1" "$2" | |
START="$1"; END="$2"; LENGTH=$(diff "$END" "$START"); | |
cat - | byteRange "$START" "$LENGTH" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment