Skip to content

Instantly share code, notes, and snippets.

@roktas
Created May 23, 2014 21:50
Show Gist options
  • Save roktas/e99592a6ea8730c0ad4e to your computer and use it in GitHub Desktop.
Save roktas/e99592a6ea8730c0ad4e to your computer and use it in GitHub Desktop.
#!/bin/bash
herestring() {
local lines lead start cur end
mapfile -t lines < <(echo "$1")
while [[ $start -lt ${#lines[@]} ]] && [[ ${lines[$start]} =~ ^[[:space:]]*$ ]]; do
((start++))
done
end=$((${#lines[@]} - 1))
while [[ $end -gt 0 ]] && [[ ${lines[$end]} =~ ^[[:space:]]*$ ]]; do
((end--))
done
if [[ ${lines[$start]} =~ ^([[:space:]]+) ]]; then
lead=${BASH_REMATCH[1]}
fi
for ((cur = start; cur <= end; cur++)); do
echo "${lines[$cur]/$lead/}"
done
}
herestring '
foo
bar
baz
quux
'
unexpand <<-EOF
# encoding: utf-8
preload_app true
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment