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