Re: shell command line argument + parsing function
2009/8/31 Stefan Miklosovic miklosovic.free...@gmail.com: hi, assuming I execute shell script like this $ ./script -c hello world I want to save hello world string to variable COMMENT in shell script. code: #!/bin/sh parse_cmdline() { while [ $# -gt 0 ]; do case $1 in -c) shift COMMENT=$1 ;; esac shift done } parse_cmdline $* echo $COMMENT exit 0 How about getopts builtin, so you can use: ./script -c hello world or ./script -chello world or while getopts c: f; do case $f in c) COMMENT=$OPTARG ;; \?) echo 'usage: $0 [-c string]' exit 1 ;; esac done echo COMMENT: $COMMENT -- Artis Caune Everything should be made as simple as possible, but not simpler. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: shell command line argument + parsing function
On Mon, Aug 31, 2009, Stefan Miklosovic wrote: hi, assuming I execute shell script like this $ ./script -c hello world I want to save hello world string to variable COMMENT in shell script. code: #!/bin/sh parse_cmdline() { while [ $# -gt 0 ]; do case $1 in -c) shift COMMENT=$1 ;; esac shift done } parse_cmdline $* echo $COMMENT exit 0 but that only write out hello. I tried to change $* to $@, nothing changed. Did you put $@ in quotes? That is parse_cmdline $@. I haven't tried this in calling functions in scripts, but use it frequently when calling scripts from other scripts. I would probably use something like: for arg in $@; do dosomething $arg done Bill -- INTERNET: b...@celestial.com Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax:(206) 232-9186 Skype: jwccsllc (206) 855-5792 Good luck to all you optimists out there who think Microsoft can deliver 35 million lines of quality code on which you can operate your business. -- John C. Dvorak ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: shell command line argument + parsing function
hehe :D easy as hell, one has to enclose argument of parse_cmdline into brackets :) parse_cmdline $@ sorry for noise On Mon, Aug 31, 2009 at 1:07 AM, Stefan Miklosovic miklosovic.free...@gmail.com wrote: hi, assuming I execute shell script like this $ ./script -c hello world I want to save hello world string to variable COMMENT in shell script. code: #!/bin/sh parse_cmdline() { while [ $# -gt 0 ]; do case $1 in -c) shift COMMENT=$1 ;; esac shift done } parse_cmdline $* echo $COMMENT exit 0 but that only write out hello. I tried to change $* to $@, nothing changed. It is interesting, that if I dont put while loop into function parse_cmdline, and do echo $COMMENT, it writes hello world. I WANT that function style. How to do it ? thank you ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: shell command line argument + parsing function
On Mon, Aug 31, 2009 at 01:07:36AM +0200, Stefan Miklosovic wrote: hi, assuming I execute shell script like this $ ./script -c hello world I want to save hello world string to variable COMMENT in shell script. code: #!/bin/sh parse_cmdline() { while [ $# -gt 0 ]; do case $1 in -c) shift COMMENT=$1 ;; esac shift done } parse_cmdline $* echo $COMMENT exit 0 but that only write out hello. I tried to change $* to $@, nothing changed. But if you use $@ (with the quote marks) instead it should work fine. For further explanation please read the sh(1) man page where it explains the special parameters $* and $@, while paying special attention to how they expand when used within double-quotes. It is interesting, that if I dont put while loop into function parse_cmdline, and do echo $COMMENT, it writes hello world. I WANT that function style. How to do it ? thank you ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org -- Insert your favourite quote here. Erik Trulsson ertr1...@student.uu.se ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org