Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

/bin/sh help needed



At some point hitherto, David Kramer hath spake thusly:
> > You seem to have overlooked the most simple solution: don't quote it
> > at all.  This should give you exactly what you want: a space separated
> > list of args.  The main reason for quotes is to preserve space or
> > change the default handling of metacharacters.  In this case, it
> > appears you don't want to do either.  So don't use quotes.
> 
> With no quoting at all, the wildcards get expanded to files, and I need to 
> pass the wildcards to the program, not the matching files.

Hmm...  when I typed that it seemed as though this was what you were
looking for; though after thinking about it for a second I can
conceive of reasons why you wouldn't.  In that case, try this:

  foo='/etc/r* /etc/rc.d/r*'
  foo=`echo "$foo" | sed -e "s/^/\'/" -e "s/ /\' \'/" -e "s/$/\'/"`
  echo "$foo"
  

The result should be to surround each argument by single quotes, which
should prevent the wildcard from being expanded by the shell when it
is appended to some command.  For example:

  ls -l $foo

would run the following command line:

  ls -l '/etc/r*' '/etc/rc.d/r*'

This prevents the shell from expanding * and looks for the specific
files, which contain asterisks.  I'll note that ordinarily, this is
not what one wants, and one would generally want the ls command to run
on all files that match the path with the expanded metacharacter, i.e.

  ls -l /etc/r* /etc/rc.d/r*

in which case you would not want to perform the sed operation, and use
$foo without quotes.  If you were passing the arguments to a subshell,
? la:

  bash -c "ls $foo"

there would be no practical difference between running either of the
following commands (the quoting is tricky here due to the fact that
bash -c requires the argument to be all one string):

  bash -c "ls "'/etc/r*'" "'/etc/rc.d/r*'
  bash -c "ls /etc/r* /etc/rc.d/r*"

[Note also that the first command can be simplified with the same
effect to:

  bash -c "ls "'/etc/r* /etc/rc.d/r*'
]

The only difference between the two is that in the first case, the
subshell expands the *, and in the second the parent shell does.  But
in both cases the end result is the same.  There's no practical
difference.

The only case I could think of where there is a practical difference
is when you're passing the arguments to a program as a regular
expression (or other program which parses metacharacters, such as
find).  But it hadn't occured to me that you might be doing that until
I read this message. ;-)

HTH

-- 
Derek Martin               ddm at pizzashack.org    
---------------------------------------------
I prefer mail encrypted with PGP/GPG!
GnuPG Key ID: 0x81CFE75D
Retrieve my public key at http://pgp.mit.edu
Learn more about it at http://www.gnupg.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
URL: <http://lists.blu.org/pipermail/discuss/attachments/20020425/889a61ee/attachment.sig>



BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org