The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using getopts TurboArkhan Shell Programming and Scripting 3 06-22-2009 04:31 PM
getopts question k2k Shell Programming and Scripting 2 06-26-2008 09:05 PM
getopts question!!! andy2000 Shell Programming and Scripting 1 03-27-2007 12:15 AM
question about getopts ahtat99 Shell Programming and Scripting 2 08-20-2006 01:45 PM
help in getopts problems Shell Programming and Scripting 1 05-04-2006 11:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-05-2009
rookieuxixsa rookieuxixsa is offline
Registered User
  
 

Join Date: Dec 2008
Location: NYC
Posts: 8
getopts question

I am trying to set up prompts when you don't enter the right information or dont enter the information at all, when executing a script. Below is the question that i am asking and i am not sure how to set up the if statements to make sure that the user enters the name, cpu's, memory and ip. I was told that getopts with a cease statement might be usefull, but im not sure how to apply it. Any help would be greatly appreciated...

NAME=$1
NCPUS=$2
MEMORY=$3
IP=$4
MACHINENAME=`hostname`
MASTERHOST=svvnyc702

echo "Do you wish to proceed with the creation of UAT ZONE zone-${NAME}, CPU's ${NCPUS}, MEMORY ${MEMORY}, IP ${IP}
<y or n> ? \c"

read WISH
echo
if [ $WISH = "n" ] ; then
echo "You chose no, good bye"
exit
fi
if [ ! $WISH = "y" ] ; then
echo in vaild optin g - exiting
exit
fi
  #2 (permalink)  
Old 01-05-2009
tostay2003 tostay2003 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
here is how you do it,

a,b,c are the command line options

Code:
while
      getopts a:b:c: OPT 2>/dev/null
   do
      case $OPT in
         a)
            VarA=$OPTARG
         ;;
         b)
            VarB=$OPTARG
         ;;
         c) 
            VarC=$OPTARG
         ;;
         *)
            help #Call help function
         ;;
      esac
   done
  #3 (permalink)  
Old 01-05-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by rookieuxixsa View Post
I am trying to set up prompts when you don't enter the right information or dont enter the information at all, when executing a script. Below is the question that i am asking and i am not sure how to set up the if statements to make sure that the user enters the name, cpu's, memory and ip. I was told that getopts with a cease statement might be usefull, but im not sure how to apply it. Any help would be greatly appreciated...
Please put code inside [code] tags.
Quote:
Code:
NAME=$1
NCPUS=$2
MEMORY=$3
IP=$4
MACHINENAME=`hostname`
MASTERHOST=svvnyc702
 
echo "Do you wish to proceed with the creation of UAT ZONE zone-${NAME}, CPU's ${NCPUS}, MEMORY ${MEMORY}, IP ${IP}
<y or n> ? \c"
Code:
while [ -z "$name" ] ## I recommend using lowercase variable names
do
  printf "Enter NAME: "
  read name
done

while [ -z "$ncpus" ]
do
  printf "Enter number of CPUs: "
  read ncpus
done

while [ -z "$memory" ]
do
  printf "Enter memory: "
  read memory
done
Quote:
Code:
read WISH
echo
if [ $WISH = "n" ] ; then
  echo "You chose no, good bye"
  exit
fi
if [ ! $WISH = "y" ] ; then
  echo in vaild optin g - exiting
  exit
fi

Code:
read wish
case $wish in
   n) echo "You chose no, good bye"
      exit ;;
esac
  #4 (permalink)  
Old 01-06-2009
rookieuxixsa rookieuxixsa is offline
Registered User
  
 

Join Date: Dec 2008
Location: NYC
Posts: 8
thanx for the help...
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:06 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0