variable invalid?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
ifconfig
Level 1
Level 1
Posts: 7
Joined: Fri Oct 18, 2013 7:57 am
Contact:

variable invalid?

Post by ifconfig »

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 ~ $
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29587
Joined: Wed Jul 06, 2011 3:58 am

Re: variable invalid?

Post by xenopeek »

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.
Image
ifconfig
Level 1
Level 1
Posts: 7
Joined: Fri Oct 18, 2013 7:57 am
Contact:

Re: variable invalid?

Post by ifconfig »

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

Re: variable invalid?

Post by ifconfig »

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 ~ $ 
User avatar
xenopeek
Level 25
Level 25
Posts: 29587
Joined: Wed Jul 06, 2011 3:58 am

Re: variable invalid?

Post by xenopeek »

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.
Image
ifconfig
Level 1
Level 1
Posts: 7
Joined: Fri Oct 18, 2013 7:57 am
Contact:

Re: variable invalid?

Post by ifconfig »

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: 7
Joined: Fri Oct 18, 2013 7:57 am
Contact:

Re: variable invalid?

Post by ifconfig »

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 ~ $

User avatar
xenopeek
Level 25
Level 25
Posts: 29587
Joined: Wed Jul 06, 2011 3:58 am

Re: variable invalid?

Post by xenopeek »

Doh! I missed that also :)
Image
Locked

Return to “Scripts & Bash”