niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
A program to quote like bash?
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
andrew.fabbro@gmail.com
*nix forums beginner


Joined: 13 Feb 2005
Posts: 6

PostPosted: Wed Jul 19, 2006 10:45 pm    Post subject: A program to quote like bash? Reply with quote

I have a directory with files full of parentheses, spaces, etc. If I'm
in bash I can type cp Blah<TAB> and bash quotes it all very nicely.

Now what I want to do is something like...

find /someplace -print > /tmp/list
<magic happens>
<everything in the file is now quoted just like bash>

....with the idea that such a file could then be used in various other
operations without getting a lot of errors due to &, (, ), spaces,
quotes, etc.

I can't seem to find, or even think of, a tool that would do this...?

Thanks.
Back to top
James Michael Fultz
*nix forums beginner


Joined: 19 Feb 2005
Posts: 11

PostPosted: Thu Jul 20, 2006 6:54 am    Post subject: Re: A program to quote like bash? Reply with quote

* <andrew.fabbro@gmail.com>:
Quote:
I have a directory with files full of parentheses, spaces, etc. If I'm
in bash I can type cp Blah<TAB> and bash quotes it all very nicely.

Now what I want to do is something like...

find /someplace -print > /tmp/list
magic happens
everything in the file is now quoted just like bash

If you don't mind some characters being unnecessarily quoted, the
following would do:

find /someplace -print | sed 's,[^/a-zA-Z0-9._-],\\&,g' > /tmp/list

Alternately, a pattern matching special characters could be used but is
messier. Also, I be leaving out something here:

find /someplace -print |
sed 's,[][|&;()<>[:blank:]*?\\'\''"$~!],\\&,g' > /tmp/list

Quote:
...with the idea that such a file could then be used in various other
operations without getting a lot of errors due to &, (, ), spaces,
quotes, etc.

If you could tell us what you're trying to do, there might be alternate
approaches.

Using the `-exec' option with find may work for your situation. Since
you mentioned bash, perhaps GNU find and GNU xargs are available to you
as well. You may be able to use `-print0' with find and `-0' with xargs
to solve the problem.

--
James Michael Fultz <xyzzy@sent.as.invalid>
Remove this part when replying ^^^^^^^^
Back to top
survivalist@gmail.com
*nix forums beginner


Joined: 22 Dec 2005
Posts: 4

PostPosted: Thu Jul 20, 2006 10:30 am    Post subject: Re: A program to quote like bash? Reply with quote

James Michael Fultz wrote:
Quote:
If you don't mind some characters being unnecessarily quoted, the
following would do:

find /someplace -print | sed 's,[^/a-zA-Z0-9._-],\\&,g' > /tmp/list

That's probably the best route...better to say what's legal than what's
not, since the latter usually only works until you come across
something that should have been on the list ;)

Quote:
If you could tell us what you're trying to do, there might be alternate
approaches.

Using the `-exec' option with find may work for your situation. Since
you mentioned bash, perhaps GNU find and GNU xargs are available to you
as well. You may be able to use `-print0' with find and `-0' with xargs
to solve the problem.

Yes, those are all good choices. I guess I was asking if there was a
generalized tool that said "quote this string so it can be used on the
command line". In this case, I have a few thousand files that need to
be moved (one-time) to different directories. There's no way to do it
except by looking at the file list...so I thought to dump the list into
a text file, vi the file to make it into a shell script. Multiple
finds, etc. could also do it...

Anyway, it seems there should be some sort of generalized tool for this
purpose. Alas, I'll have to add it to my list of things to do in my
copious spare time ;)

Thanks!
Back to top
John DuBois
*nix forums addict


Joined: 30 Mar 2005
Posts: 61

PostPosted: Thu Jul 20, 2006 5:35 pm    Post subject: Re: A program to quote like bash? Reply with quote

In article <1153349122.562734.18700@i3g2000cwc.googlegroups.com>,
<andrew.fabbro@gmail.com> wrote:
Quote:
I have a directory with files full of parentheses, spaces, etc. If I'm
in bash I can type cp Blah<TAB> and bash quotes it all very nicely.

Now what I want to do is something like...

find /someplace -print > /tmp/list
magic happens
everything in the file is now quoted just like bash

...with the idea that such a file could then be used in various other
operations without getting a lot of errors due to &, (, ), spaces,
quotes, etc.

I can't seem to find, or even think of, a tool that would do this...?

I use something like this - it does a bit of extraneous quoting in some
cases, but it works:

awk '/[^-0-9A-Za-z_.\/:+]/{gsub("\047","&\\\\&&",$0);$0="\047"$0"\047"}1'

Also, see the %q formatter to the internal printf in bash (and ksh93).

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Mon Dec 01, 2008 7:19 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts can u solve this program :P bawen C++ 0 Mon Jul 24, 2006 6:26 pm
No new posts Need Help with Program in Perl on a Netware Server fhadzocos@gmail.com Perl 3 Fri Jul 21, 2006 1:57 pm
No new posts bind keyboard POWER button to start some program Vladi Lemurov Debian 1 Fri Jul 21, 2006 6:00 am
No new posts Recommend an email program for the debian-user-digest? DJ Hackenbruch Debian 5 Fri Jul 21, 2006 3:20 am
No new posts A simple bash script JPB Suse 2 Fri Jul 21, 2006 2:19 am

Problem Mortgage | Business Credit Card | Mortgages | Premade MySpace Layouts | Quick Collect
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2109s ][ Queries: 16 (0.1267s) ][ GZIP on - Debug on ]