From owner-freebsd-questions@FreeBSD.ORG Mon Jun 22 01:30:40 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 20A45F66 for ; Mon, 22 Jun 2015 01:30:40 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx02.qsc.de (mx02.qsc.de [213.148.130.14]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id D596A1C1D for ; Mon, 22 Jun 2015 01:30:39 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r56.edvax.de (port-92-195-85-234.dynamic.qsc.de [92.195.85.234]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx02.qsc.de (Postfix) with ESMTPS id D180B24C70; Mon, 22 Jun 2015 03:30:37 +0200 (CEST) Received: from r56.edvax.de (localhost [127.0.0.1]) by r56.edvax.de (8.14.5/8.14.5) with SMTP id t5M1UbU7002892; Mon, 22 Jun 2015 03:30:37 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Mon, 22 Jun 2015 03:30:37 +0200 From: Polytropon To: "Lt. Commander" Cc: freebsd-questions@freebsd.org Subject: Re: Script question Message-Id: <20150622033037.6cfd270d.freebsd@edvax.de> In-Reply-To: References: <20150615015516.b3ea7633.freebsd@edvax.de> <2609852.Pc7nSdcYla@desk8.phess.net> Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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 01:30:40 -0000 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!" -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...