Page 1 of 1

Bash need to read and export a variable during boot (Solved)

Posted: Tue 19 Mar 2013, 14:21
by Mobeus
Hi folks,

I'm trying to add a boot script to /etc/profile.local that will export a global environment variable. I need to check the existence of and contents of 2 files. If the first file exists and has an entry, then use that entry plus two characters for the value of the environment variable. If the first file is empty or missing then I need to get the value from the second file and use that plus the two characters if that file exists. If neither file exists or both are empty then a default gets exported.

I can read a file into a variable and export it like this

Code: Select all

#export MY_VARIABLE=$(<$file)-x
#echo $MY_VARIABLE 
mydata-x 
but what I need to accomplish is beyond my current bash skills.

Help please?

Regards,
Mobeus

Posted: Tue 19 Mar 2013, 17:03
by Karl Godt
There are different kinds of global variables :
In the background with no controlling tty like rc.local or rc.services ,
In the console mode after login/autologin (etc/profile)
In X (xwin, /root/.xinitrc)

Only variables exported after login would reach X environment.

Posted: Tue 19 Mar 2013, 20:23
by Mobeus
Thanks for the interesting info Karl. At least I know that I'm trying to add the script in the right place.

Still haven't figured out how to write it though. :?

Posted: Tue 19 Mar 2013, 20:37
by SFR
If the first file exists and has an entry, then use that entry plus two characters for the value of the environment variable. If the first file is empty or missing then I need to get the value from the second file and use that plus the two characters if that file exists. If neither file exists or both are empty then a default gets exported.
Is this what you mean?

Code: Select all

FILE1=/root/file1
FILE2=/root/file2

if [ -s "$FILE1" ]; then
  export MY_VARIABLE=$(<"$FILE1")-x
elif [ -s "$FILE2" ]; then
  export MY_VARIABLE=$(<"$FILE2")-x
else
  export MY_VARIABLE=default_value
fi

echo $MY_VARIABLE
HTH
Greetings!

Posted: Tue 19 Mar 2013, 22:40
by Mobeus
Thanks SFR,

Now I know I'm not a complete bash ignoramus :) I didn't want to post my code until I (hopefully) saw an example. Our code is very similar except I read the data and make sure it's valid. At least that's the idea. Here's what I have that's not working

Code: Select all

#!/bin/bash

file=$HOME/profile/windowmanager

if [ -e "$file" ]; then
	#read data into variable, strip all whitespace, add hyphen & c
	data=$(<"$file")
	data=${data//[[:space:]]}
else
	file=/etc/windowmanager
	[ -e "$file" ] && data=$(<"$file")
	data=${data//[[:space:]]}
fi

if [ "${#data}" -eq 0 ]; then
	echo exporting default MY_VARIABLE
	data=default
fi

data=$data-c

export MY_VARIABLE=$data

echo var is $MY_VARIABLE

echo strlen is ${#MY_VARIABLE}
/etc/windowmanager has this in it; jwm, a space, and a newline
this is what I get running the above

Code: Select all

# ./bashtest
var is jwm -c
strlen is 7
#
Why in the world is my code returning jwm -c instead of jwm-c and strlen of 5??

Regards,
Mobeus

Posted: Tue 19 Mar 2013, 23:16
by SFR
And that's really weird indeed...
Your script works correctly for me, so let's see what's in those files "under the hood":

Code: Select all

hexdump -C /etc/windowmanager && hexdump -C $HOME/profile/windowmanager
BTW, does it happens also with 'default' string and did you try the script on some other textual files?

Greetings!

Posted: Wed 20 Mar 2013, 01:45
by Mobeus
Does this tell you anything?

Code: Select all

# hexdump -C /etc/windowmanager && hexdump -C $HOME/profile/windowmanager
00000000  6a 77 6d c2 a0 0a                                 |jwm...|
00000006
hexdump: /root/profile/windowmanager: No such file or directory
hexdump: /root/profile/windowmanager: Bad file descriptor
# 
My code works well on other files with characters followed by a space & newline. Just this one file is causing trouble of course, it's one I need to read.

BTW, this is in lupu 5.10, is this the normal construction of the /etc/windowmanager file in this Puppy? It's an odd file layout.

Regards,
Mobeus

Posted: Wed 20 Mar 2013, 09:41
by SFR
IMHO, 'c2 a0' shouldn't be there, it's 'NO-BREAK SPACE'.
I checked /etc/windowmanager in lupu-510.iso and it contains only '6a 77 6d', so something's been changed in between.
I believe simple echo jwm > /etc/windowmanager will fix it.

Greetings!

Posted: Wed 20 Mar 2013, 13:37
by Mobeus
So, a non breaking space... html. It makes me wonder. I know I've never opened that file in a browser or html editor.

Anyway, did the echo jwm > /etc/widowmanager and that gave a hexdump of '6a 77 6d 0a' so I removed the ending newline and rebooted. Everything still works, and the bash code works as expected now too. And here I was, certain it was something I was doing wrong.

Thanks for taking the time to work through this, SFR. It's been an education :)

Regards,
Mobeus

Posted: Wed 20 Mar 2013, 15:31
by Karl Godt

Code: Select all

  data=${data//[[:space:]]}
shouldn't it be

Code: Select all

  data=${data//[[:space:]]/}
?

Posted: Wed 20 Mar 2013, 19:03
by Mobeus
Hi Karl,

That is a good question! My bash script was put together from Google searches so I could not explain how it works, only observe what it does.

Code: Select all

data=${data//[[:space:]]}
and

Code: Select all

data=${data//[[:space:]]/}
perform exactly the same as far as I can tell.

Is there a real world difference? Perhaps even necessary for use with non-english texts?

I would truly like to know.

Regards,
Mobeus

Posted: Thu 21 Mar 2013, 00:20
by Karl Godt
Yes I can confirm that it works for me. too, both in busybox-1.19.4 ash and bash-3.00 . My manual page is a little outdated and has only this entry with one substitution :

Code: Select all

${parameter/pattern/string}
while for % and # substitution there are mentioned also %% and ##
(GNU Bash