From owner-freebsd-questions@FreeBSD.ORG Mon Feb 15 20:16:02 2010 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 20FF9106566B for ; Mon, 15 Feb 2010 20:16:02 +0000 (UTC) (envelope-from nlandys@gmail.com) Received: from qw-out-2122.google.com (qw-out-2122.google.com [74.125.92.24]) by mx1.freebsd.org (Postfix) with ESMTP id CB6338FC13 for ; Mon, 15 Feb 2010 20:16:01 +0000 (UTC) Received: by qw-out-2122.google.com with SMTP id 8so574470qwh.7 for ; Mon, 15 Feb 2010 12:16:01 -0800 (PST) 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=1NZOnx9UP9N7Q8993emiuGJ24T/XSmJ49eiAk+jTUeM=; b=JxY9Cr/RC6BLwhDx0yfIqJQQs3Ml62nKtNU4tzRq2dwADkGAud7NCYhmPw697f7inG bmiSGJxNOFIyKIt0YHtwh0wGRQsxIz34WGBz5ZkIDwnkVHCBEQqe+yIZZ6GMSox0pRFF IFN6fg4cVnEOxwSm8xiyRjFlpks5rc9AfGaIs= 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=AKMSXzqqrXyuMmBCQ8I++pVgmiECJ9v1ce4JKDytOiCb7sSsIJMwgaPzkXEfH7MT2h ABMnmEf/Nv1OAY86VaFP/kqqkBecz+jkSSQCAYt5k7PjsdrnpOn357Fpy4W2WICVMdwd ZK+58xTphMYZEIOojLH+Xw7LNtknrgi538o4o= MIME-Version: 1.0 Received: by 10.229.131.152 with SMTP id x24mr384963qcs.84.1266264961024; Mon, 15 Feb 2010 12:16:01 -0800 (PST) In-Reply-To: References: <560f92641002142207w7eade79fr6a4f40ae5b92f4b9@mail.gmail.com> Date: Mon, 15 Feb 2010 12:16:00 -0800 Message-ID: <560f92641002151216l6b8e97f4u886ac8e3b86d231e@mail.gmail.com> From: Nerius Landys To: Christian Weisgerber Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: freebsd-questions@freebsd.org Subject: Re: simple (and stupid) shell scripting question 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: Mon, 15 Feb 2010 20:16:02 -0000 >> #!/bin/sh >> >> DIRNAME=3D"`dirname \"$0\"`" >> cd "$DIRNAME" >> SCRIPTDIR=3D"`pwd`" >> >> What if I got rid of extra double quotes? =A0Like this: >> >> DIRNAME=3D`dirname \"$0\"` >> cd "$DIRNAME" >> SCRIPTDIR=3D`pwd` > > That is perfectly fine. =A0Word-splitting and filename expansion are > not performed for variable assignments. =A0Also immune is the expression > after "case", so this is always fine: > > =A0case $FOO in ... Since you guys have been so helpful I thought I'd ask one more question. Let's say I have a script named "te st" (yes, with a space in the filename)= . The contents of this script are: #!/bin/sh if [ $# -ne 2 ]; then echo 1>&2 "One argument expected. Example usage:" echo 1>&2 " `basename \"${0}\"` 64.156.192.169 cheaters" exit 1 fi When executed with no arguments, this script would print something like so: nlandys@daffy# ./te\ st One argument expected. Example usage: te st 64.156.192.169 cheaters nlandys@daffy# Is there a function, or command line utility, to "escape" a string, making it suitable to be input on the command line? For example, this "escape" utility would take a input of "te st" and create an output of "te\ st". Other things such as quotes and single quotes would be handled as well.