Page 1 of 1

How to create a global variable

Posted: 25 Sep 2011, 15:30
by bour59
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

Re: How to create a global variable

Posted: 25 Sep 2011, 16:35
by Hamza
Try with /proc/self/environ

Ciao!

Re: How to create a global variable

Posted: 26 Sep 2011, 09:00
by fanthom
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

Re: How to create a global variable [Solved]

Posted: 26 Sep 2011, 13:35
by bour59
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#

Re: How to create a global variable

Posted: 28 Jan 2012, 18:29
by bour59
@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

Re: How to create a global variable

Posted: 28 Jan 2012, 18:58
by Hamza
fanthom should answer you as he had made and removed this variable.

Re: How to create a global variable

Posted: 29 Jan 2012, 09:16
by fanthom
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.

Re: How to create a global variable

Posted: 30 Jan 2012, 02:25
by brokenman
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.