LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-15-2013, 04:50 AM   #1
Springs
Member
 
Registered: Apr 2008
Posts: 73

Rep: Reputation: 0
calling a script with options with spaces?


Hi All,

I'm using a "case" statement in a bash script which will let me select options which call particular functions.

My Question is that sometimes i need to pass it options with spaces which at the moment i'm wrapping "" around the words so they accept the spaces.

Is there another way to do this?

The way i call the whole script is:

./script OPTION1 OPTION2 OPTION3 etc..

Thanks
 
Old 04-15-2013, 05:29 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
What do you mean by options with spaces?

Are you talking about multiple, individual options separated by a space (i.e.: --verbose --all) or an option that has a second part (i.e: --outfile file).

Although you can probably do this with a case statement (which might need the shift command, depending on your needs) I would suggest using bash's getopts

Here are a few links that might help:
- Small getopts tutorial
- Bash: parsing arguments with ‘getopts’
- Getopt and getopts
 
Old 04-15-2013, 05:31 AM   #3
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Double quotes around your variable is the right way to do this.
Code:
case "$banana" in
thing) do_1;;
otherthing) do_2;;
*) do_3;;
esac
 
Old 04-15-2013, 05:53 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
So an example form of it is:
Code:
#!/bin/bash

CONFIG_OPT_A=false
CONFIG_OPT_B=false
CONFIG_OPT_W=false
CONFIG_OPT_W_ARG=''

while [[ $# -ge 1 ]]; do
    case "$1" in
    -a|--a-long-option)
        CONFIG_OPT_A=true
        ;;
    -b)
        CONFIG_OPT_B=true
        ;;
    -w|--with-arg)
        CONFIG_OPT_W=true
        if [[ -z $2 || <OPTIONALLY_TEST_$2_WITH_EXPRESSIONS> ]]; then
            echo "Invalid argument to option $1: $2"
            exit 1
        fi
        CONFIG_OPT_W_ARG=$2
        shift
        ;;
    *)
        echo "Unrecognized option: $1"
        exit 1
        ;;
    esac         
    shift
done

...

exit 0
 
Old 04-15-2013, 06:16 AM   #5
Springs
Member
 
Registered: Apr 2008
Posts: 73

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by linosaurusroot View Post
Double quotes around your variable is the right way to do this.
Code:
case "$banana" in
thing) do_1;;
otherthing) do_2;;
*) do_3;;
esac
my script already has this setup with the "" around the variable but luckily that part is only single words.

The variable i pass to it afterwards will be the ones that have spaces.

Here is what i currently have to do:

./script variable1 "variable 2" "variable 3"

and what i would like to know if its possible to be able to call variable 2 and 3 etc without having to wrap them with "" each time.
 
Old 04-15-2013, 07:50 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
@Springs It somehow confuses me what do you mean about a variable having a space. Do you mean the contents of the variable would have space, or the name of the variable itself would? Would you mind posting your script perhaps so that it would be clearer for us?
 
Old 04-19-2013, 09:31 AM   #7
linuxguru1
LQ Newbie
 
Registered: Apr 2013
Location: Maryland
Distribution: RHEL
Posts: 7

Rep: Reputation: Disabled
I think druuna is correct. If you are supplying arguments to a command on the command line your best bet is to use getopt(s). You must quote the arguments if there are spaces. Otherwise the shell will interpret spaces as a delimeter.

With that said, you could build your own version of getopt(s) with a case statement and 'shift' accordingly.

For example,
./script variable1 "variable 2" "variable 3"

case "$banana" in
variable1) # Do something ;;
variable) if [ $variable = 2 ] ; then # Do something ; fi
if [ $variable = 3 ] ; then # Do something ; fi ;;
*) ...
esac

Last edited by linuxguru1; 04-19-2013 at 12:23 PM. Reason: Forgot to add shift in the case statement. You will need it to get the next command line option.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a shell script present on another server using perl script. anandg111 Linux - Server 4 07-18-2012 12:23 AM
[SOLVED] calling bash script with more than one variable from python script mfb Programming 4 10-12-2010 06:51 AM
Calling perl script and passing variable from php script hosea Programming 5 10-21-2008 08:01 AM
how to pass a variable from a called script back to the calling script steven.c.banks Linux - General 2 05-05-2008 02:00 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration