Skip to content

Instantly share code, notes, and snippets.

@n-johnson
Created February 22, 2016 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n-johnson/aaab28f4db9d8d443bb3 to your computer and use it in GitHub Desktop.
Save n-johnson/aaab28f4db9d8d443bb3 to your computer and use it in GitHub Desktop.
Bash utility to output a range of bytes from a stream
#!/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