Skip to content

Instantly share code, notes, and snippets.

@vjt
Last active December 19, 2015 07:48
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 vjt/5920790 to your computer and use it in GitHub Desktop.
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
#!/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 <vjt@openssl.it>
#
# - 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