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 > Simple question: Capturing exported variables problem
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Simple question: Capturing exported variables problem

Réponse
 
LinkBack Outils de la discussion
Vieux 20/03/2008, 20h36   #1
wrpassos@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Simple question: Capturing exported variables problem

Hi,

I have a small issue with Bash: I try to run a bash script as a Linux
service, exporting one variable to be used in another bash script.
Here's the important pieces of the scripts:

#!bin/sh
# test script
# chkconfig 345 86 15

USER1=user1
export VAR1=/opt/test
RETVAL=0

start() {
su -l $USER1 -c $VAR1/test.sh
RETVAL=$?
}
....
exit $RETVAL

So, I registered it as a service in RHEL using chkconfig --add test.
See that I call a second script, test.sh, not as root, but as the
user1 account. This account has full privileges inside /opt/test
directory. In /opt/test/test.sh, I just have one echo, simple line:

#!bin/sh
echo "VAR1 = $VAR1"

But VAR1 is always empty when it's echoed. I never get the /opt/test
value.

When I modify the su -l line above to
.. "$VAR1"/test.sh

It works fine this way! Unfortunately, I need to run the service using
the user1 credential, not root. So, what am I missing here? Could you
me?

Thank you very much!
  Réponse avec citation
Vieux 20/03/2008, 20h59   #2
OldSchool
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple question: Capturing exported variables problem

On Mar 20, 3:36pm, wrpas...@gmail.com wrote:
> Hi,
>
> I have a small issue with Bash: I try to run a bash script as a Linux
> service, exporting one variable to be used in another bash script.
> Here's the important pieces of the scripts:
>
> #!bin/sh
> # test script
> # chkconfig 345 86 15
>
> USER1=user1
> export VAR1=/opt/test
> RETVAL=0
>
> start() {
> su -l $USER1 -c $VAR1/test.sh
> RETVAL=$?}
>
> ...
> exit $RETVAL
>
> So, I registered it as a service in RHEL using chkconfig --add test.
> See that I call a second script, test.sh, not as root, but as the
> user1 account. This account has full privileges inside /opt/test
> directory. In /opt/test/test.sh, I just have one echo, simple line:
>
> #!bin/sh
> echo "VAR1 = $VAR1"
>
> But VAR1 is always empty when it's echoed. I never get the /opt/test
> value.
>
> When I modify the su -l line above to
> . "$VAR1"/test.sh
>
> It works fine this way! Unfortunately, I need to run the service using
> the user1 credential, not root. So, what am I missing here? Could you
> me?
>
> Thank you very much!


well...you're not *you* any more. you su'd to somebody else with a
whole new environment.

how about something like:

su -l $USER1 -c $VAR1/test.sh $VAR1

so that it passes an argument to the test script



and in test.sh:
#!/bin/sh
VAR1=$1
echo $VAR1
  Réponse avec citation
Vieux 21/03/2008, 15h36   #3
wrpassos@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple question: Capturing exported variables problem

Hi "OldSchool",

I found a simple solution (how didn't I look at it before?): su has
the -m switch, "preserve environment", so the su line can be changed
to:

su -m $USER1 -c $VAR1/test.sh

This way we can call the other script using the user1 account
preserving the environment variables.

Thanks a lot for your !

On Mar 20, 4:59 pm, OldSchool <scott.my...@macys.com> wrote:
> On Mar 20, 3:36 pm, wrpas...@gmail.com wrote:
>
>
>
> > Hi,

>
> > I have a small issue with Bash: I try to run a bash script as a Linux
> > service, exporting one variable to be used in another bash script.
> > Here's the important pieces of the scripts:

>
> > #!bin/sh
> > # test script
> > # chkconfig 345 86 15

>
> > USER1=user1
> > export VAR1=/opt/test
> > RETVAL=0

>
> > start() {
> > su -l $USER1 -c $VAR1/test.sh
> > RETVAL=$?}

>
> > ...
> > exit $RETVAL

>
> > So, I registered it as a service in RHEL using chkconfig --add test.
> > See that I call a second script, test.sh, not as root, but as the
> > user1 account. This account has full privileges inside /opt/test
> > directory. In /opt/test/test.sh, I just have one echo, simple line:

>
> > #!bin/sh
> > echo "VAR1 = $VAR1"

>
> > But VAR1 is always empty when it's echoed. I never get the /opt/test
> > value.

>
> > When I modify the su -l line above to
> > . "$VAR1"/test.sh

>
> > It works fine this way! Unfortunately, I need to run the service using
> > the user1 credential, not root. So, what am I missing here? Could you
> > me?

>
> > Thank you very much!

>
> well...you're not *you* any more. you su'd to somebody else with a
> whole new environment.
>
> how about something like:
>
> su -l $USER1 -c $VAR1/test.sh $VAR1
>
> so that it passes an argument to the test script
>
> and in test.sh:
> #!/bin/sh
> VAR1=$1
> echo $VAR1

  Réponse avec citation
Vieux 21/03/2008, 16h52   #4
OldSchool
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple question: Capturing exported variables problem

On Mar 21, 10:36am, wrpas...@gmail.com wrote:
> Hi "OldSchool",
>
> I found a simple solution (how didn't I look at it before?): su has
> the -m switch, "preserve environment", so the su line can be changed
> to:
>
> su -m $USER1 -c $VAR1/test.sh
>
> This way we can call the other script using the user1 account
> preserving the environment variables.
>


I believe that that's a GNU'ism that may not be portable......
  Réponse avec citation
Vieux 21/03/2008, 17h22   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple question: Capturing exported variables problem

2008-03-21, 08:52(-07), OldSchool:
> On Mar 21, 10:36am, wrpas...@gmail.com wrote:
>> Hi "OldSchool",
>>
>> I found a simple solution (how didn't I look at it before?): su has
>> the -m switch, "preserve environment", so the su line can be changed
>> to:
>>
>> su -m $USER1 -c $VAR1/test.sh
>>
>> This way we can call the other script using the user1 account
>> preserving the environment variables.
>>

>
> I believe that that's a GNU'ism that may not be portable......


I think it rather comes from BSD. But it's true that few Unices
have it.

--
Stéphane
  Réponse avec citation
Vieux 21/03/2008, 17h49   #6
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple question: Capturing exported variables problem

In article <slrnfu7o6u.9us.stephane.chazelas@spam.is.invalid> ,
Stephane CHAZELAS <this.address@is.invalid> wrote:
....
>> I believe that that's a GNU'ism that may not be portable......

>
>I think it rather comes from BSD. But it's true that few Unices
>have it.


A statement much akin to saying that few PC operating systems support
the AllocConsole() function call.

It is true that very few do.

It is also true that almost every PC OS that matters does.

You pay your money; you take your chances.

  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 17h36.


É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,14615 seconds with 14 queries