- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Best Practices Q: framework/skeleton for CLI execu...
Operating System - Linux
1752780
Members
6428
Online
108789
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2007 01:16 PM
09-24-2007 01:16 PM
Best Practices Q: framework/skeleton for CLI executable /bin/sh script
I am currently using
set -- $(getopt ... $*)
...
while test "--" != "$1"
do
case "$1" in
...
esac
shift
done
as described by getopt(1)
This is good enough for most of my executables,
but it has two significant limitations.
1. It fails to properly handle quoted spaces
in CLI option arguments.
2. It does not read from a CLI named
configuration file.
Does anyone have a sample
(or pointer to sample) that will properly
handle spaces in
/bin/sh CLI option arguments?
foo -a 'this has space'
I have tried using $@,
but so far without success.
I suspect an example would
provide the missing details.
Does anyone have a sample
(or pointer to sample) that
will read arguments from
a file argument to -f option,
before processing the rest of the options?
foo -f configfile -a 'override'
foo -a 'override' -f configfile
I have tried parsing once with getopts,
scanning the arguments for
a configuration file,
reading the configuration file, and
scanning the arguments as indicated by
getopt(1)
set -- $(getopts ... $*)
for f in $*
do
case "$f" in
...
-f) CONF=...
...
esac
done
if test -r "$CONF"
then
read-conf-file
fi
while test "--" != "$1"
do
case "$1" in
...
esac
...
shift
done
The limitations of the above include
1. option argument spaces are still mishandled
2. reading the conf file is currently
inelegant at best
and ad hoc at worst
Does anyone have a simple
(or pointer to sample)
that parses a config file parallel
to CLI options?
Ideally, I would like something
with results are equivalent to
1. read option and possible argument
from each conf file line
2. prepend the options and arguments
to the argument list
3. rerun getopt
so that there would be no difference between
whether an option was supplied
on the command line or in the conf file.
TIA,
set -- $(getopt ... $*)
...
while test "--" != "$1"
do
case "$1" in
...
esac
shift
done
as described by getopt(1)
This is good enough for most of my executables,
but it has two significant limitations.
1. It fails to properly handle quoted spaces
in CLI option arguments.
2. It does not read from a CLI named
configuration file.
Does anyone have a sample
(or pointer to sample) that will properly
handle spaces in
/bin/sh CLI option arguments?
foo -a 'this has space'
I have tried using $@,
but so far without success.
I suspect an example would
provide the missing details.
Does anyone have a sample
(or pointer to sample) that
will read arguments from
a file argument to -f option,
before processing the rest of the options?
foo -f configfile -a 'override'
foo -a 'override' -f configfile
I have tried parsing once with getopts,
scanning the arguments for
a configuration file,
reading the configuration file, and
scanning the arguments as indicated by
getopt(1)
set -- $(getopts ... $*)
for f in $*
do
case "$f" in
...
-f) CONF=...
...
esac
done
if test -r "$CONF"
then
read-conf-file
fi
while test "--" != "$1"
do
case "$1" in
...
esac
...
shift
done
The limitations of the above include
1. option argument spaces are still mishandled
2. reading the conf file is currently
inelegant at best
and ad hoc at worst
Does anyone have a simple
(or pointer to sample)
that parses a config file parallel
to CLI options?
Ideally, I would like something
with results are equivalent to
1. read option and possible argument
from each conf file line
2. prepend the options and arguments
to the argument list
3. rerun getopt
so that there would be no difference between
whether an option was supplied
on the command line or in the conf file.
TIA,
1 REPLY 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 09:05 AM
09-25-2007 09:05 AM
Re: Best Practices Q: framework/skeleton for CLI executable /bin/sh script
Hi,
1) Option scanning
I suggest NOT to use getopt but getopts - its a builtin in pdksh, zsh and - I think - bash, as well - look into 'man getopts'.
Usage for an example call:
-v just set a flag
-o file option with a parameter
-c str option with a parameter containing spaces
cmd -o file -v -c 'more than one param' arg1 arg2
verbose=n
while getopts :vo:c:h c
do
case $c in
v) verbose=y ;;
h) echo usage; exit 0;;
o) ofile=$OPTARG ;;
c) comm="$OPTARG" ;;
?) echo usage; exit 1;;
esac
done
shift $((OPTIND-1))
par1=$1
par2=$2
...
2) Configfile parsing:
First you should decide, what is your strategy, when you get an option in a config file AND as an option argument.
I recommend, that such an additional + duplicate option has priority.
Such way, you just use the above getopts-loop BUT - if '-f conf' found as option - store the values in the configfile into different variables and use these as (new) default values for unspecified options.
mfG Peter
mfG Peter
1) Option scanning
I suggest NOT to use getopt but getopts - its a builtin in pdksh, zsh and - I think - bash, as well - look into 'man getopts'.
Usage for an example call:
-v just set a flag
-o file option with a parameter
-c str option with a parameter containing spaces
cmd -o file -v -c 'more than one param' arg1 arg2
verbose=n
while getopts :vo:c:h c
do
case $c in
v) verbose=y ;;
h) echo usage; exit 0;;
o) ofile=$OPTARG ;;
c) comm="$OPTARG" ;;
?) echo usage; exit 1;;
esac
done
shift $((OPTIND-1))
par1=$1
par2=$2
...
2) Configfile parsing:
First you should decide, what is your strategy, when you get an option in a config file AND as an option argument.
I recommend, that such an additional + duplicate option has priority.
Such way, you just use the above getopts-loop BUT - if '-f conf' found as option - store the values in the configfile into different variables and use these as (new) default values for unspecified options.
mfG Peter
mfG Peter
The Universe is a pretty big place,
it's bigger than anything anyone has ever dreamed of before.
So if it's just us, seems like an awful waste of space, right?
Jodie Foster in "Contact"
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP