Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm not a ksh scripter, but the syntax was similar enough to bash that I did some digging.
It might be useful for you to change your first line to:
Code:
#!/bin/ksh -x
The "-x" will help debug the script. According to an online copy of the ksh man page:
Quote:
Code:
set [+-abCefhkmnpsuvxX] [+-o [option]] [+-A name] [--] [arg ...]
The set command can be used to set (-) or clear (+) shell options, ...
<snip>
-x xtrace Print commands and parameter assignments when they are executed, preceded by the value of PS4.
Also, just at a quick glance (assuming ksh and bash are the same in this regard), you use a test:
Code:
if [ $# -ne 1 ]; then
$# should expand to the number of positional parameters. In your example command "./asbman training -query", $# should equal 2 ("training" and "-query"). And since your test is whether $# equals 1, then you should be dropping into your error setup everytime.
Last edited by Dark_Helmet; 12-12-2011 at 12:56 PM.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
That's because you've got two arguments; one way is to
Code:
asbman "training -query"
Note the quotes.
The other way is to adapt the following to use arguments:
Code:
#!/bin/ksh
#ident "$Id$"
#
# Name: $Source$
# Version: $Revision$
# Modified: $Date$
# Purpose: WHAT_IT_DOES_GOES_HERE
# Author: YOUR_NAME_GOES_HERE
# Date: TODAY'S_DATE_GOES_HERE
# $Log$
#
# define a fatal error function
function fatal
{
print -u2 "${1}"
exit 1
}
# initialize flags to null
aflag=
bflag=
# define usage message
USAGE="Usage:\t${0} [-a value] [-b value] args"
# process command line arguments
while getopts :?a:b: name
do
case ${name} in
a) aflag=1
aval="${OPTARG}";;
b) bflag=1
bval="${OPTARG}";;
:) print -u2 "${0}:\t${OPTARG} requires a value"
fatal "${USAGE}";;
\?) print -u2 "${0}:\tunknown option"
fatal "${USAGE}";;
esac
done
if [ ! -z "${aflag}" ]
then
print "Option -a ${aval} specified"
fi
if [ ! -z "${bflag}" ]
then
print "Option -b ${bval} specified"
fi
shift $((${OPTIND}-1))
if [ "$*" ]
then
print "Remaining arguments are:\t${*}"
fi
#
# This is where you put what you want to do
#
exit 0
You would save the above as asbman.sh and use the -a for "training" and the -b for "query" then execute using ${aval} -${bval}. And, of course, you can rename -a, -b, aval and bval to suit your own purposes.
Please note, this is a bash script. Again, I'm not a korn shell scripter, but you might be able to change the #!/bin/bash line to #!/bin/ksh and it will work under korn. I don't know that for sure though.
Code:
#!/bin/bash
if [ $# -ne 2 ] ; then
echo ""
echo "usage: asbman <env> <command>"
echo "e.g. asbman training query"
exit 0
fi
DBENV=${1}
USR_CMD=${2}
CAPS_DBENV=`echo ${DBENV} | tr '[a-z]' '[A-Z]'`
AdmSrvPort=`/usr/local/bin/cfgattr get ${CAPS_DBENV} AdmSrvPort`
PROPATH=/usr1/dlc
# I don't know how or where APPLIST is set. It's not assigned anywhere in the script. So it must be
# an environment variable. Otherwise, APPLIST will never have a value and the "Invalid environment
# name entered" will always display followed by the exit.
if [ -z "${APPLIST}" ] ; then
echo "Invalid environment name entered"
exit 1
fi
${PROPATH}/asbman -name ${CAPS_DBENV} -${USR_CMD} -port ${AdmSrvPort}
exit 0
EDIT:
By way of your example earlier, you would launch the script with:
Code:
./name_of_script training query
Please note the lack of the leading dash on "query" -- the dash is added as needed at the final command of the script.
Last edited by Dark_Helmet; 12-12-2011 at 02:38 PM.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
In the prototype KornShell program you don't need to use the command line options; they're there if you need them. What it does do is process any command line options if present then lets you work with the argument
Code:
prog -a something -b something argument
This prototype is useful for developing shell programs; e.g., you can add command line options if needed process any number of arguments and so on.
You would want to add your environment values at some point, either ahead of the command line options or after they've been processed. Here's a cut at your shell program using command line option:
Code:
#!/bin/ksh
#ident "$Id$"
#
# Name: $Source$
# Version: $Revision$
# Modified: $Date$
# Purpose: What does this program do
# Author: You name
# Date: Date created
# $Log$
#
# define a fatal error function
function fatal
{
print -u2 "${1}"
exit 1
}
# initialize flags to null
aflag=
bflag=
# define usage message
USAGE="Usage:\t${0} [-a value] [-b value] environment"
# process command line arguments
while getopts :?a:b: name
do
case ${name} in
a) aflag=1
aval="${OPTARG}";;
b) bflag=1
bval="${OPTARG}";;
:) print -u2 "${0}:\t${OPTARG} requires a value"
fatal "${USAGE}";;
\?) print -u2 "${0}:\tunknown option"
fatal "${USAGE}";;
esac
done
if [ -z "${aflag}" ]
then
print "Option -a ${aval} required"
fi
if [ ! -z "${bflag}" ]
then
print "Option -b ${bval} specified"
fi
#
# shift to get to the argument (environment name)
#
shift $((${OPTIND}-1))
# didn't get one?
if [ ! "${*}" ]
then
print -u2 "\n${0}:\tYou must specify an enviroment name\n"
fatal "${USAGE}"
fi
#
# this is the environment name
#
DBENV=${*}
#
# shift it to upper case
#
DBENV=$(echo ${DBENV} | tr '[a-z]' '[A-Z]')
#
# make sure it's valid; we need to add the command line option here
# i.e., what was entered with "-a what"
#
AdmSrvPort=$(/usr/local/bin/cfgattr get ${DBENV} -${aval} AdmSrvPort)
#
# set a PATH value
#
PROPATH=/usr1/dlc
#
# make sure it's valid
#
if [ -z "${APPLIST}" ]
then
print -u2 "\nInvalid environment name (${APPLIST}) entered"
fata "${USAGE}"
fi
#
# OK, everything looks all right, so execute
# (may need to put "-${aval}" here?)
#
${PROPATH}/asbman -name ${DBENV} -port ${AdmSrvPort}
exit 0
The prototype was switched around a little to accommodate a command line option, which would be "-a query".
I'd save this as asbman.sh then
Code:
make asbman
to create the executable -- the prototype includes CVS keywords so asbman.sh is "ready to be stored" in CVS and thence easy to edit and keep track of changes (if, of course, you use CVS for version control that is).
I developed the prototype some years ago (well, more than 15 years ago) when I was doing a great deal of software development and it got to be obvious that starting with a prototype would be a heck of a lot simpler than starting from scratch every time a new shell program was needed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.