Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-13-2010
Registered User
 

Join Date: Jul 2009
Posts: 94
Thanks: 1
Thanked 0 Times in 0 Posts
break while loop in BASH

Hi gurus, I have the following part of code which I am using for treating input

Code:
#!/bin/bash

while [[ $1 = -* ]]; do
    arg=$1; shift

    case $arg in
        -u)
            users="$1"
            shift
            ;;
        -g)
            groups="$1"
            shift
            ;;
        -m)
            mlength="$1"
            shift
            ;;
        -t)
            dfileg="$1"
            shift
            ;;
        --help)
            echo "-m maximum count of characters per line in group file"
            echo "for errors see file called: errorlog"
            break
            ;;
        *)
            break
            ;;

when I issue

Code:
./script -u john -g daemon -m 100 -t group

everything works fine

but when I issue

Code:
./script --help

the script echos text but did not exit. (I have to interrupt it by ctrl+c) how can I close script after issuing --help ? Thanks a lot
Sponsored Links
    #2  
Old 03-13-2010
alister alister is offline Forum Advisor  
Registered User
 

Join Date: Dec 2009
Posts: 1,496
Thanks: 39
Thanked 308 Times in 270 Posts
Change 'break' to 'exit' in the --help case.
Sponsored Links
    #3  
Old 03-14-2010
Registered User
 

Join Date: Jul 2009
Posts: 94
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks a lot, this helps BTW is there any more elegant way of treating input ?
    #4  
Old 03-14-2010
alister alister is offline Forum Advisor  
Registered User
 

Join Date: Dec 2009
Posts: 1,496
Thanks: 39
Thanked 308 Times in 270 Posts
Hi wakatana:

I don't think I would go so far as to call it elegant, but there is a standardized utility for dealing with command line options and arguments. getopts

It's usually implemented as a shell built-in, so search your shell's man page for 'getopts'.

Regards,
Alister
Sponsored Links
    #5  
Old 03-15-2010
Registered User
 

Join Date: Feb 2006
Location: Southern England
Posts: 230
Thanks: 0
Thanked 9 Times in 9 Posts
yes, you should use getopts
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to break a loop if condition is met Issemael Shell Programming and Scripting 9 07-19-2009 05:59 AM
Weird loop break,- please Help sx3v1l_1n51de Shell Programming and Scripting 1 07-13-2009 11:24 AM
How to break the while loop???(Very Urgent) sunitachoudhury Shell Programming and Scripting 1 03-21-2008 04:26 AM
ssh break the while loop? fedora Shell Programming and Scripting 5 05-23-2007 10:54 AM
ksh how user can break out of loop beckett Shell Programming and Scripting 4 04-07-2004 06:55 AM



All times are GMT -4. The time now is 03:45 AM.