Hello,

I am trying to write a shell script which will have the following:

1. arguments like:
-h, -- help, -v, --version, -V, --Verbose, -o filename, --output=filename
-s searchphrase, --search=searchphrase, -f format, --format=format

2. a default usage where a user runs a search phrase where the output should go to standard output or stdout.

I have very less experience in shell scripting and I am still learning,

so was wondering if anybody here could help me out or examples as to how I could accomplish this......

Thanks

Will it be possible to do this using getopt() function?

Anybody with any suggestions?

It would be really appreciated:)

while getopts s:f:vVh:A opt
do
  case $opt in
    A) echo Autor:test; exit;;
    v) echo version 1.0; exit;;
    s) searchphrase=$OPTARG ;;
    -*) echo invalid option >&2; exit 1 ;;
   esac
done
shift $(( $OPTIND - 1 ))

Right well got the serachphrase working,,,,,now the issue is
I want the searchphrase to be contained in double quotes if it contains spaces or have each space preceded by a backslash.

Any help with this????

... Right well got the serachphrase working,,,,,now the issue is
I want the searchphrase to be contained in double quotes if it contains spaces or have each space preceded by a backslash.

Any help with this????

In shell programming, ensuring that arguments are properly quoted is an exercise left to the user who must either escape spaces in the search phrase or put quotes around the search phrase.

However, you could program your case in getopts to examine the remaining args, if any:

while loop; do
  if no more args, the phrase is complete, break out of the loop
  if the next arg is "-*", the phrase is complete, break out of the loop
  if next arg is not "-*", add the arg to the phrase and shift once
done

This should work so long as you don't have any non-option args.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.