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 > how to get the script name when sourcing it?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

how to get the script name when sourcing it?

Réponse
 
LinkBack Outils de la discussion
Vieux 25/04/2008, 02h33   #1
troyzeng
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut how to get the script name when sourcing it?

How to get the script name when sourcing it?
${0} returns '-bash'.

Thanks a lot.
  Réponse avec citation
Vieux 25/04/2008, 02h46   #2
mop2
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to get the script name when sourcing it?


$BASH_SOURCE

troyzeng wrote:
> How to get the script name when sourcing it?
> ${0} returns '-bash'.
>
> Thanks a lot.

  Réponse avec citation
Vieux 25/04/2008, 03h57   #3
troyzeng
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to get the script name when sourcing it?

On 25 Apr, 11:46, mop2 <mop2bky4mz5tyjwa8ersp7hrg5u...@gmail.com>
wrote:
> $BASH_SOURCE
>
> troyzeng wrote:
> > How to get the script name when sourcing it?
> > ${0} returns '-bash'.

>
> > Thanks a lot.


Thanks. That works for bash and sh.
What about ksh? Thanks.
  Réponse avec citation
Vieux 25/04/2008, 07h42   #4
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to get the script name when sourcing it?

2008-04-24, 19:57(-07), troyzeng:
> On 25 Apr, 11:46, mop2 <mop2bky4mz5tyjwa8ersp7hrg5u...@gmail.com>
> wrote:
>> $BASH_SOURCE
>>
>> troyzeng wrote:
>> > How to get the script name when sourcing it?
>> > ${0} returns '-bash'.

>>
>> > Thanks a lot.

>
> Thanks. That works for bash and sh.


Only for sh where sh is implemented by bash, so only GNU
systems.

> What about ksh? Thanks.


Depends on the implementation of ksh. In the zsh implementation,
you'll find it in ${(%):-%N} (%N, in prompt expansion is
expanded to the name of the currently sourced script (for PS2),
(%) enables prompt expansion in parameter expansion ${:-default}
is a special form of ${var:-default} which always expands to
"default").

Portably, you could do:

source() {
sourced_script=$1
. "$1"
}

And use "source" instead of ".". And then use $sourced_script.

--
Stéphane
  Réponse avec citation
Vieux 25/04/2008, 10h05   #5
troyzeng
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to get the script name when sourcing it?

On 25 Apr, 16:42, Stephane CHAZELAS <this.addr...@is.invalid> wrote:
> 2008-04-24, 19:57(-07), troyzeng:
>
> > On 25 Apr, 11:46, mop2 <mop2bky4mz5tyjwa8ersp7hrg5u...@gmail.com>
> > wrote:
> >> $BASH_SOURCE

>
> >> troyzeng wrote:
> >> > How to get the script name when sourcing it?
> >> > ${0} returns '-bash'.

>
> >> > Thanks a lot.

>
> > Thanks. That works for bash and sh.

>
> Only for sh where sh is implemented by bash, so only GNU
> systems.
>
> > What about ksh? Thanks.

>
> Depends on the implementation of ksh. In the zsh implementation,
> you'll find it in ${(%):-%N} (%N, in prompt expansion is
> expanded to the name of the currently sourced script (for PS2),
> (%) enables prompt expansion in parameter expansion ${:-default}
> is a special form of ${var:-default} which always expands to
> "default").
>
> Portably, you could do:
>
> source() {
> sourced_script=$1
> . "$1"
>
> }
>
> And use "source" instead of ".". And then use $sourced_script.
>
> --
> Stéphane


My question really should be: how to get the script name within the
script when the script is meant to be sourced (to setup environment
like a profile)?
E.g., my script is called a.ksh. I want to find out the script name
within a.ksh. If a.ksh is executed, ${0} is the name of the script (or
the first part of the command line); when a.ksh is sourced, what is
equivalent to $[0}?
Troy
  Réponse avec citation
Vieux 25/04/2008, 11h33   #6
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to get the script name when sourcing it?

2008-04-25, 02:05(-07), troyzeng:
[...]
> My question really should be: how to get the script name within the
> script when the script is meant to be sourced (to setup environment
> like a profile)?
> E.g., my script is called a.ksh. I want to find out the script name
> within a.ksh. If a.ksh is executed, ${0} is the name of the script (or
> the first part of the command line); when a.ksh is sourced, what is
> equivalent to $[0}?

[...]

So I think you can't unless the caller has made it available in
a variable (as in my source() function example) or unless you
assume ksh is implemented with zsh.

--
Stéphane
  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 10h04.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12064 seconds with 14 queries