Some getopts help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Some getopts help needed
# 1  
Old 04-11-2011
Some getopts help needed

Hi all, so look, I know Im a newb and before you look at my question and get upset I want you to understand I have spent the last 7 days to get THIS far. Ive been reading up on getopts so much I even started coming up with an acronym for it!! Here's my question...hope ya can help.

I have a script that allows me to enter different options to do various things. I can check for the existence of a file and check to see if a user is logged on. What I want to do is add an -n option to invert everything so I can see when a user is logged off or when the file is not there. Is there an easy way Im missing??

Code:
mailopt=FALSE
interval=5


while getopts mnf:t: option
do
case "$option"
in
m) mailopt=TRUE;;
t) interval=$OPTARG;;
f) if [ -f "$OPTARG" ]
then
echo "File exits"
else
echo "File doesnt exist"
fi
exit 1;;
\?) echo "Usage: mon [-m] [-t n] [-f] user"
    echo "      -m means to be informed by mail"
    echo "      -t means check every n secs"
    echo "  -f means check for existence fo a file"
    exit 1;;
esac
done

if [ "$OPTIND" -gt "$#" ]
then
echo "Missing user name!"
exit 2
fi

shiftcount=$((OPTIND - 1))
shift $shiftcount
user=$1

until who | grep "^$user " > /dev/null
do
sleep $interval
done

term=$(who | grep $user | cut -d' ' -f3)

if [ "$mailopt" = FALSE ]
then
echo "$user has logged on $user has logged on to terminal $term"
else
runner=$(who am i | cut -c1-7)
echo "$user has logged on to terminal $term" | mail $runner
fi

# 2  
Old 04-12-2011
probably best to set INVERT to 0 (and 1 when -n passed)
Set FILE to $OPTARG from -f argument (or blank when no -f)

Then you can:
Code:
   while true
   do
        if [ -n "$FILE" ]
        then
             test -f "$FILE"
        else
            who | grep "^$user " > /dev/null
        fi
        [ $? -eq $INVERT ] && break
        sleep $interval
   done

# 3  
Old 04-12-2011
I dont get where exactly you are doing this. Especially the while true. While what is true? Im a little lost as to the origin of where you are putting this while loop or what I should eb replacing with it.

If you can clarify that would be awesome!! Smilie
# 4  
Old 04-12-2011
Here's how I would tackle this (note ksh shell).
1. do not do any processing the getopts loop.
2. define variables for every flag. $fFLAG, for example.
3. your logic to invert is odd. If your checking not for a filename and it is not there then you have a success. I would simply check for a file and print a message ether way.

See if this makes sense. It's not coplete but should give you some ideas...

Code:
##################################################################################
# Process options
#
#  add flags to this list
#  if the flag requires a parameter, follow it by a :
##################################################################################
[[ $VERBOSE -ge $TRUE ]] && print_status_message "STEP: Processing getopts..." STEP

while getopts ":f:hn#u:" OPT                            # While there is a command line option
do
   case $OPT in
      f)   fFLAG=$TRUE
           FILENAME=$OPTARG
           ;;

      h)   print_options
           ;;

      n)   nFLAG=$TRUE
           ;;

      u)   uFLAG=$TRUE
           USERNAME=$OPTARG
           ;;

      ?)   print_status_message "ERROR: Unknown flag encountered." ERROR
           print_options
           exit 2
           ;;

   esac
done                                            # end -

###################################################################################
# Main line here
###################################################################################
if [[ $fFLAG -eq $TRUE && $nFLAG -eq $FALSE ]]
then
   # check for filename
   if [[ -e $FILENAME ]]
   then
      print "Found $FILENAME."
      RETCODE=0

   else
      print "WARNING: $FILENAME was not found."
      RETCODE=-1

   fi

elif [[ $fFLAG -eq $TRUE && $nFLAG -eq $TRUE ]]
then
   # check NOT for filename
   if [[ ! -e $FILENAME ]]
   then
      print "$FILENAME was not found."
      RETCODE=0

   else
      print "WARNING: Found $FILENAME."
      RETCODE=-1

   fi

fi

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

? used in getopts

Suppose I have a code below . while getopts a: opt do case $opt in a) app_name="$OPTARG";; *) echo "$opt is an invalid option"; exit 1;; ?) echo "The value of $OPTARG is an invalid option"; exit 1;; esac done Could anyone please tell me in which case my... (1 Reply)
Discussion started by: maitree
1 Replies

4. 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

5. 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

6. Shell Programming and Scripting

Help in getopts

Hi, My script will take a input file as a parameter(which is not mandatory) and also an option. ksh my_script.sh <inputfile> The option -n I have given is no way related to the input file. Now the problem here is when i execute the script specifying the input file and the option(the way... (4 Replies)
Discussion started by: chella
4 Replies

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

getopts

I have a script that facillitates NDM (Connect::\Direct) transfer to remote hosts. This script uses getopts to parse through the parameters passed to it and to set appropriate variables based upon what was passed in. Kickoff="mv $PATH/$FILE1 $PATH/$FILE2" ndm_shell.ksh -p $Node -s $Source -d... (3 Replies)
Discussion started by: google
3 Replies
Login or Register to Ask a Question