LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-07-2010, 08:05 PM   #1
supulton
LQ Newbie
 
Registered: Feb 2010
Posts: 21

Rep: Reputation: 0
case statement passing multiple arguments


Hello, I've started dabbling with the case statement in order to pass some option's arguments into variables.

I do not think I am doing this right.

Code:
usage() {
    echo "Usage: $0 [-z|--snooze] [-c|--channel] [-p|--playlist]
    [-m|--message] [-v|--mpcvolume]"
    exit 1
}

if [ $# -lt 1 ]; then
    echo "No arguments supplied"
    usage
    #ztime=10
    #chan='PCM'
    #playlist='alarm'
    #msg='Wake up, ya lazy bum!'
    #mpcvol=80
fi

while test -n "$@"; do
    case "$@" in
        --snooze "$2"|-z "$2")
            ztime="$2"
            shift
            ;;
        --channel "$2"|-c "$2")
            chan="$2"
            shift
            ;;
        --playlist "$2"|-p "$2")
            playlist="$2"
            shift
            ;;
        --message "$2"|-m "$2")
            msg='Wake up!'
            shift
            ;;
        --mpcvolume "$2"|-v "$2")
            mpcvol="$2"
            shift
            ;;
        *)
            echo "Unknown argument: $1"
            usage
            exit 1
            ;;
    esac
done
As you can see, I want to pass arguments depending on the option(s) chosen by the user; ie. --snooze, or --channel.

By default, if no options are chosen, I'll display a usage message; though in the future I'll provide some sane defaults.

I'd like to create a case statement to handle passing arguments to any number of options; something like:
Code:
wakethehellup.sh --snooze 20 --message 'wake up!'
and for the other arguments, it would have a default set.

The case statement I provided fails with a syntax error "syntax error near unexpected token `$2'" near the '--snooze' in the statement, so I take it you can't pass a parameter in this way; but I'm confused as to how I'm supposed to pass different parameters to different options without the options being confused as parameters. Could somebody provide some insight?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 08-07-2010, 08:28 PM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
that can be something like this:
Code:
#!/bin/bash

usage() {
    echo "Usage: $0 [-z|--snooze] [-c|--channel] [-p|--playlist]
    [-m|--message] [-v|--mpcvolume]"
	exit 1
}

if [ $# -eq 0 ]; then
    echo "No arguments supplied"
    usage
fi

ztime=10
chan='PCM'
playlist='alarm'
msg='Wake up, ya lazy bum!'
mpcvol=80

while [[ $# -gt 0 ]]; do
	case "$1" in
		--snooze|-z)
			ztime=$2
			shift
			;;
		--channel|-c)
			chan=$2
			shift
			;;
		--playlist|-p)
			playlist=$2
			shift
			;;
		--message|-m)
			msg=$2
			shift
			;;
		--mpcvolume|-v)
			mpcvol=$2
			shift
			;;
#       --option-with-no-arg)
#           variable=1 || variable=true
#           ;;
		*)
			echo "Unknown argument: $1"
			usage
			;;
	esac
	shift
done

# ....
 
2 members found this post helpful.
Old 08-07-2010, 09:00 PM   #3
supulton
LQ Newbie
 
Registered: Feb 2010
Posts: 21

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
that can be something like this:
Code:
#!/bin/bash

usage() {
    echo "Usage: $0 [-z|--snooze] [-c|--channel] [-p|--playlist]
    [-m|--message] [-v|--mpcvolume]"
	exit 1
}

if [ $# -eq 0 ]; then
    echo "No arguments supplied"
    usage
fi

ztime=10
chan='PCM'
playlist='alarm'
msg='Wake up, ya lazy bum!'
mpcvol=80

while [[ $# -gt 0 ]]; do
	case "$1" in
		--snooze|-z)
			ztime=$2
			shift
			;;
		--channel|-c)
			chan=$2
			shift
			;;
		--playlist|-p)
			playlist=$2
			shift
			;;
		--message|-m)
			msg=$2
			shift
			;;
		--mpcvolume|-v)
			mpcvol=$2
			shift
			;;
#       --option-with-no-arg)
#           variable=1 || variable=true
#           ;;
		*)
			echo "Unknown argument: $1"
			usage
			;;
	esac
	shift
done

# ....
thanks, that works perfectly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash case statement with multiple command line options pwc101 Programming 16 06-01-2007 12:43 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 09:24 AM
Problem with passing arguments in Perl bahadur Programming 1 05-30-2005 02:47 AM
C++, indefinite function arguments and multiple constructor passing @_@ R00ts Programming 2 04-08-2005 04:33 PM
Handline passing arguments in C AMMullan Programming 9 03-22-2004 02:37 AM


All times are GMT -5. The time now is 03:11 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration