<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="https://tanguy.ortolo.eu/blog/feed/rss/commentaires/" />
	<link>https://tanguy.ortolo.eu/blog/article150/shell-process-arguments</link>
	<language>en</language>
	<description>a blog about Debian and self-hosting</description>
	<lastBuildDate>Thu, 09 Jun 2016 08:36:00 +0000</lastBuildDate>
	<generator>PluXml</generator>
	<item>
		<title>Process command line arguments in shell - Written by Tanguy @ thursday 09 june 2016, 08:36</title> 
		<link>https://tanguy.ortolo.eu/blog/article150/shell-process-arguments/#c1465461360-1</link>
		<guid>https://tanguy.ortolo.eu/blog/article150/shell-process-arguments/#c1465461360-1</guid>
		<description>@martin : Thank you for this example, you made me discover the column utility, and the possibility to use opening parentheses in case lists. I am not sure &quot;${NEXT:-}&quot; is any more useful than just &quot;$NEXT&quot; that should expand the same.

Anyway, this mode of operation is good when you know exactly what options you will get, but not when you do not know but only want to operate (modify, remove, insert…) on some known options while keeping the other ones unmodified.

Also, regarding the usage of getopt to process options with or without parameters, I personally use it this way, shifting the arguments instead of iterating over them, which removes the need for special handling of the next argument following an option that takes a parameter:


eval set -- &quot;$(getopt -o hw: --long help,wait: -- &quot;$@&quot;)&quot;
while [ &quot;$1&quot; != &quot;--&quot; ]
do
    case &quot;$1&quot;
    in
        -w|--wait) wait=&quot;$2&quot;; shift 2 ;;
        -h|--help) usage ; exit 0 ;;
    esac
done
shift
if [ $# -ne 1 ] ; then usage ; exit 1 ; fi
file=&quot;$1&quot;</description>
		<pubDate>Thu, 09 Jun 2016 08:36:00 +0000</pubDate>
		<dc:creator>Tanguy</dc:creator>
	</item>
	<item>
		<title>Process command line arguments in shell - Written by martin @ thursday 09 june 2016, 07:55</title> 
		<link>https://tanguy.ortolo.eu/blog/article150/shell-process-arguments/#c1465458927-1</link>
		<guid>https://tanguy.ortolo.eu/blog/article150/shell-process-arguments/#c1465458927-1</guid>
		<description>How about using getopt? Here&amp;#039;s something I wrote lately, which works rather well:

  usage() {
    echo &amp;quot;Usage: ${0##*/} [options] output-directory -- [git-annex filter]&amp;quot;
    cat &amp;lt;&amp;lt;-_eof | column -ts:
    --list, -l: output the list of files created
    --wipe, -w: wipe the target directory first
    --mode, -m: create copies, links, rellinks, or abslinks
    --get, -g: Obtain missing files from git-annex
    --visitor, -i: Include file for visitor pattern functions
    --help, -h: this message
    _eof
    echo
    echo &amp;quot;  The git-annex filter defaults to &amp;#039;$DEFAULT_FILTER&amp;#039;&amp;quot;
  }

  WIPE=0
  MODE=rellinks
  LIST=0
  GET=0
  OUTDIR=
  VISITOR=
  eval set -- $(getopt -o &amp;#039;lm:wgi:h&amp;#039; -l &amp;#039;list,mode:,wipe,get,visitor:,help&amp;#039; -n &amp;quot;${0##*/}&amp;quot; -s sh -- &amp;quot;$@&amp;quot;)
  for opt; do
    case &amp;quot;${NEXT:-}&amp;quot; in
      (&amp;#039;&amp;#039;)
      	case &amp;quot;$opt&amp;quot; in
          (--list|-l) LIST=1;;
          (--wipe|-w) WIPE=1;;
          (--mode|-m) NEXT=mode;;
          (--get|-g) GET=1;;
          (--visitor|-i) NEXT=visitor;;
          (--help|-h) usage; exit 0;;
          (--) NEXT=outdir;;
      	esac
      	;;
      (mode) MODE=&amp;quot;$opt&amp;quot;; unset NEXT;;
      (outdir) OUTDIR=&amp;quot;$opt&amp;quot;; NEXT=filter;;
      (filter) FILTER=&amp;quot;${FILTER:+$FILTER }$opt&amp;quot;;;
      (visitor) VISITOR=&amp;quot;$opt&amp;quot;; unset NEXT;;
    esac
  done</description>
		<pubDate>Thu, 09 Jun 2016 07:55:00 +0000</pubDate>
		<dc:creator>martin</dc:creator>
	</item>
	<item>
		<title>Process command line arguments in shell - Written by Tanguy @ wednesday 08 june 2016, 13:21</title> 
		<link>https://tanguy.ortolo.eu/blog/article150/shell-process-arguments/#c1465392060-1</link>
		<guid>https://tanguy.ortolo.eu/blog/article150/shell-process-arguments/#c1465392060-1</guid>
		<description>@Loïc Minier : You are welcome. Regarding your solution, rather than a while true with a break inside it, why not use a while [ $# -gt 0 ]?

Also, when one needs to do several such list processing, it can be useful to define functions, as each shell function has its own &quot;$@&quot;. I think I already used that at least once, not sure what for though. But then it becomes rather tricky anyway, so this is a point where one should start asking himself whether or not he should be using a regular programming language instead of shell scripting.</description>
		<pubDate>Wed, 08 Jun 2016 13:21:00 +0000</pubDate>
		<dc:creator>Tanguy</dc:creator>
	</item>
	<item>
		<title>Process command line arguments in shell - Written by Loïc Minier @ wednesday 08 june 2016, 12:37</title> 
		<link>https://tanguy.ortolo.eu/blog/article150/shell-process-arguments/#c1465389427-1</link>
		<guid>https://tanguy.ortolo.eu/blog/article150/shell-process-arguments/#c1465389427-1</guid>
		<description>Hey,

Alternatively, you may also escape args and exec them as follows:
escape() {
    echo &amp;quot;$*&amp;quot; | sed &amp;quot;s/&amp;#039;/&amp;#039;\&amp;quot;&amp;#039;\&amp;quot;&amp;#039;/g; s/.*/&amp;#039;&amp;amp;&amp;#039;/&amp;quot;
}

        command=&amp;quot;&amp;quot;
        while :; do
            command=&amp;quot;$command $(escape &amp;quot;$1&amp;quot;)&amp;quot;
            shift
            if [ $# -eq 0 ]; then
                break
            fi
        done
        break

eval $command

This basically serializes the args into a space separated array in a string using shell escape sequences.

However I like your solution much more as it doesn&amp;#039;t involve spawning external commands; I hadn&amp;#039;t thought of reusing the positional args in my earlier shell scripts, thanks for sharing  :-)

Cheers,
- Loïc</description>
		<pubDate>Wed, 08 Jun 2016 12:37:00 +0000</pubDate>
		<dc:creator>Loïc Minier</dc:creator>
	</item>
		<title>Tanguy Ortolo - Process command line arguments in shell - Comments</title> 
</channel>
</rss>