getopts Questions - am confused


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts Questions - am confused
# 1  
Old 10-05-2011
getopts Questions - am confused

Hi all,

Below is an excerpt from my Korn shell script and I just have some questions about using getopts and its behavior.

Code:
 
while getopts kmg size_unit
do
   case $size_unit in
      k) size="KB"
         header="Filesystem KBytes Used Avail Capacity Mount"
         echo "${header}" > ${tmp_header}
         ;;
      m) size="MB"
         header="Filesystem MBytes Used Avail Capacity Mount"
         echo "${header}" > ${tmp_header}
         ;;
      g) size="GB"
         header="Filesystem GBytes Used Avail Capacity Mount"
         echo "${header}" > ${tmp_header}
         ;;
      *) size="GB"
         header="Filesystem GBytes Used Avail Capacity Mount"
         echo "${header}" > ${tmp_header}
         ;;
   esac
done
 
if [[ -z "${size}" ]] ; then
   size="GB"
   header="Filesystem GBytes Used Avail Capacity Mount"
   echo "${header}" > ${tmp_header}
fi
echo "!!!size=${size}"
echo "!!!size_unit=${size_unit}"
exit 0

Output from the test run of the script as below:

Code:
 
aklss901(oracle)[]/nas_mnt/common/oracle/aklss901$: /tmp/x.ksh -k
!!!size=KB
!!!size_unit=?
aklss901(oracle)[]/nas_mnt/common/oracle/aklss901$: /tmp/x.ksh -g
!!!size=GB
!!!size_unit=?
aklss901(oracle)[]/nas_mnt/common/oracle/aklss901$: /tmp/x.ksh -m
!!!size=MB
!!!size_unit=?
aklss901(oracle)[]/nas_mnt/common/oracle/aklss901$: /tmp/x.ksh
!!!size=GB
!!!size_unit=?
aklss901(oracle)[]/nas_mnt/common/oracle/aklss901$: /tmp/x.ksh -y
/tmp/x.ksh[28]: getopts: y bad option(s)
!!!size=GB
!!!size_unit=?

Questions:

- Can I suppress the error/warning /tmp/x.ksh[28]: getopts: y bad option(s)? I managed to do so doing while getopts kmg size_unit 2>/dev/null, is that the only way to do so?

Also, doesn't the following lines supposed to be executed if I run the script with the wrong command line option, i.e. /tmp/x.ksh -y, or if I run the script without any command line option, i.e. /tmp/x.ksh?

I have the if-then clause because the *) size="GB" line does not work like I expect it to. I've also tried ?) size="GB" or \?) size="GB" and it does not make any difference.

Code:
      *) size="GB"
         header="Filesystem GBytes Used Avail Capacity Mount"
         echo "${header}" > ${tmp_header}
         ;;
   esac
done
 
if [[ -z "${size}" ]] ; then
   size="GB"
   header="Filesystem GBytes Used Avail Capacity Mount"
   echo "${header}" > ${tmp_header}
fi

Feedback will be much appreciated. Thanks in advance.
# 2  
Old 10-05-2011
Quote:
- Can I suppress the error/warning /tmp/x.ksh[28]: getopts: y bad option(s)? I managed to do so doing while getopts kmg size_unit 2>/dev/null, is that the only way to do so?
No, just put a colon in front of the option string: :kmg.

You need to check explicitly for no arguments.
I don't understand the last question, I get size=GB when I run your script with -y ...

Last edited by radoulov; 10-06-2011 at 11:37 AM.. Reason: Corrected: semicolon -> colon.
# 3  
Old 10-15-2011
Quote:
Originally Posted by radoulov
No, just put a colon in front of the option string: :kmg.

You need to check explicitly for no arguments.
I don't understand the last question, I get size=GB when I run your script with -y ...

Hi radoulov,

Thanks for your response.

I did what you suggested so now the getopts line looks like

Code:
while getopts :kmg size_unit

Now when I run /tmp/x.ksh -y, it runs the default that I want it to which is this one below here, i.e. size=GB is if I supplied an invalid option that is NOT k, m or g. However when I run /tmp/x.ksh without a command line option, it does not default to size=GB. Does that mean I have to do a separate check for when I do not specify a command line argument?

Code:
...
...
      *) size="GB"
         header="Filesystem GBytes Used Avail Capacity Mount"
         echo "${header}" > ${tmp_header}
         ;;
   esac
done

Also, why is size_unit showing ?, I thought it is supposed to parse to k,m or g but it is showing the ? character instead in all cases? WHY? Smilie-

Last edited by newbie_01; 10-15-2011 at 10:55 AM..
# 4  
Old 10-15-2011
Quote:
Originally Posted by newbie_01
However when I run /tmp/x.ksh without a command line option, it does not default to size=GB. Does that mean I have to do a separate check for when I do not specify a command line argument?
Yes, as I already said you need to add code to handle that case.
Hint:

Code:
[ "$#" -gt 0 ] || ..

or, if you want to handle empty parameters:

Code:
[ -z "$1" ] && ...

Quote:
Code:
...
...
      *) size="GB"
         header="Filesystem GBytes Used Avail Capacity Mount"
         echo "${header}" > ${tmp_header}
         ;;
   esac
done

Also, why is size_unit showing ?, I thought it is supposed to parse to k,m or g but it is showing the ? character instead in all cases? WHY? Smilie-
Because:

Code:
When the end of options is encountered, getopts exits with a return value greater than zero.  OPTIND is
              set to the index of the first non-option argument, and name is set to ?.

# 5  
Old 10-15-2011
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confused with if/then

Hi All, I'm pretty new to this so please bear with me... I'm trying to write a bash script to first search in a file for a string of characters; if the characters exist than skip the rest of the code until you get to the last line and run that command /sbdin/ldconfig; if the string doesn't... (2 Replies)
Discussion started by: gmdune
2 Replies

2. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

3. Ubuntu

New and Confused

Hello, I am having a problem with Dual Booting Windows XP Pro and Linux Mint. I have Three Hard Drives, One Hard Drive has Linux Mint Loaded on it. When it is hooked up to the computer by itself it works great. This is an IDE Drive. The Second Hard Drive has Window XP Pro loaded on it.... (3 Replies)
Discussion started by: Forextrading
3 Replies

4. Solaris

Confused c#t#d#s#

I am confused c#t#d#s# once I learn the following : slice 0 ...... 0 to 2520 slice 1....... 2521 to 2840 slice 6........2841 to 8891 slice 2........0 to 8891 really really confused. Please explain. (8 Replies)
Discussion started by: deltakutty
8 Replies

5. UNIX for Dummies Questions & Answers

Confused with the permission

Hi, One of the directory in my folder has the following persion : drwxr-sr-x Can any one explain this permission and how can his occur ? This folder is created with Win SCP. Any folder created with win SCP is being set to this permission only. (1 Reply)
Discussion started by: risshanth
1 Replies

6. UNIX for Dummies Questions & Answers

confused

A red hat linux ftp server exists in which a file exists. My problem is I need to connect to this server from my windows xp terminal which is in the same network & retrieve the file then convert it to xcel for some data Pls advs commands and procedure to connect to the machine...oh my god... (1 Reply)
Discussion started by: sauravjung
1 Replies

7. Shell Programming and Scripting

confused with cp

may i know what cp $1 $2 $0 $2 does? (12 Replies)
Discussion started by: C|[anti-trust]
12 Replies

8. UNIX for Dummies Questions & Answers

confused

hi! how when i'm chattin inside com there was this chatter andi don't know what he did but he saw all my files inside my shell. what did he do? (4 Replies)
Discussion started by: hapiworm
4 Replies

9. UNIX for Dummies Questions & Answers

confused,,,,

Hi,,, is there any possibility to install Linux in my P.C which is use Win98 without loose anything from my hard disk???? ---------------------------------------------------------------------------------- Is it better for a newbie in this kind of OS to install Linux instead of... (5 Replies)
Discussion started by: spyros
5 Replies
Login or Register to Ask a Question