PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > bad substitution
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

bad substitution

Réponse
 
LinkBack Outils de la discussion
Vieux 25/07/2007, 17h58   #1
patrick
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut bad substitution

Hello,

I have :

str=foo.txt
echo ${str%%.*}

which works.

But if I want to replace str by `basename $0` in echo..., I get "bad
substitution"

Is there a way to do it in 1 line ?
(I tried with eval() by whitout success...)

Thanks in advance.

  Réponse avec citation
Vieux 25/07/2007, 18h14   #2
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad substitution

On Wed, 25 Jul 2007 09:58:16 -0700, patrick wrote:

> Hello,
>
> I have :
>
> str=foo.txt
> echo ${str%%.*}
>
> which works.
>
> But if I want to replace str by `basename $0` in echo..., I get "bad
> substitution"
>
> Is there a way to do it in 1 line ?
> (I tried with eval() by whitout success...)
>
> Thanks in advance.


Not that I can see. That is the way things work.

You can use `basename $0` to get the NAME of a variable to expand, in
which case $0 had better not have a "." in its final component, as these
are not valid in variable names (unless you have ksh, and compound
variables).
  Réponse avec citation
Vieux 25/07/2007, 20h32   #3
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad substitution

patrick wrote:
> Hello,
>
> I have :
>
> str=foo.txt
> echo ${str%%.*}
>
> which works.
>
> But if I want to replace str by `basename $0` in echo..., I get "bad
> substitution"
>
> Is there a way to do it in 1 line ?
> (I tried with eval() by whitout success...)
>
> Thanks in advance.
>


If your intention is to increase speed, you can maybe do

str=${0##*/}; echo ${str%%.*}


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 25/07/2007, 20h53   #4
johngnub
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad substitution

On Jul 25, 12:32 pm, Michael Tosch
<eed...@NO.eed.SPAM.ericsson.PLS.se> wrote:
> patrick wrote:
> > Hello,

>
> > I have :

>
> > str=foo.txt
> > echo ${str%%.*}

>
> > which works.

>
> > But if I want to replace str by `basename $0` in echo..., I get "bad
> > substitution"

>
> > Is there a way to do it in 1 line ?
> > (I tried with eval() by whitout success...)

>
> > Thanks in advance.

>
> If your intention is to increase speed, you can maybe do
>
> str=${0##*/}; echo ${str%%.*}
>
> --
> Michael Tosch @ hp : com


Silly question, What is in his $0 var?
"bad substitution" is an odd evil message, hinting that his $0 has
some thing funny in it maybe ?
Just a thought.... JB

  Réponse avec citation
Vieux 26/07/2007, 04h25   #5
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad substitution

On Wed, 25 Jul 2007 12:53:17 -0700, johngnub wrote:

> On Jul 25, 12:32 pm, Michael Tosch
> <eed...@NO.eed.SPAM.ericsson.PLS.se> wrote:
>> patrick wrote:
>> > Hello,

>>
>> > I have :

>>
>> > str=foo.txt
>> > echo ${str%%.*}

>>
>> > which works.

>>
>> > But if I want to replace str by `basename $0` in echo..., I get "bad
>> > substitution"

>>
>> > Is there a way to do it in 1 line ?
>> > (I tried with eval() by whitout success...)

>>
>> > Thanks in advance.

>>
>> If your intention is to increase speed, you can maybe do
>>
>> str=${0##*/}; echo ${str%%.*}
>>
>> --
>> Michael Tosch @ hp : com

>
> Silly question, What is in his $0 var? "bad substitution" is an odd evil
> message, hinting that his $0 has some thing funny in it maybe ?
> Just a thought.... JB


Well, the example he gives is "foo.txt". The fact that he is trying to
strip off ".*" as a glob pattern implies that it probably has a "." in
it, which makes it an illegal name for a variable (unless he has compound
variables, which is pretty unlikely).
  Réponse avec citation
Vieux 26/07/2007, 09h24   #6
patrick
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad substitution

On 25 juil, 21:32, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
wrote:
> patrick wrote:
> > Hello,

>
> > I have :

>
> > str=foo.txt
> > echo ${str%%.*}

>
> > which works.

>
> > But if I want to replace str by `basename $0` in echo..., I get "bad
> > substitution"

>
> > Is there a way to do it in 1 line ?
> > (I tried with eval() by whitout success...)

>
> > Thanks in advance.

>
> If your intention is to increase speed, you can maybe do
>
> str=${0##*/}; echo ${str%%.*}



Thanks for your answer, but the goal is just to do :

str=`basename $0`
echo ${str%%.*}

in 1 instruction instead of 2.
(if my script name is "foo.ksh", I get "foo")

  Réponse avec citation
Vieux 26/07/2007, 10h43   #7
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad substitution

On 26 Jul., 10:24, patrick <patrick.beltra...@caramail.com> wrote:
> On 25 juil, 21:32, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
> wrote:
>
>
>
> > patrick wrote:
> > > Hello,

>
> > > I have :

>
> > > str=foo.txt
> > > echo ${str%%.*}

>
> > > which works.

>
> > > But if I want to replace str by `basename $0` in echo..., I get "bad
> > > substitution"

>
> > > Is there a way to do it in 1 line ?
> > > (I tried with eval() by whitout success...)

>
> > > Thanks in advance.

>
> > If your intention is to increase speed, you can maybe do

>
> > str=${0##*/}; echo ${str%%.*}

>
> Thanks for your answer, but the goal is just to do :


Goal? To achieve what? If you want to make a simple problem overly
complicated and less readable or less portable or less performant
you can have a look whether your shell supports ${var/.../...}
replacements and backreferences, otherwise I'd suggest to use the
standard solution that Michael proposed upthread

str=${0##*/}; echo ${str%%.*}

You can of course make use of external programs if the "one statement"
requirement is some homework task; have a look at the program expr(1)
as in

expr $var : regexp

where regexp contains the subexpression to be matched in var; what is
in regexp enclosed in \( and \) will be printed.
(http://www.scit.wlv.ac.uk/cgi-bin/mansec?1+expr)

Janis

>
> str=`basename $0`
> echo ${str%%.*}
>
> in 1 instruction instead of 2.
> (if my script name is "foo.ksh", I get "foo")



  Réponse avec citation
Vieux 26/07/2007, 10h43   #8
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bad substitution

On 26 Jul., 10:24, patrick <patrick.beltra...@caramail.com> wrote:
> On 25 juil, 21:32, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
> wrote:
>
>
>
> > patrick wrote:
> > > Hello,

>
> > > I have :

>
> > > str=foo.txt
> > > echo ${str%%.*}

>
> > > which works.

>
> > > But if I want to replace str by `basename $0` in echo..., I get "bad
> > > substitution"

>
> > > Is there a way to do it in 1 line ?
> > > (I tried with eval() by whitout success...)

>
> > > Thanks in advance.

>
> > If your intention is to increase speed, you can maybe do

>
> > str=${0##*/}; echo ${str%%.*}

>
> Thanks for your answer, but the goal is just to do :


Goal? To achieve what? If you want to make a simple problem overly
complicated and less readable or less portable or less performant
you can have a look whether your shell supports ${var/.../...}
replacements and backreferences, otherwise I'd suggest to use the
standard solution that Michael proposed upthread

str=${0##*/}; echo ${str%%.*}

You can of course make use of external programs if the "one statement"
requirement is some homework task; have a look at the program expr(1)
as in

expr $var : regexp

where regexp contains the subexpression to be matched in var; what is
in regexp enclosed in \( and \) will be printed.
(http://www.scit.wlv.ac.uk/cgi-bin/mansec?1+expr)

Janis

>
> str=`basename $0`
> echo ${str%%.*}
>
> in 1 instruction instead of 2.
> (if my script name is "foo.ksh", I get "foo")



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h52.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17943 seconds with 16 queries