07-25-2019
Quote:
Originally Posted by
mohtashims
I was looking for the solution proposed by @Corona688. However, if the last arguments '-c' does not have any value associated which is fine; the script fails and displays the helpFunction. I want -c to be optional with or without a value. So, '-c' or '-c<args>' both should work.
I don't think the builtin
getopts allows for optional arguments, unfortunately. If you are on a GNU system and have access to the external GNU version of
getopt you can have optional arguments to one or more of your options, but I believe
getopt is considered deprecated. Its usage is also inconsistent with that of
getopts.
Andrew
10 More Discussions You Might Find Interesting
1. Programming
Hi,
while compiling my c++ program, I get the following Message:
/usr/include/iso/math_iso.h", line 84: Error: No direct declarator preceding "("
Does anybody know this?
Thanks
Nik (1 Reply)
Discussion started by: nik
1 Replies
2. Shell Programming and Scripting
Hi,
I am looking for a way to find a particular word in a file then print a line that precedes this line, as well as this line.
Sometimes in a log file there is only one word per line and I need to print one of the lines leading up to the single worded line.
Example - I can grep for ouch... (5 Replies)
Discussion started by: g_jumpin
5 Replies
3. Shell Programming and Scripting
Ok
Suppose I have a log file like the below:
2010-07-15 00:00:01,410 DEBUG
2010-07-15 00:01:01,410 DEBUG
2010-07-15 00:01:02,410 DEBUG com.af ajfajfaf affafadfadfd dfa fdfadfdfadfadf
fafafdfadfdafadfdaffdaffadf
afdfdafdfdafafd error
error failure afdfadfdfdfdf
EBUDGG eafaferror failure... (6 Replies)
Discussion started by: SkySmart
6 Replies
4. Shell Programming and Scripting
Hi
Following is an example line.
echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:"
I want it's output to be
200
However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies
5. Shell Programming and Scripting
Hello,
I have an xml file whose contacts are like below:
<Node>Apple
<B>Value1</B>
<B>Value2</B>
<B>Value3</B>
</Node>
<Node>Mango
<B>Value1</B>
<B>Value2</B>
<B>Value3</B>
</Node>
<Node>Apple
<B>Value1</B>
<B>Value2</B>
<B>Value3</B>
</Node>
<Node>Bannana (3 Replies)
Discussion started by: umarsatti
3 Replies
6. Shell Programming and Scripting
Gurus:
How can I transpose the output below to a format in which I can plot a graph to show VSZ memory usage by PIDs?
stdout:
Tue Jan 22 07:29:19 CUT 2013
42336296 1841272 java wilyadm
21889232 438616 jlaunch sidadm
42532994 414336 jlaunch sidadm
Tue Jan 22 07:49:20 CUT 2013... (1 Reply)
Discussion started by: ux4me
1 Replies
7. Shell Programming and Scripting
Hi,
Is this possible? I want to take a csv file and find the closest value in another csv file preceding it and following it.
For ex. In this csv file, I'll take the first line:
1309341156.800000000
1309341156.802500000
1309341156.805000000
1309341156.807500000
and find the closest... (2 Replies)
Discussion started by: superbbrr
2 Replies
8. Linux
I have a file that looks like this,
P38112 Q12389; Q03532; P43586; Q08208; P37838; P40693; P38805
Q12389 P32892; P36049; Q03532; P43586; Q12176; P38112; P39744; P37838
P11154 P39940
P50094 P50095
P32892 Q12389; Q04660; P43586; P37838; P40693
Q05022 Q08235; Q12176; P39744
P40347 P32333... (2 Replies)
Discussion started by: Syeda Sumayya
2 Replies
9. Shell Programming and Scripting
Hi,
I have some data like below:
John 254
Chris 254
Matt 123
Abe 123
Raj 487
Moh 487
How can i print it using awk to have:
254 John,Chris
123 Matt,Abe
487 Raj,Moh
Thanks. (4 Replies)
Discussion started by: james2009
4 Replies
10. Shell Programming and Scripting
Hello.
Here is a file contents :
declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern
=I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '='
Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies
LEARN ABOUT DEBIAN
getopt_long
GETOPTS(3) libbash getopts Library Manual GETOPTS(3)
NAME
getopts -- libbash library for command line parameters parsing
SYNOPSIS
$retval getopt_long <Instructions> <Parameters>
DESCRIPTION
This is a documentation for libbash getopts library, that implements getopt_long function for bash(1). For documentation of bash getopts
function, please see getopts(1) ( getopts(1posix) on some systems).
Here is a table for reference:
getopts(1) (or 1posix on some systems) implemented by bash
getopts(3) implemented by libbash.
getopt(1) implemented by getopt utils (part of util-linux)
getopt_long(1) implemented by libbash and installed to section 1 instead of 3 to prevent collision with C man pages.
getopt(3) implemented by GNU C library.
getopt_long(3) implemented by GNU C library.
I have also seen separate getopt utility which part of util-linux package.
The getopt_long function parses the command line arguments. It uses Instructions as the rules for parsing the Parameters.
The Instructions
A string that specifies rules for parameters parsing. The instructions string is built of a group of independent instructions, separated by
a white space. Each instruction must have the following structure:
-<SingleLetter>|--<MultiLetter>-><VariableName>[:]
This structure contains three parts:
-<SingleLetter>
This is the parameter single-letter sign. For example -h.
--<MultiLetter>
This is the parameter's corresponding multi-letter sign. For example --help.
<VariableName>[:]
This is the name of the variable that will contain the parameter value. For example: HELP.
The Variable name can represent one of two variables types:
Flag variable (not followed by ':')
In this case, it will hold the value 1 if 'on' (i.e. was specified on command line) and will not be defined if 'off'.
Value variable (followed by ':')
In this case, the value it will hold is the string that was given as the next parameter in the Parameters string (Separated by
white-space or '=' ). If input contains more then one instance of the considered command line option, an array of the given
parameters will be set as the value of the variable.
The Parameters
The Parameters are simply the parameters you wish to parse.
RETURN VALUE
This function returns a string that contains a set of variables definitions. In order to define the variables, this string should be given
as a parameter to eval function. This value is returned in the variable $retval.
EXAMPLES
Parse command line parameters looking for the flags -h | --help and -v | --version and for the value -p | --path :
getopt_long '-h|--help->HELP
-v|--version->VERSION
-p|--path->PATH:' $*
eval $retval
In this example, for the parameters --help --path=/usr/ the variables that will be created are:
HELP=1
PATH=/usr/
for the parameters --help --path=/usr --path=/bin the variables that will be created are:
HELP=1
PATH=(/usr /bin)
BUGS
Values must not contain the string `__getopts__'. This string will be parsed as a single white-space.
A value should not start with an already defined multi-letter sign. If such a value exists, it will be treated as the equivalent singe-letter
sign. This bug only accures when using a single-letter sign, or a multi-letter sign that are not followed by a `='.
For example: If we have a script named `foo', and we parse the parameters `-d|--dir:' and `-f|--file:', then
foo -d --file
and
foo --dir --file
will not work
foo --dir=--file
will work.
AUTHORS
Hai Zaar <haizaar@haizaar.com>
Gil Ran <gil@ran4.net>
SEE ALSO
ldbash(1), getopt_long(1), getopts(1), getopt(1), libbash(1), getopt(3), getopt_long(3)
Linux Epoch Linux