0

I have parameters named "Country" and "City"

The usual method of running the script is Script.ksh "India" "Mumbai".. ..it will run ok.

But, My requirement is...I want to run this script as Script.ksh -Country "India" -City "Delhi"

Culd any one please get me out of this

Thanks in Advance...

2
  • is getopt available as a command on your system? Commented Aug 1, 2012 at 22:31
  • Could you please give one example...using this command? I have no idea on it. Commented Aug 3, 2012 at 20:45

1 Answer 1

1

A sample use:

#!/bin/sh

setopt() {
  if [ -n "$1" -a -n "$2" ]; then
    optname=opt_${1#--}
    optval="\"$2\""
    eval $optname="$optval"
    shift
    shift
    setopt "$@"
  fi
}

eval setopt $(getopt -a -l city:,country: -o "" -- "$@")

echo "City is ${opt_city}"
echo "Country is ${opt_country}"

You could use the same technique without getopt, but getopt has the added benefit of normalizing names and recognizing abbreviations (at least GNU getopt does).

$ ./opttest -city "New Delhi" -country India
City is New Delhi
Country is India

$ ./opttest -ci "New Delhi" -co India
City is New Delhi
Country is India
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.