<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Best Practices Q: framework/skeleton for CLI executable /bin/sh script in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/best-practices-q-framework-skeleton-for-cli-executable-bin-sh/m-p/4075351#M64949</link>
    <description>I am currently using&lt;BR /&gt;&lt;BR /&gt;set -- $(getopt ... $*)&lt;BR /&gt;...&lt;BR /&gt;while test "--" != "$1"&lt;BR /&gt;do&lt;BR /&gt;case "$1" in&lt;BR /&gt;...&lt;BR /&gt;esac&lt;BR /&gt;shift&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;as described by getopt(1)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;This is good enough for most of my executables,&lt;BR /&gt;but it has two significant limitations.&lt;BR /&gt;&lt;BR /&gt;1. It fails to properly handle quoted spaces&lt;BR /&gt; in CLI option arguments.&lt;BR /&gt;2. It does not read from a CLI named&lt;BR /&gt; configuration file.&lt;BR /&gt;&lt;BR /&gt;Does anyone have a sample&lt;BR /&gt; (or pointer to sample) that will properly&lt;BR /&gt; handle spaces in&lt;BR /&gt; /bin/sh CLI option arguments?&lt;BR /&gt;&lt;BR /&gt;foo -a 'this has space'&lt;BR /&gt;&lt;BR /&gt;I have tried using $@,&lt;BR /&gt;but so far without success.&lt;BR /&gt;I suspect an example would&lt;BR /&gt; provide the missing details.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Does anyone have a sample&lt;BR /&gt; (or pointer to sample) that&lt;BR /&gt; will read arguments from&lt;BR /&gt; a file argument to -f option,&lt;BR /&gt; before processing the rest of the options?&lt;BR /&gt;&lt;BR /&gt;foo -f configfile -a 'override'&lt;BR /&gt;foo -a 'override' -f configfile&lt;BR /&gt;&lt;BR /&gt;I have tried parsing once with getopts,&lt;BR /&gt;scanning the arguments for&lt;BR /&gt; a configuration file,&lt;BR /&gt;reading the configuration file, and&lt;BR /&gt;scanning the arguments as indicated by&lt;BR /&gt; getopt(1)&lt;BR /&gt;&lt;BR /&gt;set -- $(getopts ... $*)&lt;BR /&gt;for f in $*&lt;BR /&gt;do&lt;BR /&gt;case "$f" in&lt;BR /&gt;...&lt;BR /&gt;-f) CONF=...&lt;BR /&gt;...&lt;BR /&gt;esac&lt;BR /&gt;done&lt;BR /&gt;if test -r "$CONF"&lt;BR /&gt;then&lt;BR /&gt;read-conf-file&lt;BR /&gt;fi&lt;BR /&gt;while test "--" != "$1"&lt;BR /&gt;do&lt;BR /&gt;case "$1" in&lt;BR /&gt;...&lt;BR /&gt;esac&lt;BR /&gt;...&lt;BR /&gt;shift&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;The limitations of the above include&lt;BR /&gt;1. option argument spaces are still mishandled&lt;BR /&gt;2. reading the conf file is currently&lt;BR /&gt; inelegant at best&lt;BR /&gt; and ad hoc at worst&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Does anyone have a simple&lt;BR /&gt; (or pointer to sample)&lt;BR /&gt; that parses a config file parallel&lt;BR /&gt; to CLI options?&lt;BR /&gt;&lt;BR /&gt;Ideally, I would like something&lt;BR /&gt; with results are equivalent to&lt;BR /&gt;1. read option and possible argument&lt;BR /&gt; from each conf file line&lt;BR /&gt;2. prepend the options and arguments&lt;BR /&gt; to the argument list&lt;BR /&gt;3. rerun getopt&lt;BR /&gt;so that there would be no difference between&lt;BR /&gt; whether an option was supplied&lt;BR /&gt; on the command line or in the conf file.&lt;BR /&gt;&lt;BR /&gt;TIA,&lt;BR /&gt;</description>
    <pubDate>Mon, 24 Sep 2007 20:16:44 GMT</pubDate>
    <dc:creator>drb_1</dc:creator>
    <dc:date>2007-09-24T20:16:44Z</dc:date>
    <item>
      <title>Best Practices Q: framework/skeleton for CLI executable /bin/sh script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/best-practices-q-framework-skeleton-for-cli-executable-bin-sh/m-p/4075351#M64949</link>
      <description>I am currently using&lt;BR /&gt;&lt;BR /&gt;set -- $(getopt ... $*)&lt;BR /&gt;...&lt;BR /&gt;while test "--" != "$1"&lt;BR /&gt;do&lt;BR /&gt;case "$1" in&lt;BR /&gt;...&lt;BR /&gt;esac&lt;BR /&gt;shift&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;as described by getopt(1)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;This is good enough for most of my executables,&lt;BR /&gt;but it has two significant limitations.&lt;BR /&gt;&lt;BR /&gt;1. It fails to properly handle quoted spaces&lt;BR /&gt; in CLI option arguments.&lt;BR /&gt;2. It does not read from a CLI named&lt;BR /&gt; configuration file.&lt;BR /&gt;&lt;BR /&gt;Does anyone have a sample&lt;BR /&gt; (or pointer to sample) that will properly&lt;BR /&gt; handle spaces in&lt;BR /&gt; /bin/sh CLI option arguments?&lt;BR /&gt;&lt;BR /&gt;foo -a 'this has space'&lt;BR /&gt;&lt;BR /&gt;I have tried using $@,&lt;BR /&gt;but so far without success.&lt;BR /&gt;I suspect an example would&lt;BR /&gt; provide the missing details.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Does anyone have a sample&lt;BR /&gt; (or pointer to sample) that&lt;BR /&gt; will read arguments from&lt;BR /&gt; a file argument to -f option,&lt;BR /&gt; before processing the rest of the options?&lt;BR /&gt;&lt;BR /&gt;foo -f configfile -a 'override'&lt;BR /&gt;foo -a 'override' -f configfile&lt;BR /&gt;&lt;BR /&gt;I have tried parsing once with getopts,&lt;BR /&gt;scanning the arguments for&lt;BR /&gt; a configuration file,&lt;BR /&gt;reading the configuration file, and&lt;BR /&gt;scanning the arguments as indicated by&lt;BR /&gt; getopt(1)&lt;BR /&gt;&lt;BR /&gt;set -- $(getopts ... $*)&lt;BR /&gt;for f in $*&lt;BR /&gt;do&lt;BR /&gt;case "$f" in&lt;BR /&gt;...&lt;BR /&gt;-f) CONF=...&lt;BR /&gt;...&lt;BR /&gt;esac&lt;BR /&gt;done&lt;BR /&gt;if test -r "$CONF"&lt;BR /&gt;then&lt;BR /&gt;read-conf-file&lt;BR /&gt;fi&lt;BR /&gt;while test "--" != "$1"&lt;BR /&gt;do&lt;BR /&gt;case "$1" in&lt;BR /&gt;...&lt;BR /&gt;esac&lt;BR /&gt;...&lt;BR /&gt;shift&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;The limitations of the above include&lt;BR /&gt;1. option argument spaces are still mishandled&lt;BR /&gt;2. reading the conf file is currently&lt;BR /&gt; inelegant at best&lt;BR /&gt; and ad hoc at worst&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Does anyone have a simple&lt;BR /&gt; (or pointer to sample)&lt;BR /&gt; that parses a config file parallel&lt;BR /&gt; to CLI options?&lt;BR /&gt;&lt;BR /&gt;Ideally, I would like something&lt;BR /&gt; with results are equivalent to&lt;BR /&gt;1. read option and possible argument&lt;BR /&gt; from each conf file line&lt;BR /&gt;2. prepend the options and arguments&lt;BR /&gt; to the argument list&lt;BR /&gt;3. rerun getopt&lt;BR /&gt;so that there would be no difference between&lt;BR /&gt; whether an option was supplied&lt;BR /&gt; on the command line or in the conf file.&lt;BR /&gt;&lt;BR /&gt;TIA,&lt;BR /&gt;</description>
      <pubDate>Mon, 24 Sep 2007 20:16:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/best-practices-q-framework-skeleton-for-cli-executable-bin-sh/m-p/4075351#M64949</guid>
      <dc:creator>drb_1</dc:creator>
      <dc:date>2007-09-24T20:16:44Z</dc:date>
    </item>
    <item>
      <title>Re: Best Practices Q: framework/skeleton for CLI executable /bin/sh script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/best-practices-q-framework-skeleton-for-cli-executable-bin-sh/m-p/4075352#M64950</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;1) Option scanning&lt;BR /&gt;I suggest NOT to use getopt but getopts - its a builtin in pdksh, zsh and - I think - bash, as well - look into 'man getopts'.&lt;BR /&gt;Usage for an example call:&lt;BR /&gt;-v   just set a flag&lt;BR /&gt;-o file   option with a parameter&lt;BR /&gt;-c str    option with a parameter containing spaces &lt;BR /&gt;cmd -o file -v -c 'more than one param' arg1 arg2&lt;BR /&gt;&lt;BR /&gt;verbose=n&lt;BR /&gt;while getopts :vo:c:h c&lt;BR /&gt;do&lt;BR /&gt;  case $c in&lt;BR /&gt;  v) verbose=y ;;&lt;BR /&gt;  h) echo usage; exit 0;;&lt;BR /&gt;  o) ofile=$OPTARG ;;&lt;BR /&gt;  c) comm="$OPTARG" ;;&lt;BR /&gt;  ?) echo usage; exit 1;;&lt;BR /&gt;  esac&lt;BR /&gt;done&lt;BR /&gt;shift $((OPTIND-1))&lt;BR /&gt;par1=$1&lt;BR /&gt;par2=$2&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;2) Configfile parsing:&lt;BR /&gt;First you should decide, what is your strategy, when you get an option in a config file AND as an option argument.&lt;BR /&gt;I recommend, that such an additional + duplicate option has priority.&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;mfG Peter&lt;BR /&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Tue, 25 Sep 2007 16:05:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/best-practices-q-framework-skeleton-for-cli-executable-bin-sh/m-p/4075352#M64950</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2007-09-25T16:05:57Z</dc:date>
    </item>
  </channel>
</rss>

