|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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). |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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). |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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") |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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") |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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") |
|
![]() |
| Outils de la discussion | |
|
|