0

I want to make a Pathogen helper script using a .sh file. I know if you make it executable it can be run as a command, but I have no idea how to do -o --options or arguments or anything like that.

Basically that's what I want answered, really all I need to know is how to do something like:

pathogen install git://...

Or something along those lines. Any help is appreciated. :)

greduan
  • 4,770
  • 6
  • 45
  • 73
  • 2
    [Obligatory link.](http://tldp.org/LDP/abs/html/) Your question is too broad and any answer would probably be high above your level of expertise. I'd suggest you learn at least the basics of shell scripting or just do `$ git clone git://...` like every Git-lovin' Pathogen user. – romainl Sep 20 '13 at 20:38
  • @romainl I will check out that link, I was not aware of it even though I did Google for this stuff. Thanks! – greduan Sep 20 '13 at 20:54

2 Answers2

1

The bash builtin getopts does not handle long arg parsing mechanism as far as I know.

getopt(1) is the tool you are looking for.

Not a program entirely, but you'll get the idea

PARSED_OPTIONS=$(getopt -n "$0"  -o h123: --long "help,one,two,three:"  -- "$@")
while true;
do
  case "$1" in

    -h|--help)
      echo "usage $0 -h -1 -2 -3 or $0 --help --one --two --three"
     shift;;

    -1|--one)
      echo "One"
      shift;;

    --)
      shift
      break;;
  esac
done

Take a look at code example and explanation given here.

mtk
  • 13,221
  • 16
  • 72
  • 112
  • Thanks for your answer! I'll accept one of these whenever I figure it out. And I'll accept whichever was most helpful. :) – greduan Sep 20 '13 at 21:20
1

Passing arguments is the easiest of the two (see "What are special dollar sign shell variables?" on SO):

#!/bin/sh
echo "$#"; # total number of arguments
echo "$0"; # name of the shell script
echo "$1"; # first argument

Assuming the file is named "stuff" (sans an extension) and the result of running ./stuff hello world:

3
stuff
hello

To pass in single letter switches (w/ optional associated params), e.g. ./stuff -v -s hello you'll want to use getopts. See "How do you use getopts" on SO and this great tutorial. Here is an example:

#!/bin/sh
verbose=1
string=
while getopts ":vs:" OPT; do
    case "$OPT" in
        v) verbose=0;;
        s) string="$OPTARG";;
    esac;
done;
if verbose; then
    echo "verbose is on";
fi;
echo "$string";

The line having getopts coupled with while needs further explanation:

  • while - start the while loop, going through everything getopts returns back after it processes
  • getopts :vs: OPT; - the program getopts with 2 arguments :vs: and OPT
    • getopts - returns something while can iterate over
    • :vs: - the first argument, this describes what switches getopts will look for while it parses the shell line
      • : - the first colon takes getopts out of debug mode, omit this to make getopts verbose
      • v - find the switch -v, this will not have an argument after it, just a simple switch
      • s: - find the option -s with an argument after it
    • OPT - will store the character used (the name of the switch), e.g. "v" or "s"
  • OPTARG - the variable to load the value into during each of while's iterations. For v, $OPTARG will not have a value, but for s it will.

The colon : tells getopts to look for an argument after the switch. The only exception is if the sequence of characters starts with : then it toggles getopts in/out of debug/verbose mode. For example:

getopts :q:r:stu:v will take getopts out of debug mode, will tell it that switches q, r, and u will expects args, while s, t, and u won't. This would be applicable for something like: stuff -q hello -r world -s -t -u 123 -v

getopts tuv will only tell getopts to search for switches t, u and v with no arguments, e.g. stuff -t -u -v, and to be verbose

Community
  • 1
  • 1
zamnuts
  • 9,492
  • 3
  • 39
  • 46
  • Thanks for your answer! I'll accept one of these whenever I figure it out. And I'll accept whichever was most helpful. :) – greduan Sep 20 '13 at 21:20
  • no problem, don't forget to make your shell script executable `chmod +x stuff`, and you may want to consider using bash for your shebang `#!/bin/bash` – zamnuts Sep 20 '13 at 21:24