LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-12-2011, 01:09 PM   #1
exxoid
LQ Newbie
 
Registered: Mar 2009
Location: Vancouver
Posts: 8

Rep: Reputation: 0
need help with a shell script?


Not sure if this is the correct forum to post in, but I am trying to hack together a quick script.. here is what I have..

Code:
#!/bin/ksh

if [ $# -ne 1 ]; then
 echo ""
 echo "usage: asbman [env]"
 echo "i.e. asbman training -query"
 exit 0
fi

DBENV=$1
DBENV=`echo $DBENV | tr '[a-z]' '[A-Z]'`
AdmSrvPort=`/usr/local/bin/cfgattr get $DBENV AdmSrvPort`
PROPATH=/usr1/dlc

if [ -z "$APPLIST" ]; then
  echo "Invalid environment name entered"
  exit 1
fi

$PROPATH/asbman -name $DBENV -port $AdmSrvPort

exit 0
But when I call the script ./asbman training -query I get nothing.

What I would expect is the asbman to be called, but its not getting that far.. any idea why?
 
Old 12-12-2011, 01:53 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
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.
KSH(1) Man Page (from 1996)

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 01:56 PM.
 
Old 12-12-2011, 01:58 PM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
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.

Hope this helps some.
 
Old 12-12-2011, 02:43 PM   #4
exxoid
LQ Newbie
 
Registered: Mar 2009
Location: Vancouver
Posts: 8

Original Poster
Rep: Reputation: 0
I am not fully sure I understand how to adopt the 2nd script there, (new to shell scripting)... but this is ultimately what I was looking to do..

User calls script with:

asbman environment-name command

training can be = training, production, development, or testing
query can be = query, start, stop

In the script, I need to define the command as such..

DBENV=$1
DBENV=`echo $DBENV | tr '[a-z]' '[A-Z]'`
AdmSrvPort=`/usr/local/bin/cfgattr get $DBENV AdmSrvPort`

asbman -name [environment-name] -[command] -port $AdmSrvPort

If user enters asbman training query, the script will translate that into this command:

asbman -name training -query -port 20932

The port, it will get from cfgattr by taking in the -name value, and then getting the port for that env from cfgattr.

Does that make sense?

Any help would be appreciated.
 
Old 12-12-2011, 03:28 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
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 03:38 PM.
 
Old 12-13-2011, 09:51 AM   #6
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
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.

Hope this helps some.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script running in another shell script and then exit dreamervlk Linux - General 3 09-16-2011 07:40 AM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
Shell script calling shell script - List of all nikunjbadjatya Programming 7 04-13-2011 07:27 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration