How to create a global variable

Post here if you are a new Porteus member and you're looking for some help.
Post Reply
bour59
Samurai
Samurai
Posts: 118
Joined: 29 Dec 2010, 08:10
Distribution: porteus v3.2.2-xfce
Location: France

How to create a global variable

Post#1 by bour59 » 25 Sep 2011, 15:30

Bonjour,
most of the time I'm working in text mode on several computers.
So, Porteus is loaded either from sda4 or sdb5 or sdh1 ...
Maybe a $porteus global variable should be set (like $PATH ..)
$porteus=/mnt/sdxx/porteus
I'm using a local variable defined in .bash_profile
issued from /mnt/live/var/log/livedbg
I vote for "cd $porteus" instead "cd /mnt/sd??/porteus" without knowing sd??

Where are these global variables located?
Someone has a way to do ?
thanks

User avatar
Hamza
Warlord
Warlord
Posts: 1846
Joined: 28 Dec 2010, 07:41
Distribution: Porteus
Location: France

Re: How to create a global variable

Post#2 by Hamza » 25 Sep 2011, 16:35

Try with /proc/self/environ

Ciao!
NjVFQzY2Rg==

User avatar
fanthom
Site Admin
Site Admin
Posts: 4547
Joined: 28 Dec 2010, 02:42
Distribution: Porteus Kiosk
Location: Poland, currently - Cork, IE
Contact:

Re: How to create a global variable

Post#3 by fanthom » 26 Sep 2011, 09:00

hello bour59,

please add 2 lines to /etc/profile:

Code: Select all

# export Porteus booting device:
export porteus=`awk 'c-->0;/Porteus data found in/{c=1}' /mnt/live/var/log/livedbg`
this variable will be set by default in Porteus 1.1

Cheers
Please add [Solved] to your thread title if the solution was found.

bour59
Samurai
Samurai
Posts: 118
Joined: 29 Dec 2010, 08:10
Distribution: porteus v3.2.2-xfce
Location: France

Re: How to create a global variable [Solved]

Post#4 by bour59 » 26 Sep 2011, 13:35

Many thanks. I was using in .bash_profie

Code: Select all

old_IFS=$IFS ; IFS=$'\n' ; i=1
     for line in $(cat /mnt/live/var/log/livedbg) ; do 
     array[$i]=$line ; ((i+=1)) ; done
   IFS=$old_IFS
   for ((i=1; i<=${#array[@]};i++)) ; do 
     case ${array[$i]:0:6} in
     '# Port')
     export porteus=${array[$i+1]};;
     esac
   done
Ill see Hamza's soluce later
It's time for me to learn awk
Good pub to have a porteus variable very usefull
http://porteus.org/forum/posting.php?mo ... f=81&t=798#

bour59
Samurai
Samurai
Posts: 118
Joined: 29 Dec 2010, 08:10
Distribution: porteus v3.2.2-xfce
Location: France

Re: How to create a global variable

Post#5 by bour59 » 28 Jan 2012, 18:29

@hamza : hello!
you wrote
Postby Hamza » 28 Jan 2012, 12:10
It was possible to track directly the porteus boot device by using $porteus variable but it has been removed due to some reported user's troubles.
Can you tell me more about these troubles
Please is there a right way to get the porteus boot device into a variable
Thanks

User avatar
Hamza
Warlord
Warlord
Posts: 1846
Joined: 28 Dec 2010, 07:41
Distribution: Porteus
Location: France

Re: How to create a global variable

Post#6 by Hamza » 28 Jan 2012, 18:58

fanthom should answer you as he had made and removed this variable.
NjVFQzY2Rg==

User avatar
fanthom
Site Admin
Site Admin
Posts: 4547
Joined: 28 Dec 2010, 02:42
Distribution: Porteus Kiosk
Location: Poland, currently - Cork, IE
Contact:

Re: How to create a global variable

Post#7 by fanthom » 29 Jan 2012, 09:16

Can you tell me more about these troubles
Global variables are bad.
let's say we have porteus variable set globally which is used in many porteus scripts. user is not aware of this variable and exports it's own global variable under the same (porteus) name. result: all porteus scripts are broken.

i would rather create external library containing porteus specific variables (like booting device, changes directory, xxx) and functions (create_module, find_and_run_scripts, xxx) and import this lib when necessary. have a look on slax's /usr/lib/liblinuxlive which is used in activate, xzm2dir, etc scripts.

i see nothing wrong with using global variables when you are the only user of the system.
Please add [Solved] to your thread title if the solution was found.

User avatar
brokenman
Site Admin
Site Admin
Posts: 5436
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v3.2rcX all desktops
Location: Brazil
Contact:

Re: How to create a global variable

Post#8 by brokenman » 30 Jan 2012, 02:25

I agree. Global variables can be useful, but they need to have unique names. I fell into this trap with porteus global variable while writing PPM. It's not too much harder to grab the path from porteus-livedbg.
How do i become super user?
Wear your underpants on the outside and put on a cape.

Post Reply