Issue with getopts


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Issue with getopts
# 1  
Old 11-18-2009
Issue with getopts

Hi,

I am using below code to check files on ftp server and notify users if files are not arrived. I am invoking another script to send mail using smtpmail. Mail is being sent properly in first call to child script notify.sh ( i.e. in if condition ) but in second call ( i.e. in else part ) mail is being sent with out subject or with same subject as in first call.

Code:
ftpcnt=1
maxftpcnt=9
chkfile=0

while [ $ftpcnt -lt $maxftpcnt ] 
do
if [ ${chkfile} = 0 ]
   then sleep 1
           if [ $ftpcnt -eq 4 ]
              then let ftpcnt=$ftpcnt+1 
              . notify.sh -m /dev/null -s "File Not Found in FTP Server - Hourly Message"
          elif [ $ftpcnt -eq 8 ]
              then 

              . notify.sh  -m /dev/null -s "File Not Found in FTP Server : After 2 Hours"
              exit 1
           else
             let ftpcnt=$ftpcnt+1

           fi
fi

done


#**************
#notify.sh
#********************

sender_mail_id='sender@abc.com'
host_name='hostname'

MAILLIST="xyz@abc.com"

message_body=''
subject=''

while getopts :f:h:m:s: option
do
                case $option in
                                s)            subject="$OPTARG";;
                                m)          message_body="$OPTARG";;
                                f)             sender_mail_id="$OPTARG";;
                                h)            host_name="$OPTARG";;
                                
                                
                esac
done

if [ -z "$message_body" ]; then
                message_body=/dev/null
fi

if [ -z "$subject" ]; then
                subject="empty"
fi

smtpmail -f "$sender_mail_id" -h "$host_name" -m "$message_body" -s "$subject" $MAILLIST

# 2  
Old 11-18-2009
I think it is because you are sourcing rather than calling notify.sh, i.e.:
Code:
. notify.sh

vs.
Code:
./notify.sh

# 3  
Old 11-18-2009
Please observe space between . and notify.sh , this is to invoke notify.sh in current shell itself.
# 4  
Old 11-18-2009
I am aware of that. This is called sourcing.
# 5  
Old 11-18-2009
yes, you are correct , I modified code to invoke as another shell and it's working fine.
It there any way to run the script in current shell and get the code working?
# 6  
Old 11-18-2009
You could create a function and call that. Pay attention to using getopts inside a function since you would have to call
Code:
OPTIND=1

to reset the index of the next arg.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using getopts

Hi. Can somebody please show me an example of how to use getopts to assign a variable if it's been passed into the script but to set a default if no value has been passed in? And also how to handle a param with multiple values ... so a sub parse (can I use a function for this?)? Here's my code... (1 Reply)
Discussion started by: user052009
1 Replies

2. UNIX for Dummies Questions & Answers

Getopts

while getopts v OPTION do case $OPTION in v) echo "Hello" ;; *) exit 1;; esac done Suppose I have script tmp.sh Whose Signature is tmp.sh <fixed_argument> When I run the script with tmp.sh -v "file", it echoes a hello but, when I try the other way i.e, tmp.sh... (1 Reply)
Discussion started by: Devendra Hupri
1 Replies

3. Shell Programming and Scripting

Using getopts. Need help

Hi all... I have been looking on here for the past few days for an answer and Im gonna have to break down and ask. I just learned about the getopts command last week so have been trying to utilize it in my scripts. Below, I am trying to set up a case structure for options using getopts.... (1 Reply)
Discussion started by: losingit
1 Replies

4. Shell Programming and Scripting

getopts issue

Hi, I have a script, that accepts two positional parameters. Now, I want to add one more optional parameter to it. But I do not want it to be an optional positional parameter as this will impact the future enhancement to the script. Hence I want to use getopts. The script is now executed as... (2 Replies)
Discussion started by: sudvishw
2 Replies

5. UNIX for Dummies Questions & Answers

Getopts

Hey, i need help with the use of getopts in shell script. tried reading a lot online, but found incomplete examples (maybe complete but cudn't make out). PLzz help...explain in deatil plzzz, i am a newbie:confused: (3 Replies)
Discussion started by: SasankaBITS
3 Replies

6. Shell Programming and Scripting

using getopts

Hi, I have a program where I want to use getopts. I want to use "-i" option and then optionally supply arguments. If user dosent supply arguments, then also it should work. Please tell me how to proceed. Here is some code, this is not right code btw but a sample to understand what I want to... (1 Reply)
Discussion started by: som.nitk
1 Replies

7. HP-UX

using getopts

Is there a restriction on levels of using 'getopts' ? I have several scripts, each of which requires an option as the first parameter . If I call one prg separately it works fine, but when one prg calls another prg and passes the option on the called prg, then the called prg seems not to process... (3 Replies)
Discussion started by: vslewis
3 Replies

8. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies

9. Shell Programming and Scripting

help in getopts

hey need help with getopts again. i am using getopts to read my command line options and arguments. i can manage to do for options that have only one argument e.g srcipt_name -f 3 i am able to use getopts to do this but i am having problems two accept more than two agruments e.g.... (1 Reply)
Discussion started by: problems
1 Replies

10. Shell Programming and Scripting

getopts

I have a script which fires a command based on certain parameters. I am posting the code below.. The options needs be given such that -u option goes along with -d and -s, -f goes with -d and -t goes with -s and -d. 1) How do I ensure that user misses any of the option then he should be... (5 Replies)
Discussion started by: yerra
5 Replies
Login or Register to Ask a Question