ksh "getopts" -- Unsetting an Option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh "getopts" -- Unsetting an Option
# 1  
Old 08-07-2013
ksh "getopts" -- Unsetting an Option

I use the "getopts" ksh built-in to handle command-line options, and I'm looking for a clean/standard way to "unset" an option on the command line. I don't know if this is a technical question about getopts or more of a style/standards question. Anyway, I understand that getopts processes its args (by default $*) from left to right, and I'm hoping to use that to allow a second specification of an option to "turn off" an option.

For options that take an option argument, this isn't a problem. In my getopts processing case statement I just say, for the "-x" option, for example:
Code:
x ) x_arg=$OPTARG
    ;;

and the last instance of "-x" on the command line wins, which is what I want.

The problem comes in with options that don't take an option argument. If the option is just a switch/flag then my case statement would just have something like:
Code:
x ) x_specified=1
    ;;

Now, the second time "-x" is seen on the command line the same thing just happens, and "x_specified=1" is executed again. What's the best/easiest/simplest/standardest way to "unset x_specified"? That's my question.

I had hoped to invoke the getopts functionality that allows either "+" or "-" to precede option letters, but it seems there's no way to tell within the getopts processing loop whether "+" or "-" was used. If there were a way to tell then I could adopt that standard that "+" means to turn off the setting. The other thing I could do is something like:
Code:
x ) if [[ -z $x_specified ]] ; then
        x_specified=1
    else
        unset x_specified
    fi
    ;;

Now the "-x" option would be a toggle; each time it's seen on the command line the setting changes. However, I'd like a way on the command line to say, "turn off the -x setting," and not just "toggle the -x setting." I could also pick some other letter, e.g. "X", as the way to turn it off, but many of my scripts already use both the upper case and lower case versions of the same letter to mean different things, and if I have to pick some other random letter to mean "turn off -x" then that's not very mnemonic, and my scripts are hard enough to learn how to use already.

Finally, you may say, "If you want -x off on the command line, why are you turning it on in the first place?" Option processing in my scripts goes through a multi-step process. Command-line options are checked, and also a config file is checked for options, and there's a precedence between the two. This all works fine for options that take an option argument, but the scheme falls apart for options that don't.
# 2  
Old 08-07-2013
it most likely will make it easier for people to help you if you could post ...

1. the script or portions of the script that is having the issue as well as
2. how the script is called with arguments
3. os and version
4. error messages as printed to your screen
# 3  
Old 08-08-2013
Quote:
Originally Posted by Just Ice
it most likely will make it easier for people to help you if you could post ...

1. the script or portions of the script that is having the issue as well as
2. how the script is called with arguments
3. os and version
4. error messages as printed to your screen
I think I did supply most of the info you mention. I posted script snippets and explanations of how the scripts are used. There are no error messages, and OS and version are, I think, irrelevant, except maybe to note that I'm using ksh93 and not ksh88.

My question is about how to use getopts in any ksh script to overcome the issues I discuss. There is not a single, particular script that's involved, or a particular bug that needs to be fixed. I realize the original post is not short, but I think the discussion is concise.
# 4  
Old 08-08-2013
Should work on recent shells:
Code:
x ) x_specified=$((1-x_specified))

# 5  
Old 08-08-2013
Quote:
Originally Posted by Matt Miller
I had hoped to invoke the getopts functionality that allows either "+" or "-" to precede option letters, but it seems there's no way to tell within the getopts processing loop whether "+" or "-" was used.
That is not true. If getopts is configured to allow "+" to precede option letters, then the opt parameter that comes back during the processing loop has a "+" sign in front of it. So, I just hadn't read the manual, and the functionality I needed was there all along.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

ksh - default value for getopts option's argument

Hello everyone, I need help in understanding the default value for getopts option's argument in ksh. I've written a short test script: #!/bin/ksh usage(){ printf "Usage: -v and -m are mandatory\n\n" } while getopts ":v#m:" opt; do case $opt in v) version="$OPTARG";; ... (1 Reply)
Discussion started by: da1
1 Replies

3. UNIX for Dummies Questions & Answers

What is the meaning of "-s" option in "if" statement?

Hi Guys, I'm sorry but I can't find answer for this, what is the meaning of -s option in "if" statement on unix scipting. Please see sample below: opath=/home/output for i in N1 N2 N3 N4 do echo $i if then grep $i $opath/N5_CRAI > $opath/N5_$i.crai chmod 777 $opath/N5_$i.crai ... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

4. Shell Programming and Scripting

Purpose of "read" and "$END$" in ksh ?

Hi, Could anyone please shed some light on the following script lines and what is it doing as it was written by an ex-administrator? cat $AMS/version|read a b verno d DBVer=$(/usr/bin/printf "%7s" $verno) I checked that the cat $AMS/version command returns following output: ... (10 Replies)
Discussion started by: dbadmin100
10 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

"#!/bin/ksh -f" What does the -f option do?

What does "-f" option do? This is at the start of a shell scripts to point to full path to the interpreter such as /bin/ksh What does the -f option do? #!/bin/ksh -f (3 Replies)
Discussion started by: Arsenalman
3 Replies

7. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

8. HP-UX

script running with "ksh" dumping core but not with "sh"

Hi, I have small script written in korn shell. When it is called from different script, its dumping core, but no core dump when we run it standalone. And its not dumping core if we run the script using "/bin/sh" instead of "ksh" Can some body please help me how to resolve this issue. ... (9 Replies)
Discussion started by: simhe02
9 Replies

9. Shell Programming and Scripting

getopts and "priority level" for args

Hi, I use getopts in this way:while getopts ":d:f:crapv" Option do case $Option in d ) BACKUP_DIR="$OPTARG";echo $BACKUP_DIR;; #echo fot test c ) compress_file;; r ) remove_file;; a ) remove_file && compress_file;;... (2 Replies)
Discussion started by: mbarberis
2 Replies
Login or Register to Ask a Question