| Author |
Message |
Mobeus

Joined: 26 Aug 2010 Posts: 93
|
Posted: Tue 19 Mar 2013, 10:21 Post subject:
Bash need to read and export a variable during boot (Solved) |
|
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: | #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
_________________ /root for the home team
Last edited by Mobeus on Wed 20 Mar 2013, 09:41; edited 1 time in total
|
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 2675 Location: Kiel,Germany
|
Posted: Tue 19 Mar 2013, 13:03 Post subject:
|
|
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.
|
|
Back to top
|
|
 |
Mobeus

Joined: 26 Aug 2010 Posts: 93
|
Posted: Tue 19 Mar 2013, 16:23 Post subject:
|
|
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
|
|
Back to top
|
|
 |
SFR

Joined: 26 Oct 2011 Posts: 570
|
Posted: Tue 19 Mar 2013, 16:37 Post subject:
|
|
| Quote: | | 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: | 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!
_________________ [O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
|
|
Back to top
|
|
 |
Mobeus

Joined: 26 Aug 2010 Posts: 93
|
Posted: Tue 19 Mar 2013, 18:40 Post subject:
|
|
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: | #!/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: | # ./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
|
|
Back to top
|
|
 |
SFR

Joined: 26 Oct 2011 Posts: 570
|
Posted: Tue 19 Mar 2013, 19:16 Post subject:
|
|
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: | | 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!
_________________ [O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
|
|
Back to top
|
|
 |
Mobeus

Joined: 26 Aug 2010 Posts: 93
|
Posted: Tue 19 Mar 2013, 21:45 Post subject:
|
|
Does this tell you anything?
| Code: | # 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
|
|
Back to top
|
|
 |
SFR

Joined: 26 Oct 2011 Posts: 570
|
Posted: Wed 20 Mar 2013, 05:41 Post subject:
|
|
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!
_________________ [O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
|
|
Back to top
|
|
 |
Mobeus

Joined: 26 Aug 2010 Posts: 93
|
Posted: Wed 20 Mar 2013, 09:37 Post subject:
|
|
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
|
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 2675 Location: Kiel,Germany
|
Posted: Wed 20 Mar 2013, 11:31 Post subject:
|
|
| Code: | | data=${data//[[:space:]]} |
shouldn't it be
| Code: | | data=${data//[[:space:]]/} |
?
|
|
Back to top
|
|
 |
Mobeus

Joined: 26 Aug 2010 Posts: 93
|
Posted: Wed 20 Mar 2013, 15:03 Post subject:
|
|
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: | | data=${data//[[:space:]]} |
and
| Code: | | 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
|
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 2675 Location: Kiel,Germany
|
Posted: Wed 20 Mar 2013, 20:20 Post subject:
|
|
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: | | ${parameter/pattern/string} |
while for % and # substitution there are mentioned also %% and ##
(GNU Bash‐3.2 2006 September 28 )
|
|
Back to top
|
|
 |
|