From owner-freebsd-questions@freebsd.org Tue Jun 7 18:15:05 2016 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 955C8B6EFCA for ; Tue, 7 Jun 2016 18:15:05 +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 5E87E11DC for ; Tue, 7 Jun 2016 18:15:04 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r56.edvax.de (port-92-195-156-20.dynamic.qsc.de [92.195.156.20]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx02.qsc.de (Postfix) with ESMTPS id 1C8A324C56; Tue, 7 Jun 2016 20:06:05 +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 u57I65hw003043; Tue, 7 Jun 2016 20:06:05 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Tue, 7 Jun 2016 20:06:05 +0200 From: Polytropon To: Murk Fletcher Cc: User Questions Subject: Re: rc scripting trouble with quotes Message-Id: <20160607200605.6761fc0e.freebsd@edvax.de> In-Reply-To: References: 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.22 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Jun 2016 18:15:05 -0000 On Tue, 7 Jun 2016 14:04:27 +0200, Murk Fletcher wrote: > Hi! > > How do I access a variable inside quotes? Right now I'm having some > difficulties: > > stop_cmd="cd ${myapp} && \ > ${myapp} stop && \ > kill -9 `cat ${myapp}/tmp/pids/example.pid`" > > Returns: > > cat: ${myapp}/tmp/pids/example.pid: No such file or directory Have you checked the actual value of that expression? For testing, insert something like echo ${myapp}/tmp/pids/example.pid to see if you _really_ get the file name you're expecting. > I hear it would work better with double quotes, [...] There is no "work better" here: If you use double quotes, variables will be expanded; if you use single quotes, they will not. Double quotes are usually needed when a path contains spaces (because the space character is the argument separator, while it also is a valid character for file names). > [...] but that would add a > double-double quote at the end: > > stop_cmd="cd ${myapp} && \ > ${myapp} stop && \ > kill -9 "$(cat -- ${myapp}/tmp/pids/example.pid)"" You could use qupting, \", but that probably won't work as intended. You need to use quotes because the value you assign to $stop_cmd contains spaces. You could use quoted spaces, \ , to get rid of them, but that makes the whole thing nearly unreadable and will probably introduce more problems. :-) > Is there a way I could wrap the contents of `stop_cmd` inside a function or > something? Basically, your initial assignment looks correct, there surely is a different problem (maybe with the evaluation of ${myapp}. As it as already been mentioned, /etc/rc.subr should do what's needed to stop a program, maybe you don't even need to do this manually. -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...