LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-17-2017, 02:07 PM   #1
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Rep: Reputation: 18
bash getopts pare param


Hello,

I'm facing this situation and for now I'm running completely out of ideas....

Code:
while getopts :M:h:vc:w: arg; do
        case $arg in
        M)  mon="$OPTARG"
            do_checks $hostname        ;;
        v)  verbose="yes";;
        w)  w1="$OPTARG";;
        c)  c1="$OPTARG";;
        h)  hostname="$OPTARG";;
        *)   usage;;
        esac
done

Well,
1st problem
I try to exec my script as ./script -M -h _HOSTNAME_

I have no idea how to parse $hostname variable that is an argument to -h option..
 
Old 02-17-2017, 02:11 PM   #2
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
late edit:

is working as designed if I i exec script as: ./script -h _HOSTNAME_ -M
 
Old 02-17-2017, 02:18 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Like this?

Code:
while getopts :Mh:vc:w: arg; do
        case $arg in
        M)  mon="yes"
        v)  verbose="yes";;
        w)  w1="$OPTARG";;
        c)  c1="$OPTARG";;
        h)  hostname="$OPTARG";;
        *)   usage;;
        esac
done

if [ "$mon" = yes ] ; then
    do_checks $hostname
fi
 
Old 02-17-2017, 02:25 PM   #4
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by ntubski View Post
Like this?

Code:
while getopts :Mh:vc:w: arg; do
        case $arg in
        M)  mon="yes"
        v)  verbose="yes";;
        w)  w1="$OPTARG";;
        c)  c1="$OPTARG";;
        h)  hostname="$OPTARG";;
        *)   usage;;
        esac
done

if [ "$mon" = yes ] ; then
    do_checks $hostname
fi
you just saved my day !!!


and here is 2nd issue

if parameter -M is used => to require as mandatory also params -w -c
 
Old 02-17-2017, 02:26 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Because the original order says 'do_checks $hostname', define $hostname.

So $hostname isn't defined yet. With -h first, you define the $hostname, then run -M using it.
 
Old 02-17-2017, 02:32 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Better to ask your questions all at once as dribs and drabs will start to frustrate some people

As shown by ntubski, you already have an 'if' setup that says -M was chosen, so within that 'if' you can also test if w1 and c1 were set and if not, display a message to advise these are required
when using -M OR if the values are pre-known defaults you could set them if not already set to new values
 
Old 02-17-2017, 02:43 PM   #7
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by grail View Post
Better to ask your questions all at once as dribs and drabs will start to frustrate some people

As shown by ntubski, you already have an 'if' setup that says -M was chosen, so within that 'if' you can also test if w1 and c1 were set and if not, display a message to advise these are required
when using -M OR if the values are pre-known defaults you could set them if not already set to new values
yep, got the idea now
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can bash's getopts function handle switches as words? nyc_rr Programming 3 06-10-2014 02:07 PM
getopts question in bash script. lleb Linux - Newbie 4 07-20-2012 05:37 PM
getopts and mandatory arguments in BASH jmcejuela Programming 3 04-03-2009 04:00 PM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM
[BASH] How to filter characters away? !{param#word} {param%word}! Dark Carnival Programming 8 03-17-2005 01:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:00 AM.

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