variable invalid?

Forum rules
Before you post please read this

variable invalid?

Postby ifconfig on Wed Oct 23, 2013 3:24 pm

Hi, can you explain me, why the variable has no value in the second call in the bin/test?
here the log:
Code: Select all
user1@pc1 ~ $ echo $MODAT
/media/user1/daten/daten/mobile_daten/
user1@pc1 ~ $ echo ${MODAT}
/media/user1/daten/daten/mobile_daten/
user1@pc1 ~ $ cat  bin/test
#! /bin/bash

echo ${MODAT}
user1@pc1 ~ $  bin/test

user1@pc1 ~ $
ifconfig
Level 1
Level 1
 
Posts: 6
Joined: Fri Oct 18, 2013 7:57 am

Linux Mint is funded by ads and donations.
 

Re: variable invalid?

Postby xenopeek on Wed Oct 23, 2013 3:46 pm

Did you export the variable MODAT? If not, it is a variable only set for the current process (the bash shell in your case). If you want subsequent processes (such as you telling your bash shell to run bin/test) to inherit the variable, you have to export it.

See bash's manpage for more info on export.

You can check which variables are exported by running the command `export`. To export the variable in your example, you would run `export MODAT`. Or `export MODAT=/media/user1/daten/daten/mobile_daten/` if you want to export and set the variable in one go.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14554
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: variable invalid?

Postby ifconfig on Thu Oct 24, 2013 6:26 am

Thank you, the export solved my problem.
ifconfig
Level 1
Level 1
 
Posts: 6
Joined: Fri Oct 18, 2013 7:57 am

Re: variable invalid?

Postby ifconfig on Thu Oct 24, 2013 6:41 am

But another problem consists...if i call ALT+F2 and type in "l", the logins.txt file doesnt get openend by gedit, instead a new empty file gets opened in /home/user1/logins.txt.
But my logins-script should open the existing logins.txt in /media/user1/daten/daten/mobile_daten/logins.txt. Is this also another export problem?

The script i call with ALT+F2+"l" is the following:
Code: Select all
user1@pc1 ~ $ cat bin/l
#! /bin/bash

#gedit /media/user1/daten/daten/mobile_daten/logins.txt
gedit ${mobile_daten}logins.txt
user1@pc1 ~ $
ifconfig
Level 1
Level 1
 
Posts: 6
Joined: Fri Oct 18, 2013 7:57 am

Re: variable invalid?

Postby xenopeek on Thu Oct 24, 2013 6:48 am

Where have you done the export? You should put it in your .profile file I believe, so it will be set at login to the desktop. If you have put it in .bashrc, it will be set only when you open a terminal.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14554
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: variable invalid?

Postby ifconfig on Thu Oct 24, 2013 7:57 am

yes, i had the export in the bashrc. Now i have changed the export to the /etc/profile, and then reboot, but the problem remains. I have also tried to put the export into the /etc/environment, but it also doesnt help. then i have tried to write it without the "export", just the same as the PATH Variable is defined in the /etc/environment, but this also doesnt help...
ifconfig
Level 1
Level 1
 
Posts: 6
Joined: Fri Oct 18, 2013 7:57 am

Re: variable invalid?

Postby ifconfig on Thu Oct 24, 2013 8:10 am

Ah! Now i found the problem...i forgot to rename the variable in the script from mobile_daten to MODAT...now it works with ALT+F2+"l"

currently working config:
Code: Select all
user1@pc1 ~ $ cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
MODAT="/media/user1/daten/daten/mobile_daten/"
user1@pc1 ~ $ cat bin/l
#! /bin/bash

#gedit /media/user1/daten/daten/mobile_daten/logins.txt
gedit ${MODAT}logins.txt
user1@pc1 ~ $

ifconfig
Level 1
Level 1
 
Posts: 6
Joined: Fri Oct 18, 2013 7:57 am

Re: variable invalid?

Postby xenopeek on Thu Oct 24, 2013 9:44 am

Doh! I missed that also :)
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14554
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest