0

I am writing a script which can choose a file and print specific content. For example,

san#./script.sh

Expected Usage : ./script.sh --file1 --dns

(Here it checks for file1, search for dns name and prints. Basically there are sub-parameters under a parameter)

I tried for single parameter/Option as below :

options=$@

arguments=($options)

index=0;
for argument in $options
do
    index=`expr $index + 1`;
    case $argument in
    -a | --fun1 ) run_function1 ;;
    -b | --fun2 ) run_function2 ;;
    -c | --fun3 ) run_function3 ;;
    esac
done
exit;

[ ${1} ] || helpinfo

Can any one suggest for double parameter(sub options) ?

Expected target options :

./script.sh


OPTIONS : ./script.sh -h

./script --fun1 stackoverflow
        microsoft
        Google
     --fun2 Yahoo 

Basically each function will look into one file. I have looked into getopt or getopts, But it doesn't have long option (--long is not possible, instead we can use only -l). But again not sure of sub parameters. Can any one help on this ?

San
  • 223
  • 1
  • 6
  • 15

2 Answers2

1

I'm not sure I understand you properly ... but let's try my luck :)

$ cat a.sh
#!/bin/bash

function fun1 {
   echo "fun1 '$1'"
}

function fun2 {
   echo "fun2 '$1'"
}

function err {
   echo "No function has been specified"
   exit 1
}

FUNCTION=err
while [ $# -gt 0 ]; do
   case "$1" in
      -a | --fun1 ) FUNCTION=fun1 ;;
      -b | --fun2 ) FUNCTION=fun2 ;;
      *) $FUNCTION "$1" ;;
   esac
   shift
done

$ ./a.sh --fun1 one two -b three
fun1 'one'
fun1 'two'
fun2 'three'
Neuron
  • 363
  • 2
  • 6
0

There is a detailed FAQ dealing with parameter parsing (including what you call "suboptions"): http://mywiki.wooledge.org/BashFAQ/035

Sir Athos
  • 9,403
  • 2
  • 22
  • 23
  • I don't find much details to implement in my scenario. Can you give me a sample code from the above details ? Thanks! – San May 04 '13 at 05:07