954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Bash Shell Scripting

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

rEhSi_123
Junior Poster
159 posts since Feb 2009
Reputation Points: 18
Solved Threads: 0
 

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

rEhSi_123
Junior Poster
159 posts since Feb 2009
Reputation Points: 18
Solved Threads: 0
 

Well it's where I would start looking, if that's any use to you.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Anybody with any suggestions?

It would be really appreciated:)

rEhSi_123
Junior Poster
159 posts since Feb 2009
Reputation Points: 18
Solved Threads: 0
 
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????

rEhSi_123
Junior Poster
159 posts since Feb 2009
Reputation Points: 18
Solved Threads: 0
 

You seem to be managing at the moment.
Keep digging away at it :)

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

... 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.

Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You