DSM-G600, DNS-3xx and NSA-220 Hack Forum

Unfortunately no one can be told what fun_plug is - you have to see it for yourself.

You are not logged in.

Announcement

#1 2011-02-20 23:44:17

Opperpanter
Member
From: Eindhoven, Netherlands.
Registered: 2008-08-11
Posts: 89

Exporting variables doesn't work?!

Hello,

I am trying to store the external ip of my NAS in en environment variables.
As far as I can see a simple export should do the work.
I can't get it work however. Could be it's too late for my brains to see the mistake:

I created a file called setMYIP.sh

Code:

#!/bin/sh
#lookup the current ip of somehost.dyndns.org (managed by the WRT54G router)
MYIP=`nslookup somehost.dyndns.org | tail -n 1 | awk '{print $3}'`
echo $MYIP
export MYIP

When I run ./setMYIP.sh I am getting the expected output:

Code:

root@NAS:/ffp/scripts# ./setMYIP.sh
1.2.3.4

But afterwards when I check the value of the MYIP variable, it's not set at all

Code:

root@NAS:/ffp/scripts# set | grep MY
_=setMYIP.sh

I also tried the following, which didn't work either.

Code:

export MYIP=`nslookup somehost.dyndns.org | tail -n 1 | awk '{print $3}'`

Code:

export MYIP="1.2.3.4"

Enlighten me please smile

Last edited by Opperpanter (2011-02-20 23:45:07)

Offline

 

#2 2011-02-21 04:36:15

neerav
Member
Registered: 2011-01-16
Posts: 15

Re: Exporting variables doesn't work?!

My shell scripting is quite rusty, but here goes.  The script starts a new shell, and that environment ends when the script completes.  You can use that variable elsewhere in that script and in scripts and commands called by the original script.  But when the script ends, those variables also disappear.

I can guess two possible solutions:

1) if the script is used as a part of the startup, or the code in the fun_plug script, then that environment lasts as long as the device is on and may be inherited by other scripts.  A caveat: I have read somewhere in these forums that some scripts on the NAS, or is it cron jobs scripts, may run in a different environment and thus is the cause of many a script headache.

2) have your script dump the ip address into a file (echo $MYIP > /ffp/etc/myip.txt)  Have the script run on startup, and maybe as a cron job every x hours.  Your other scripts can read in the file when needed.


Neerav
DNS-321 • 2 x 2TB WD20EADS in standard mode • ffp 0.5

Offline

 

#3 2011-02-21 10:54:36

scaramanga
Member
Registered: 2010-08-04
Posts: 251

Re: Exporting variables doesn't work?!

neerav is right, when you run the script, it creates a new process that inherits it's environment from the process that ran it (the interactive shell, in this case). Changes to that process' environment won't be reflected in the interactive shell process' environment.
There is, however, a solution to that: run the script using '.', like so:

Code:

. ./setMYIP.sh

This will run the script in the current shell, without spawning a child process.

To learn more, see under Builtins here: http://linux.die.net/man/1/ash (you'll also find there what export does, and see why that didn't work).

Last edited by scaramanga (2011-02-21 10:55:52)


DNS-323 HW Rev. C1 FW 1.10 fun-plug 0.5
2 x WD10EARS-00Y5B1 in Standard mode (LCC set to 5 min; Aligned to 4K)
Transmission with Transmission Remote GUI

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2010 PunBB