From owner-freebsd-questions@FreeBSD.ORG Tue Sep 1 08:19:37 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 563081065670 for ; Tue, 1 Sep 2009 08:19:37 +0000 (UTC) (envelope-from artis.caune@gmail.com) Received: from mail-bw0-f206.google.com (mail-bw0-f206.google.com [209.85.218.206]) by mx1.freebsd.org (Postfix) with ESMTP id D62D18FC14 for ; Tue, 1 Sep 2009 08:19:36 +0000 (UTC) Received: by bwz2 with SMTP id 2so3145700bwz.43 for ; Tue, 01 Sep 2009 01:19:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=weJLsd8o5LLtHbCyTXnsbGGk+djALWAbP2JvtOr4q3A=; b=XAtD2fv8okxQ58s65xeqhNSt/JTaAfnx+fQtosToTnpkt/GQp0t+vMUEdosdbA5Z4I PpjFPYuzczKLiyT745b9K96bnFvC/Cs3omLArK1MXqjasDG7Xk9Lk4ppkjnWE9FRs8sF /VwQSGGCtnSbqcgr6LAFKxWtueIJTc/chOaJw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=LcumwKHPDQQdNcaQjPt7InCi7R9IMb8qCYHgoHJt91fHmD41q09DMiFHnKpgelsgLv Od+SoeYTlhiECkG2eBrBmrE240628BXNcT2VrX2nCN626ICk++J5iR3T/fZ/k78rbhZ6 7s3aWiUIQTgELd6SL+8yiHeVQviEaev5rIhaQ= MIME-Version: 1.0 Received: by 10.103.78.38 with SMTP id f38mr2742505mul.72.1251793175403; Tue, 01 Sep 2009 01:19:35 -0700 (PDT) In-Reply-To: References: Date: Tue, 1 Sep 2009 11:19:35 +0300 Message-ID: <9e20d71e0909010119g387c3869pb61a59c8b3c73c5b@mail.gmail.com> From: Artis Caune To: Stefan Miklosovic Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: freebsd-questions@freebsd.org Subject: Re: shell command line argument + parsing function X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Sep 2009 08:19:37 -0000 2009/8/31 Stefan Miklosovic : > 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() { > =C2=A0 =C2=A0while [ $# -gt 0 ]; do > =C2=A0 =C2=A0 =C2=A0 =C2=A0case "$1" in > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-c) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0shift > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0COMMENT=3D"$1" > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0;; > =C2=A0 =C2=A0 =C2=A0 =C2=A0esac > =C2=A0 =C2=A0 =C2=A0 =C2=A0shift > =C2=A0 =C2=A0done > } > > parse_cmdline $* > > echo $COMMENT > > exit 0 How about getopts builtin, so you can use: ./script -c "hello world" or ./script -c"hello world" or while getopts c: f; do case $f in c) COMMENT=3D$OPTARG ;; \?) echo 'usage: $0 [-c string]' exit 1 ;; esac done echo "COMMENT: $COMMENT" --=20 Artis Caune Everything should be made as simple as possible, but not simpler.