zsh-workers
 help / color / mirror / code / Atom feed
* Stripping spaces from a shell variable, portably
@ 2003-10-01  9:27 DervishD
  2003-10-01  9:50 ` Thomas Köhler
  2003-10-01 15:14 ` Bart Schaefer
  0 siblings, 2 replies; 5+ messages in thread
From: DervishD @ 2003-10-01  9:27 UTC (permalink / raw)
  To: Zsh

    Hi all :))

    I need to strip leading and trailing spaces from the contents of
a shell variable, and I need to do it portably, with a construct like
${...%% } and with no loops. When I say 'portably' I mean using SuSv3
constructs, not Zsh extensions.

    I've tried the obvious ${variable# } for stripping a single
space, but this needs a while loop, which I want to avoid (although
it seems to be portable):

    while [ "$variable" != "${variable# }" ]
    do
        variable="${variable# }"
    done

    And the same with the '%' construct. Please note that I cannot
use '[[' for the test, since it must be portable, and this involves
exec'ing the 'test' binary for each space in the variable :((

    If no solution is possible I'll use 'tr', because the shell code
must run in bash and zsh at least, but I really want it to be able to
run in any SuSv3 compliant shell.

    Thanks a lot for your help :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Stripping spaces from a shell variable, portably
  2003-10-01  9:27 Stripping spaces from a shell variable, portably DervishD
@ 2003-10-01  9:50 ` Thomas Köhler
  2003-10-01 14:00   ` DervishD
  2003-10-01 15:14 ` Bart Schaefer
  1 sibling, 1 reply; 5+ messages in thread
From: Thomas Köhler @ 2003-10-01  9:50 UTC (permalink / raw)
  To: Zsh

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

Hi,

DervishD wrote [2003/10/01]:
>     Hi all :))
> 
>     I need to strip leading and trailing spaces from the contents of
> a shell variable, and I need to do it portably, with a construct like
> ${...%% } and with no loops. When I say 'portably' I mean using SuSv3
> constructs, not Zsh extensions.
> 
>     I've tried the obvious ${variable# } for stripping a single
> space, but this needs a while loop, which I want to avoid (although
> it seems to be portable):
> 
>     while [ "$variable" != "${variable# }" ]
>     do
>         variable="${variable# }"
>     done
> 
>     And the same with the '%' construct. Please note that I cannot
> use '[[' for the test, since it must be portable, and this involves
> exec'ing the 'test' binary for each space in the variable :((
> 
>     If no solution is possible I'll use 'tr', because the shell code
> must run in bash and zsh at least, but I really want it to be able to
> run in any SuSv3 compliant shell.

Why not
  variable=`echo "$variable" | sed -e 's/^ *//' -e 's/ *$//'`
?

I'm not sure whether all versions of sed understand multiple
"-e" arguments, so it might me necessary to use this:
  variable=`echo "$variable" | sed -e 's/^ *//' | sed -e 's/ *$//'`

>     Thanks a lot for your help :)
> 
>     Raúl Núñez de Arenas Coronado

Ciao,
Thomas

-- 
 Thomas Köhler Email:   jean-luc@picard.franken.de     | LCARS - Linux
     <><        WWW:     http://jeanluc-picard.de      | for Computers
                IRC:             jeanluc               | on All Real
               PGP public key available from Homepage! | Starships

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

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

* Re: Stripping spaces from a shell variable, portably
  2003-10-01  9:50 ` Thomas Köhler
@ 2003-10-01 14:00   ` DervishD
  0 siblings, 0 replies; 5+ messages in thread
From: DervishD @ 2003-10-01 14:00 UTC (permalink / raw)
  To: Zsh

    Hi Thomas :)

 * Thomas Köhler <jean-luc@picard.franken.de> dixit:
> Why not
>   variable=`echo "$variable" | sed -e 's/^ *//' -e 's/ *$//'`

    Because if possible I want to do it just with shell code, without
external programs. In fact, instead of sed I would prefer do with
'tr', so the code is not dependant on sed. 'tr' won't do a good job
if the variable contents have spaces (legal ones) interspersed, so if
sed is the only solution I would prefer to have the nasty spaces
instead.

> I'm not sure whether all versions of sed understand multiple
> "-e" arguments, so it might me necessary to use this:

    According to SuSv3 (since the shell script will be SuSv3
compliant, I'd better use it for sed, too), you can provide multiple
-e options, so if I use 'sed' (not probable, as I tell in the last
paragraph) I will use multiple -e arguments.

    Thanks anyway, Thomas. Really these kind of processes should be
done by sed, not shell code, if you want portability, but in this
particular case I prefer the loop: slower, nastier, but not dependent
on sed (although sed should be present on every Unix system out
there, specially SuSv3 compliant ones...).

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Stripping spaces from a shell variable, portably
  2003-10-01  9:27 Stripping spaces from a shell variable, portably DervishD
  2003-10-01  9:50 ` Thomas Köhler
@ 2003-10-01 15:14 ` Bart Schaefer
  2003-10-01 19:49   ` DervishD
  1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2003-10-01 15:14 UTC (permalink / raw)
  To: Zsh

On Oct 1, 11:27am, DervishD wrote:
}
}     I need to strip leading and trailing spaces from the contents of
} a shell variable, and I need to do it [...] using SuSv3
} constructs, not Zsh extensions.

What about:

	tmp="${variable##*[^ ]}"
	variable="${variable%${tmp}}"
	tmp="${variable%%[^ ]*}"
	variable="${variable#${tmp}}"


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

* Re: Stripping spaces from a shell variable, portably
  2003-10-01 15:14 ` Bart Schaefer
@ 2003-10-01 19:49   ` DervishD
  0 siblings, 0 replies; 5+ messages in thread
From: DervishD @ 2003-10-01 19:49 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> }     I need to strip leading and trailing spaces from the contents of
> } a shell variable, and I need to do it [...] using SuSv3
> } constructs, not Zsh extensions.
> What about:
> 	tmp="${variable##*[^ ]}"
> 	variable="${variable%${tmp}}"
> 	tmp="${variable%%[^ ]*}"
> 	variable="${variable#${tmp}}"

    You're an effin genius, Bart ;)) I didn't even thing about using
a temporary... I wrote a similar solution, but using Zsh nested
substitution feature (getting rid of the temporary) in two
assignments, one for stripping the leading, another for the trailing.

    Thanks a lot :)) This is for a shell based building system, just
like autoconf but without autodetection (by now) and easier for
developers (they don't need to learn almost anything), that will be
published, if possible, this month. It's working since some time, but
now I'm doing some cleanup, and I needed to sanitize the options and
other things in the code. Most of the trimming is just one space, but
in a few points there are more... Thanks for your help :)

    If everybody in the world used zsh, I would have had no problems
writing this building system... but unfortunately I must write it
portably :( At least is GPL'd ;)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

end of thread, other threads:[~2003-10-01 19:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-01  9:27 Stripping spaces from a shell variable, portably DervishD
2003-10-01  9:50 ` Thomas Köhler
2003-10-01 14:00   ` DervishD
2003-10-01 15:14 ` Bart Schaefer
2003-10-01 19:49   ` DervishD

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).