Last active
December 19, 2015 07:48
-
-
Save vjt/5920790 to your computer and use it in GitHub Desktop.
Extract a stored procedure's text off a Sybase database from the command line. Required tools: sqsh, GNU sed
This file contains hidden or 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/sh | |
# | |
# Poor man's defncopy implementation in Bourne Shell. | |
# | |
# Usage: ./extract_sproc.sh <sproc> <sqsh_options> | |
# | |
# Example: ./extract_sproc.sh foobar -S SERVER -U USER -P PASS -D DATABASE | |
# | |
# Description: Uses sqsh to invoke Sybase's sp_helptext that fetch a sproc | |
# definition from the system tables. Uses the "csv" output, that preserves | |
# whitespace, and feeds into sed to merge together different lines. Public | |
# Domain, do whatever you want with this. :-) | |
# | |
# Author: Marcello Barnaba <[email protected]> | |
# | |
# - vjt Wed Jul 3 19:08:55 CEST 2013 | |
# | |
sproc=$1 | |
shift 1 | |
if [ -z "$sproc" -o $# -eq 0 ]; then | |
echo "Usage: $0 <sproc> <sqsh options>" | |
exit 1 | |
fi | |
## | |
# Sanity checks | |
# | |
sqsh=$(which sqsh) | |
if ! [ -x "$sqsh" ]; then | |
echo "This script requires sqsh <http://www.sqsh.org/>." | |
exit 2 | |
fi | |
sed=$(which sed) | |
if ! [ -x "$sed" ]; then | |
echo "This script requires sed(1)." | |
exit 3 | |
fi | |
if ! sed --version 2>&1 | grep -q '^GNU sed'; then | |
echo "This script requires GNU sed <http://www.gnu.org/software/sed/>." | |
exit 4 | |
fi | |
## | |
# The actual code | |
# | |
echo -e "sp_helptext $sproc;" |\ | |
$sqsh "$@" -m csv |\ | |
$sed \ | |
-e ':x;N;$!bx;s/"\n"//g' \ | |
-e 's/"\(\nClock.*\)\?$//' \ | |
-e 's/^# Lines of Text.*text\n"//' | |
# EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment