For Programmers: Free Programming Magazines  


Home > Archive > Unix Shell Programming > September 2004 > Parsing a bash string?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Parsing a bash string?
seguso

2004-09-18, 8:56 am

Hello,

For my application (http://onefinger.sf.net), which is a wrapper over bash,
I have to be able to parse a command line typed by the user.

I.e., I need a function that, when given a typical bash command string like

mplayer -sub /usr/a\ good\ movie.srt "/home/a good movie.avi"

, outputs a list of python strings (tokens):

[ "mplayer", "-sub", "a good movie.srt", "/home/a good movie.avi"]

Writing this function doesn't seem easy to me: it seems I must split the
string when I encounter a space/tab, but not if the space/tab is enclosed
into quotes... furthermore, I have to remove the quotes (notice the last
token in the string has the quotes removed).

Do you know some python library I can use? Or is there an easier way?

PS: The current implementation is broken when the user writes quotes.

Thanks for any help,
--
Best Regards,
Maurizio Colucci --- http://onefinger.sf.net
Please remove the uppercase letters "S,P,A,M":
seSgPuAsMo.forever@tin.it
seguso

2004-09-18, 8:56 am

John L wrote:

>
> "seguso" <look@in.signature> wrote in message
> news:jfT2d.342483$5D1.15296543@news4.tin.it...
>
> You want to parse the string in the same way bash would, so
> the easiest thing is to let bash do it.
>
> The outline of such a function might look like:
> while [ $# -gt 0 ]
> do
> echo $1
> shift
> done


That's what I wanted to know! I'll try. Thanks a lot!

BTW, shift is not recognized by bash, but this seems to work:

#!/bin/sh
for i in "$@"
do
echo $i
done

--
Best Regards,
Maurizio Colucci --- http://onefinger.sf.net
Please remove the uppercase letters "S,P,A,M":
seSgPuAsMo.forever@tin.it
William Park

2004-09-18, 3:58 pm

seguso <look@in.signature> wrote:
> Hello,
>
> For my application (http://onefinger.sf.net), which is a wrapper over bash,
> I have to be able to parse a command line typed by the user.
>
> I.e., I need a function that, when given a typical bash command string like
>
> mplayer -sub /usr/a\ good\ movie.srt "/home/a good movie.avi"
>
> , outputs a list of python strings (tokens):
>
> [ "mplayer", "-sub", "a good movie.srt", "/home/a good movie.avi"]
>
> Writing this function doesn't seem easy to me: it seems I must split the
> string when I encounter a space/tab, but not if the space/tab is enclosed
> into quotes... furthermore, I have to remove the quotes (notice the last
> token in the string has the quotes removed).
>
> Do you know some python library I can use? Or is there an easier way?


Let Bash do it.

1. a=( mplayer ... )

2. set -- mplayer ...

--
William Park <opengeometry@yahoo.ca>
Open Geometry Consulting, Toronto, Canada
Chris F.A. Johnson

2004-09-18, 3:58 pm

On 2004-09-18, seguso wrote:
> John L wrote:
>
>
> That's what I wanted to know! I'll try. Thanks a lot!
>
> BTW, shift is not recognized by bash,


It certainly is! All Bourne-type shells use it.

> but this seems to work:
>
> #!/bin/sh
> for i in "$@"
> do
> echo $i
> done


Or just:

printf "%s\n" "$@"


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Bill Marcum

2004-09-18, 8:56 pm

On Sat, 18 Sep 2004 11:47:55 GMT, seguso
<look@in.signature> wrote:
>
> BTW, shift is not recognized by bash, but this seems to work:
>

What version of bash does not recognize shift?


--
System Events
=-=-=-=-=-=-=
Sep 16 03:31:11 don kernel: lp0 on fire

rakesh sharma

2004-09-19, 3:56 am

seguso <look@in.signature> wrote in message news:

>
> For my application (http://onefinger.sf.net), which is a wrapper over bash,
> I have to be able to parse a command line typed by the user.
>
> I.e., I need a function that, when given a typical bash command string like
>
> mplayer -sub /usr/a\ good\ movie.srt "/home/a good movie.avi"
>
> , outputs a list of python strings (tokens):
>
> [ "mplayer", "-sub", "a good movie.srt", "/home/a good movie.avi"]
>
> Writing this function doesn't seem easy to me: it seems I must split the
> string when I encounter a space/tab, but not if the space/tab is enclosed
> into quotes... furthermore, I have to remove the quotes (notice the last
> token in the string has the quotes removed).
>
> Do you know some python library I can use? Or is there an easier way?
>


#!/bin/sh

# define a function to parse the options here
ParseOptions() {
for i do
printf '%s\n' "|$i|"
done
}

# invoke the parser function
ParseOptions mplayer -sub /usr/a\ good\ movie.srt "/home/a good movie.avi"
Stephane CHAZELAS

2004-09-20, 9:04 am

2004-09-18, 09:30(+00), seguso:
> Hello,
>
> For my application (http://onefinger.sf.net), which is a wrapper over bash,
> I have to be able to parse a command line typed by the user.
>
> I.e., I need a function that, when given a typical bash command string like
>
> mplayer -sub /usr/a\ good\ movie.srt "/home/a good movie.avi"
>
> , outputs a list of python strings (tokens):
>
> [ "mplayer", "-sub", "a good movie.srt", "/home/a good movie.avi"]

[...]

And what should it do if the line is:

mplayer ./*.mpg

mplayer `find . -name '*.mpg'`

for f in ./*.mpg; do maplyer "$f"; done

--
Stephane
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com