getopts: bad option(s)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts: bad option(s)
# 1  
Old 08-19-2008
getopts: bad option(s)

Hi,

I have a script that ran perfectly on Solaris 5.8
However after upgrade to Solaris 5.10 it started failing.
I invoke the script as below:
./TestScript3.ksh --dir $APP_DATA_IN_OLD $NDM_DATA/$NEXT_FILE
When i execute it i get the following error "getopts: dir bad option(s)".
Please let me know what has changed between Solaris 5.8 & Solaris 5.10 to cause this & how it can be fixed. I am a Newbie to Scripts.
The body of TestScript3.ksh is given below:

Code:
#!/bin/ksh

USAGE="
Usage:  TestScript2 ( --about | --help | --usage | --version )
        TestScript2 [ --bzip ] [ -b|--bzip2] [ -c|--cycle cycles ] [ -d|--date ]
                   [ --dir dir ] [ -f|--force ] [ -g|--gzip ] [ -p|--pack ]
                   [ -s|--silent ] [ -t|--touch ] [ -z|--compress ]
                   file [file ...]
"
HELP="
Options:
        --about         Display information about the program.
        --help          Displays this message.
        --usage         Displays the program's usage.
        --version       Displays the program's version.
        --bzip
                Compress the cycled files with bzip.
        -b, --bzip2
                Compress the cycled files with bzip2.
        -c, --cycle cycles
                Keep 'cycles' number of cycled files. (Default is 3).
        -d, --date
                Append -YYYYMMDDHHMMSS to the cycled files where YYYY is the
                current year; MM is the current month; DD is the current day;
                HH is the current hour; MM is the current minute; and SS is the
                current second.
        --dir dir
                Specify the name of the directory for the cycled files to be
                shifted into.
        -f, --force
                Cycle a file even if it is zero bytes in length.
        -g, --gzip
                Compress the cycled files with gzip.
        -p, --pack
                Compress the cycled files with pack.
        -s, --silent
                Do not display warnings or informational messages.
                Errors will still be displayed.
        -t, --touch
                Create an empty file to replace the cycled file.
        -z, --compress
                Compress the cycled files with compress.
        file
                This is the name of the file to cycle.
"
# Define a restrictive PATH.
#
 export PATH=/usr/xpg4/bin:/usr/bin:/usr/ucb:/usr/local/bin
#export PATH=/bin
# Function to display information about this program.
#
about()
{
        echo
        echo "Copyright (c) 1998 Jim Barber. All rights reserved."
        [[ $change_id != $(eval echo "@(#) \\\$Id\: %\M%,v %\I% %\E% %\U% \\\$" | sed 's/\\//g') ]] && echo "$change_id"
        echo "$ABOUT"
        exit 2
}
# Function to display some help about the program.
#
help()
{
        echo "$USAGE$HELP"
        exit 2
}
# Function to display the usage of this program.
#
usage()
{
        exec >&2
        echo "$USAGE"
        exit 2
}
# Function to display the version of the program.
#
version()
{
        echo
        if [[ $change_id = $(eval echo "@(#) \\\$Id\: %\M%,v %\I% %\E% %\U% \\\$" | sed 's/\\//g') ]]
        then
                echo "cycle_file version 1.1"
        else
                echo $change_id | awk '{printf("cycle_file version %s\n", $4)}'
        fi
        echo
        exit 2
}
# Function to get the argument passed to a long option.
#
# This can't be used if it is valid for the parameter to the long option to
# have a leading '-' (except negative numbers, which are handled correctly).
#
# Example of usage:
#
#       VAR=$(get_long_optarg $OPTARG $OPTIND "$@") || exit ; shift
#
get_long_optarg()
{
        OPTARG=$1
        OPTIND=$2
        shift 2
        if [[ $(eval echo \$$OPTIND) = @(-*) && $(eval echo \$$OPTIND) != @(-)+([0-9])?(.)*([0-9]) || -z $(eval echo \$$OPTIND) ]]
        then
                echo "@basename@: --$OPTARG argument expected" >&2
                usage
        fi
        eval echo \$$OPTIND
}
# Function to convert Access Control Lists to a value suitable for passing to
# the chmod command.
#
acl_to_chmod()
{
        U=$(expr "$1" : '.\(...\)......')
        G=$(expr "$1" : '....\(...\)...')
        O=$(expr "$1" : '.......\(...\)')
        echo "u=$U,g=$G,o=$O" | sed 's/-//g; s/s/sx/g; s/S/s/g; s/t/tx/g; s/T/t/g'
}
# Initialise some parameters.
#
unset COMPRESS DATE DIR FORCE SILENT TOUCH
CYCLES=3
# Parse the command line parameters.
#
OPTIND=1
while getopts 'bc:dfgpstz-:' PARAMS
do
        case "$PARAMS" in
                b) COMPRESS='bzip2 -9' ;;
                c) CYCLES=$OPTARG ;;
                d) DATE=$(date "+%Y%m%d%H%M%S") ;;
                f) FORCE=true ;;
                g) COMPRESS='gzip -9' ;;
                p) COMPRESS=pack ;;
                s) SILENT=true ;;
                t) TOUCH=true ;;
                z) COMPRESS=compress ;;
                -)
                case "$OPTARG" in
                        about)          about ;;
                        help)           help ;;
                        usage)          usage ;;
                        version)        version ;;
                        bzip)           COMPRESS=bzip ;;
                        bzip2)          COMPRESS='bzip2 -9' ;;
                        compress)       COMPRESS=compress ;;
                        cycle)          CYCLES=$(get_long_optarg $OPTARG $OPTIND "$@") || exit ; shift ;;
                        date)           DATE=$(date "+%Y%m%d%H%M%S") ;;
                        dir)            DIR=$(get_long_optarg $OPTARG $OPTIND "$@") || exit ; shift ;;
                        force)          FORCE=true ;;
                        gzip)           COMPRESS='gzip -9' ;;
                        pack)           COMPRESS=pack ;;
                        silent)         SILENT=true ;;
                        touch)          TOUCH=true ;;
                        *)
                        echo "Illegal option --$OPTARG" >&2
                        usage ;;
                esac ;;
                \?)     usage ;;
        esac
done
shift $((OPTIND - 1))
echo $DIR
# Make sure that at least one file name was passed.
#
(( $# < 1 )) && usage
# Make sure that the directory for the log files to to be cycled to exists.
#
if [[ -n $DIR && ! -d $DIR ]]
then
        echo "Error: '$DIR' is not a valid directory."
        exit 1
fi
echo $@
for REAL_FILE in "$@"
do
        # Only do the cycle if the file exists.
        #
        if [[ ! -f $REAL_FILE ]]
        then
                [[ -z $SILENT ]] && echo "File '$REAL_FILE' does not exist. Skipping..." >&2
                RET_CODE=1
                continue
        else
        echo "File exists"
        fi
done
# Set a return code for the program.
#
RET_CODE=0
exit $RET_CODE

# 2  
Old 08-19-2008
please use the "code" tags if you post code!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh - default value for getopts option's argument

Hello everyone, I need help in understanding the default value for getopts option's argument in ksh. I've written a short test script: #!/bin/ksh usage(){ printf "Usage: -v and -m are mandatory\n\n" } while getopts ":v#m:" opt; do case $opt in v) version="$OPTARG";; ... (1 Reply)
Discussion started by: da1
1 Replies

2. Shell Programming and Scripting

Getopts option -please help

Hello, I am using below code in AIX env to interpret -n option given in argument while executing the script .I want to give another argument -t #!/bin/sh #set -x while getopts ":n:" opt; do case "$opt" in n) host=$OPTARG shift 2 ;; *)... (3 Replies)
Discussion started by: Vishal_dba
3 Replies

3. Shell Programming and Scripting

ksh "getopts" -- Unsetting an Option

I use the "getopts" ksh built-in to handle command-line options, and I'm looking for a clean/standard way to "unset" an option on the command line. I don't know if this is a technical question about getopts or more of a style/standards question. Anyway, I understand that getopts processes its... (4 Replies)
Discussion started by: Matt Miller
4 Replies

4. Shell Programming and Scripting

How to execute default in getopts when no option is given ?

hi, here is a ksh script i wrote using getopts... i want to find out how i can run it in default mode when no option is mentioned and no arguments are provided... ? i.e if the script name is final1, then just running final1 should run in default mode.... while getopts 1:2:3:4: mode ... (1 Reply)
Discussion started by: pravsripad
1 Replies

5. Shell Programming and Scripting

getopts fails to error on option w/o dash

I have a script with several options and during testing I found that the \? option does not handle options without dashes as I would expect. Then I run the script with any option that does not include a dash, it runs the script when I would expect \? to catch it and error. I've tried this with... (2 Replies)
Discussion started by: HexKnot
2 Replies

6. Shell Programming and Scripting

bad option(s) with awk

Hi All I am trying to split a string which is like below -p/usr/home/dfusr/adm/props/Comments.properties_-iPEL17 i want to split "_" as delimiter. Please find the code below #!/bin/ksh cd /usr/home/dfusr/backup... (6 Replies)
Discussion started by: rajeshorpu
6 Replies

7. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

8. Shell Programming and Scripting

getopts with repeat of same option

Hello, Does getopts have some way of handling the use of an option that requires a parameter more than once on the command line. e.g. mycmd -a john -a jane I came up with a solution using arrays (shown below), but wonder if getopts has some other way of handling it. Other solutions... (2 Replies)
Discussion started by: CarlosNC
2 Replies

9. Shell Programming and Scripting

getopts with non-option arguments?

Hello everyone, Is it possible to use getopts and also receive arguments without option flags? e.g. myscript arg1 arg2 -a arg3 -b arg4 If so, how do you stop getopts from exiting as soon as it detects the non-option arguments? (2 Replies)
Discussion started by: kdelok
2 Replies

10. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies
Login or Register to Ask a Question