Skip to content

Instantly share code, notes, and snippets.

@dualbus
Created October 3, 2013 15:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dualbus/6811562 to your computer and use it in GitHub Desktop.
Save dualbus/6811562 to your computer and use it in GitHub Desktop.
#!/bin/bash
trap 'rm -rf "$tempdir"' EXIT
tempdir=$(mktemp -d)
cd "$tempdir" || exit 1
printf '%s\n' \
'(return 2>/dev/null); echo $?' \
> bash_return
printf '%s\n' \
'printf %s\\n "$((! BASH_LINENO))"' \
> bash_lineno
printf '%s\n' 'should pass'
for approach in bash_{lineno,return}; do
printf '%s ' "$approach"
{
bash -c ". $approach"
bash -s <<< ". $approach"
printf '. %s\n' "$approach" > "$approach"_test;
chmod +x "$approach"_test;
./"$approach"_test;
bash "$approach"_test;
. "$approach"_test;
. "$approach";
} | tr '\n01' ' ox'
printf ': %s\n' "$(< "$approach")"
done
printf '%.s-' {0..79}; printf \\n
printf '%s\n' 'should fail'
for approach in bash_{lineno,return}; do
printf '%s ' "$approach"
{
bash "$approach"
bash -s < "$approach"
eval "$(<"$approach")"
} | tr '\n01' ' ox'
printf ': %s\n' "$(< "$approach")"
done
@dualbus
Copy link
Author

dualbus commented Apr 1, 2015

@ejhuff Wow, this thing doesn't notify me. I agree with your points. Specially the second, I started doing: unset var; trap ' uses var ' FOO; var=... for this precisely, but I guess that was before I wrote this :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment