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 02-28-2012, 05:16 PM   #1
hmhoang04
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Rep: Reputation: Disabled
ksh script Migrationfrom Solaris to Linux; getopts: arguments changed since last call


I am new to Unix scripting and trying to migrate a ksh from Sun Solaris to Linux. I got error " getopts: arguments changed since last call" when I executed the below sript in Linux. The script works fine in Sun Solaris though. Please note that the script uses getops in two places: the main routine and also in the notify function.



The function is: notify()
{
BOPT=0; LOPT=0; MOPT=0; POPT=0;

while getopts :blmp OPTION "$@"
do
case $OPTION in
b) BOPT=1 ;;
l) LOPT=1 ;;
m) MOPT=1 ;;
p) POPT=1 ;;
\?) ;;
esac
done

shift OPTIND-1
MSG=$*

if [ LOPT -eq 1 ];
then
log "$MSG"
fi

if [ POPT -eq 1 ];
then
msg "$MSG"
fi

if [ MOPT -eq 1 ];
then
email "$MSG"
fi

if [ BOPT -eq 1 ];
then
beep "$MSG"
fi

return;
}


The calling statement in the main is as follows:

##############################################################################
#
# MAIN ROUTINE
#
##############################################################################

HOST=`uname -n`
PROG=`basename $0`
ME=`whoami`
DT=`date '+%y%m%d'`
DATE=`date "+%Y-%m-%d %H:%M:%S"`

NODATAONLY="ON"

RESULT=
EXIT0=0
EXIT1=1
EXIT2=2
EXIT3=3

if [ $# -eq 0 ]; then
usage
fi

while getopts ":u:s:b:lnd" OPTM "$@"
do
case "${OPTM}" in
u) USER=${OPTARG};;
s) SID=${OPTARG};;
p) USERPASSWD=${OPTARG};;
b) BPERSON=${OPTARG};;
l) LOGTOSCREEN="ON";;
d) DEBUG="ON";;
*) notify -p "**ERROR : Invalid option -${OPTARG}"
usage;;
esac
done

set_def_vars;
set_env;
notify -l "Starting ${PROG} for ${USER}@${SID} ......"
.
.
.
.
 
Old 02-28-2012, 06:29 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
Quote:
The shell does not reset OPTIND automatically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used.
.. this is from the bash man page, but I'm guessing it's probably the same as ksh in this case.
 
1 members found this post helpful.
Old 02-29-2012, 12:04 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Also add a shebang line to the top to ensure it uses your desired shell
Code:
#!/bin/ksh

# OR
#!/bin/bash
for RHEL based systems; your system may vary
 
1 members found this post helpful.
Old 03-07-2012, 08:58 AM   #4
hmhoang04
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hi kbp, chrism01,
I reset the OPTIND and it works. Many thanks for your help. I greatly appreciate it. Take care.
hmhoang04
 
  


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
getopts and mandatory arguments in BASH jmcejuela Programming 3 04-03-2009 05:00 PM
Porting ksh script from Solaris to Linux Gives unmatched if error markatharvestinfotec Programming 4 08-28-2008 02:51 PM
getopts - displaying warnings/errors when extraneous arguments are provided retrovertigo Programming 5 07-18-2007 04:44 PM
Passing command line arguments through getopts and long options neville310 Programming 3 04-16-2007 07:38 AM
Mandatory arguments with getopts? rose_bud4201 Programming 2 03-10-2005 03:18 PM


All times are GMT -5. The time now is 01:35 AM.

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