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

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

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

#1 Post 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
Last edited by Mobeus on Wed 20 Mar 2013, 13:41, edited 1 time in total.
/root for the home team

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#2 Post 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.

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#3 Post 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. :?
/root for the home team

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#4 Post 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!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#5 Post 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
/root for the home team

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#6 Post 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!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#7 Post 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
/root for the home team

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#8 Post 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!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#9 Post 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
/root for the home team

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#10 Post by Karl Godt »

Code: Select all

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

Code: Select all

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

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#11 Post 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
/root for the home team

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#12 Post 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

Post Reply