From owner-freebsd-questions@FreeBSD.ORG Mon Jun 22 02:01:48 2015 Return-Path: Delivered-To: freebsd-questions@nevdull.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 328833A4 for ; Mon, 22 Jun 2015 02:01:48 +0000 (UTC) (envelope-from listmgr@antennex.com) Received: from BAY004-OMC4S8.hotmail.com (bay004-omc4s8.hotmail.com [65.54.190.210]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (Client CN "*.outlook.com", Issuer "MSIT Machine Auth CA 2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 17799189 for ; Mon, 22 Jun 2015 02:01:47 +0000 (UTC) (envelope-from listmgr@antennex.com) Received: from BAY404-EAS84 ([65.54.190.201]) by BAY004-OMC4S8.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.22751); Sun, 21 Jun 2015 19:00:42 -0700 X-TMN: [TqTrN+GEEdxp491BDY26G3dbAzzDu8mgwDPKqqxS9bQ=] X-Originating-Email: [listmgr@antennex.com] Message-ID: From: "Lt. Commander" To: "'Polytropon'" , "'Lt. Commander'" CC: References: <20150615015516.b3ea7633.freebsd@edvax.de> <2609852.Pc7nSdcYla@desk8.phess.net> <20150622033037.6cfd270d.freebsd@edvax.de> In-Reply-To: <20150622033037.6cfd270d.freebsd@edvax.de> Subject: RE: Script question Date: Sun, 21 Jun 2015 21:00:39 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQABAgME0X1Dm5rl1ITbGRHvuqm5ZwDmqqb4AImG+awAOb+0/QChWiN0ACr4FWoAANHiZwBu370yAH5eu84A9Ixn/QDMN8yNAImCkRIA0TZFMaEjGtiA Content-Language: en-us X-OriginalArrivalTime: 22 Jun 2015 02:00:42.0764 (UTC) FILETIME=[3ECC94C0:01D0AC8F] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 22 Jun 2015 02:01:48 -0000 From: owner-freebsd-questions@freebsd.org [mailto:owner-freebsd-questions@freebsd.org] On Behalf Of Polytropon Sent: Sunday, June 21, 2015 8:31 PM To: Lt. Commander Cc: freebsd-questions@freebsd.org Subject: Re: Script question On Sun, 21 Jun 2015 16:36:36 -0500, Lt. Commander wrote: > Sheesh! Here's the script: > #get_yes_no() { > while true > do > echo -n "$1 (Y/N) ? " > read -t 30 a > if [ $? != 0 ]; then > a="No"; > return; > fi > case $a in > [Yy]) a="Yes"; > return;; > [Nn]) a="No"; > return;; > *);; > esac > done > #} > > get_yes_no "Do you want to continue......" > > [ $a = 'No' ] && exit 1 I did a little re-formatting for readability, so you can spot the mistake easier. The following code works as intended. For the test cases: [ (or "test") uses -eq and ! -eq (or -ne) for numerical values, but = and != for strings. See "man test" for details. Also note the removal of the ; after commands. Shell != C. :-) You will see the following cases now (as expected): Do you want to continue (Y/N) ? y Continuing! and Do you want to continue (Y/N) ? n and the program exits with exit code 1. It accepts y, Y, n and N as valid inputs, defaulting to the result "No" after 30 seconds. Every other input causes the input loop to repeat. Here's the code now: #!/bin/sh get_yes_no() { while true; do echo -n "$1 (Y/N) ? " read -t 30 REPLY if [ ! $? -eq 0 ]; then ANSWER="No" return fi case "$REPLY" in [Yy]) ANSWER="Yes" return ;; [Nn]) ANSWER="No" return ;; *) ;; esac done } get_yes_no "Do you want to continue" [ $ANSWER = "No" ] && exit 1 echo "Continuing!" ------------------------------------------------------- Bingo! You got it. Works fine. Funny tho, I have used that little sh shell intro for years to lead into various scripts. This is the first time it didn't work as I had it. Oh, well... now it's fixed and that's all that counts. Once again many thanks for the help!! --Jason -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... _______________________________________________ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"