LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2012, 03:35 PM   #1
khandu
Member
 
Registered: Sep 2003
Posts: 93

Rep: Reputation: 0
Getopts script not working in redhat sh (works in sunos sh)


I have a script with works perfectly on SunOS machines with sh..

Here is a part of it which is not working on RedHat sh

config.sh
Code:
#!/bin/sh
..............
..............
# main

PATH=/usr/bin:/bin
export PATH

$HOSTNAME=`hostname`

while getopts t:a:D:w:r:h:p:i: name
do
  case $name in
    t)  TYPE=$OPTARG;if [ "$TYPE" != "abc" -o "$TYPE" != "xy" ]
                        then
                        usage
                        elif [ "$TYPE" = "abc" ]
                        then
                            PORT=3546
                            PATH="/srv/temp/$HOST_NAME"
                        elif [ "$TYPE" = "xy" ]
                        then
                                PORT=7854
                                PATH="/srv/temp/$HOST_NAME-xy"
                        fi;;
    a)  INSTTYPE=$OPTARG;;
    D)  DN=$OPTARG;;
    w)  PASSWORD=$OPTARG;;
    r)  MAN=$OPTARG;;
    h)  HOST_NAME=$OPTARG;;
    p)  PORT=$OPTARG;;
    i)  PATH=$OPTARG;;
    ?)  usage
        ;;
  esac
done
usage function is defined elsewhere and works fine

Now when I call the program as

Code:
./config.sh -t abc -a all .......
the -a does not get registered and nor does -p and -i

When I do

Code:
./config.sh -a all -t abc -p 5478 -i /srv/asd/casd .......
the value of -a gets registered in $TYPE but then the values of -i and -p does not get stored..

this script is working fine on a SunOS machine.. but not on RHEL

What is going wrong here? I want the script to run as

Quote:
./config.sh -t xxx -a xxx .... (t first)
In SunOS the

Quote:

[ "$TYPE" != "abc" -o "$TYPE" != "xy" ]
is actually

Quote:
[ $TYPE -ne abc -o $TYPE -ne xy ]
This has been changed in RHEL to avoide some errors i was getting

Last edited by khandu; 02-15-2012 at 05:21 PM.
 
Old 02-16-2012, 08:27 AM   #2
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: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
I've found that enclosing an ${OPTARG} is double quotes is useful; e.g.,
Code:
aval="${OPTARG}"
For what it's worth here is the Korn Shell template I use for shell programs. Other than the "print" command it ought to work in the other shells (dunno, don't use BASH).
Code:
#!/bin/ksh
#ident  "$Id$"
#
#       Name:           $Source$
#       Version:        $Revision$
#       Modified:       $Date$
#       Purpose:        What does it do
#       Author:         You 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
exit 0
Hope this helps some.
 
  


Reply

Tags
sh



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
getopts script m4rtin Programming 12 12-14-2009 08:47 AM
password less authentication from SunOS to HP-UX not working xxx_anuj_xxx Solaris / OpenSolaris 4 07-29-2009 11:10 AM
How can i install Redhat 8 in SunOS machine petero Linux - Software 5 12-22-2005 01:58 PM
cronjob not working but script works dtra Linux - Newbie 4 02-28-2005 05:05 PM
Script works on Redhat but not on Slack Kocil Slackware 3 07-18-2003 12:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:43 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
Open Source Consulting | Domain Registration