public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] shell script
@ 2006-04-18  5:34 scwang
  2006-04-18  5:41 ` [gentoo-user] " Francesco Talamona
  2006-04-18  5:47 ` [gentoo-user] " Zac Slade
  0 siblings, 2 replies; 5+ messages in thread
From: scwang @ 2006-04-18  5:34 UTC (permalink / raw
  To: gentoo-user

In a shellp script, let
       STRING="a.txt b.txt c.txt"
And I want to delete a sub-string from $STRING, for example
b.txt, and then we got
       $STRING is "a.txt c.txt"

How to achieve it?


-- 
scwang
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [gentoo-user] Re: shell script
  2006-04-18  5:34 [gentoo-user] shell script scwang
@ 2006-04-18  5:41 ` Francesco Talamona
  2006-04-18  5:47 ` [gentoo-user] " Zac Slade
  1 sibling, 0 replies; 5+ messages in thread
From: Francesco Talamona @ 2006-04-18  5:41 UTC (permalink / raw
  To: gentoo-user

On Tuesday 18 April 2006 07:34, scwang@ios.ac.cn wrote:
> In a shellp script, let
>        STRING="a.txt b.txt c.txt"
> And I want to delete a sub-string from $STRING, for example
> b.txt, and then we got
>        $STRING is "a.txt c.txt"
>
> How to achieve it?

http://www.tldp.org/LDP/abs/html/string-manipulation.html

ciao
	Francesco
-- 
Linux Version 2.6.16-gentoo-r2, Compiled #1 PREEMPT Thu Apr 13 07:09:30 
CEST 2006
One 2.2GHz AMD Athlon 64 Processor, 2GB RAM, 4416.05 Bogomips Total
aemaeth
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [gentoo-user] shell script
  2006-04-18  5:34 [gentoo-user] shell script scwang
  2006-04-18  5:41 ` [gentoo-user] " Francesco Talamona
@ 2006-04-18  5:47 ` Zac Slade
  2006-04-18 12:11   ` Benno Schulenberg
  1 sibling, 1 reply; 5+ messages in thread
From: Zac Slade @ 2006-04-18  5:47 UTC (permalink / raw
  To: gentoo-user

On Tuesday 18 April 2006 00:34, scwang@ios.ac.cn wrote:
> In a shellp script, let
>        STRING="a.txt b.txt c.txt"
> And I want to delete a sub-string from $STRING, for example
> b.txt, and then we got
>        $STRING is "a.txt c.txt"
>
> How to achieve it?
>From man bash:
${parameter:-word}
Use Default Values.  If parameter is unset or null, the expansion of word is 
substituted.  Otherwise, the value of parameter is substituted.

So for what you want just do:
STRING="a.txt b.txt c.txt"
${STRING:-b.txt}

There are tons of ways to manipulate "parameters" and you should reference the 
man page for more.  Look under "Parameter Expansion" for all the 
possibilities.
-- 
Zac Slade
krakrjak@volumehost.net
ICQ:1415282 YM:krakrjak AIM:ttyp99

-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [gentoo-user] shell script
  2006-04-18  5:47 ` [gentoo-user] " Zac Slade
@ 2006-04-18 12:11   ` Benno Schulenberg
  2006-04-18 17:12     ` Boyd Stephen Smith Jr.
  0 siblings, 1 reply; 5+ messages in thread
From: Benno Schulenberg @ 2006-04-18 12:11 UTC (permalink / raw
  To: gentoo-user

Zac Slade wrote:
> On Tuesday 18 April 2006 00:34, scwang@ios.ac.cn wrote:
> > In a shellp script, let
> >        STRING="a.txt b.txt c.txt"
> > And I want to delete a sub-string from $STRING, for example
> > b.txt, and then we got
> >        $STRING is "a.txt c.txt"
>
> So for what you want just do:
> STRING="a.txt b.txt c.txt"
> ${STRING:-b.txt}

That doesn't work.  This does:

$ STRING="a.txt b.txt c.txt"
$ REMOVE="b.txt"
$ echo ${STRING/$REMOVE/}
a.txt c.txt

Benno
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [gentoo-user] shell script
  2006-04-18 12:11   ` Benno Schulenberg
@ 2006-04-18 17:12     ` Boyd Stephen Smith Jr.
  0 siblings, 0 replies; 5+ messages in thread
From: Boyd Stephen Smith Jr. @ 2006-04-18 17:12 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 1680 bytes --]

On Tuesday 18 April 2006 07:11, Benno Schulenberg 
<benno.schulenberg@gmail.com> wrote about 'Re: [gentoo-user] shell 
script':
> Zac Slade wrote:
> > On Tuesday 18 April 2006 00:34, scwang@ios.ac.cn wrote:
> > > In a shellp script, let
> > >        STRING="a.txt b.txt c.txt"
> > > And I want to delete a sub-string from $STRING, for example
> > > b.txt, and then we got
> > >        $STRING is "a.txt c.txt"
> >
> > So for what you want just do:
> > STRING="a.txt b.txt c.txt"
> > ${STRING:-b.txt}
>
> That doesn't work.  This does:

Yeah, I don't know why someone would ever think that
STRING="a.txt b.txt c.txt"
made STRING empty or undefined. Or why substituting in the value "b.txt" 
would give the desired "a.txt c.txt".

(Hi Zac! :P)

> $ STRING="a.txt b.txt c.txt"
> $ REMOVE="b.txt"
> $ echo ${STRING/$REMOVE/}
> a.txt c.txt

However, it actually leave an extra space in the variable, which got 
swallowed by your shell since you didn't quote the expansion. Following 
the above with:
$ echo "${STRING/$REMOVE/}"
a.txt  c.txt

You might try either including the leading or trailing space in REMOVE or 
running the string through shell expansion (as you did by calling echo 
without quoting the expansion)

The original poster might want to look into bash arrays, esp. since the 
${VARIABLE/MATCH/REPLACEMENT} expansion doesn't work in sh either (so we 
are already into bash-specific shell), AFAIK.  

-- 
"If there's one thing we've established over the years,
it's that the vast majority of our users don't have the slightest
clue what's best for them in terms of package stability."
-- Gentoo Developer Ciaran McCreesh

[-- Attachment #2: Type: application/pgp-signature, Size: 191 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-04-18 17:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-18  5:34 [gentoo-user] shell script scwang
2006-04-18  5:41 ` [gentoo-user] " Francesco Talamona
2006-04-18  5:47 ` [gentoo-user] " Zac Slade
2006-04-18 12:11   ` Benno Schulenberg
2006-04-18 17:12     ` Boyd Stephen Smith Jr.

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox