Code not working in init script of initrd.gz. (Solved)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

Code not working in init script of initrd.gz. (Solved)

#1 Post by LazY Puppy »

Hi.

I got some code used to change the current wallpaper at boot up.

When executing this code from within a script in /root/Startup, it works as it is supposed to do.

But when inserting into the init script of the initrd.gz, it causes a Kernel Panic!

Here is the code (snippet) (the Line that causes Kernel Panic is marked up):
read OLDWP_LINE < /etc/Puppybackgroundpicture-orig
OLDWP=$(echo $OLDWP_LINE|cut -d' ' -f2)
echo -n "$OLDWP" > /tmp/Puppybackgroundpicture
read OLDWP < /tmp/Puppybackgroundpicture
nlen="`echo ${#OLDWP}`"
OLDWP2=${OLDWP:1:nlen-2}
NEWWP2="/usr/share/backgrounds/$MYWALLPAPER"
As soon as I'm commenting out the Line OLDWP2=${OLDWP:1:nlen-2} there is NO Kernel Panic and the Puppy boots straight up - as is should do. But the script (function) won't work then.

Any hints?
Last edited by LazY Puppy on Fri 05 Dec 2014, 01:38, edited 1 time in total.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#2 Post by amigo »

During early boot there is no desktop yet and hence no wallpaper to change.

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#3 Post by LazY Puppy »

amigo wrote:During early boot there is no desktop yet and hence no wallpaper to change.
No, you just did not understand the code.

I don't change a wallpaper at this stage of booting. Just collecting some information and echoing some of them refined for a later use.

This: OLDWP2=${OLDWP:1:nlen-2} is the line causing the kernel panic and it has nothing to do with a wallpaper!

Please read again my opening post as well as the quoted code and probably post a useful solution.

Thanks.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#4 Post by LazY Puppy »

Probably some more detailed information will make it much more clear to understand what I am doing.

When the init script comes to the point where it is echoing Performing a 'switch_root' to the layered filesystem... I do a copy of /pup_new/etc/Puppybackgroundpicture.

Code: Select all

cp -af /pup_new/etc/Puppybackgroundpicture /pup_new/etc/Puppybackgroundpicture-orig
Content of File /pup_new/etc/Puppybackgroundpicture is: -stretch "/usr/share/backgrounds/default.jpg" Nothing

I grab the BG image with this code:

Code: Select all

read OLDWP_LINE < /pup_new/etc/Puppybackgroundpicture
OLDWP=$(echo $OLDWP_LINE|cut -d' ' -f2)
which returns to me "/usr/share/backgrounds/default.jpg"

Then echoing the result to a different temp file:

Code: Select all

echo -n "$OLDWP" > /tmp/Puppybackgroundpicture
After this I'm reading out the original wallpaper and changing the content of /pup_new/etc/Puppybackgroundpicture

Code: Select all

read OLDWP < /tmp/Puppybackgroundpicture
NEWWP='"/usr/share/backgrounds/'$MYWALLPAPER'"'
sed -i 's|'"$OLDWP"'|'"$NEWWP"'|' /pup_new/etc/Puppybackgroundpicture #>/dev/null
to get the new wallpaper into /pup_new/etc/Puppybackgroundpicture.

But to get the new wallpaper also into /pup_new/root/Choices/ROX-Filer/PuppyPin I need to get rid of the double quotes (") at the beginning and end of "/usr/share/backgrounds/default.jpg".

This is to be done by the line causing the Kernel Panic: OLDWP2=${OLDWP:1:nlen-2}

The exactly equal code (without the /pup_new) is working from a normal script after X desktop appeared on the screen.

Again, this is causing the Kernel Panic: OLDWP2=${OLDWP:1:nlen-2}

No wallpaper changed or even touched at this stage of booting. Only collecting and echoing information. No desktop needed at all to do such collecting and echoing of information.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#5 Post by greengeek »

LazY Puppy wrote:This: OLDWP2=${OLDWP:1:nlen-2} is the line causing the kernel panic and it has nothing to do with a wallpaper!
I'm bad at coding but I'm going to take a stab in the dark:
Shouldnt there ba a $ sign directly in front of the OLDWP?
OLDWP2=${$OLDWP:1:nlen-2}

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#6 Post by LazY Puppy »

No, this gives a bad substitution.
Terminal Output wrote:line 14: ${$OLDWP:1:nlen-2}: bad substitution
Also this:
Terminal Output wrote:line 14: ${$OLDWP:1:$nlen-2}: bad substitution
Edit:

:idea: Probably I should try just to sed the double quotes in temp file, /tmp/Puppybackgroundpicture. :idea:
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#7 Post by seaside »

My guess is that the bash shell is not being used at the start....
That is a "bashism" parameter expansion.

Cheers,
s

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#8 Post by LazY Puppy »

Ha,

in Germany we do have a saying that goes:

Man sieht den Wald vor lauter Bäumen nicht.

which could be translated like:

You can't watch the forest because of too many trees. :lol:

Yes, sed did the job and it works now as it should have done already.
seaside wrote:My guess is that the bash shell is not being used at the start....
That is a "bashism" parameter expansion.
I already feared there must be a difference between the #!/bin/sh used in the init script and the #!/bin/sh used later in the scripts, when the desktop has appeared. Just could not imagine or explain to myself.

Thanks for all the help.

Solved!
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

Sylvander
Posts: 4416
Joined: Mon 15 Dec 2008, 11:06
Location: West Lothian, Scotland, UK

#9 Post by Sylvander »

LazY Puppy wrote:You can't watch the forest because of too many trees. :lol:
Is that similar to, or the same as, the following English saying:
He/I/they "cannot see the wood for the trees"?

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#10 Post by LazY Puppy »

Sylvander wrote:
LazY Puppy wrote:You can't watch the forest because of too many trees. :lol:
Is that similar to, or the same as, the following English saying:
He/I/they "cannot see the wood for the trees"?
Exactly!

Ok, I got a deeper look into that.

In initrd.gz /bin/sh is a symbolic link to /bin/busybox.

In puppy_precise_5.7.1.sfs /bin/sh is a symbolic link to /bin/bash.

That's just all, but makes a HUGE difference in use.

I assume after the switch_root is executed the symbolic link to /bin/busybox is not longer existing and #!/bin/sh will work like #!/bin/bash.

Btw: almost everything in /bin inside of the initrd.gz is a symbolic link to /bin/busybox.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#11 Post by amigo »

Yeah, I was gonna suggest that the shebang was causing the problem. You could try:

Code: Select all

echo ${OLDWP:1:$((nlen - 2))}

User avatar
MinHundHettePerro
Posts: 852
Joined: Thu 05 Feb 2009, 22:22
Location: SE

#12 Post by MinHundHettePerro »

Perhaps jumping in a bit late ...
To just get rid of all the double-quotes in BB-ash (or in Bash)

Code: Select all

OLDWP2=${OLDWP//\"/}
FWIW :)/ MHHP
[color=green]Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285[/color]
Slackos & 214X, ... and Q6xx
[color=darkred]Nämen, vaf....[/color] [color=green]ln -s /dev/null MHHP[/color]

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#13 Post by LazY Puppy »

MinHundHettePerro wrote:Perhaps jumping in a bit late ...
To just get rid of all the double-quotes in BB-ash (or in Bash)

Code: Select all

OLDWP2=${OLDWP//"/}
FWIW :)/ MHHP
I will try this, but I will never ever understand things like: //"/ :lol:
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#14 Post by technosaurus »

Code: Select all

OLDWP2=${OLDWP:1:nlen-2} #<-WTF is that?
during bootup sh is /bin/ash but I can't believe that works in bash
Were you trying for:

Code: Select all

OLDWP2=${OLDWP:1:$((nlen-2))}
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#15 Post by LazY Puppy »

technosaurus wrote:

Code: Select all

OLDWP2=${OLDWP:1:nlen-2} #<-WTF is that?
during bootup sh is /bin/ash but I can't believe that works in bash
Were you trying for:

Code: Select all

OLDWP2=${OLDWP:1:$((nlen-2))}
From where did you quoted/coded this?

I did never write or quote: OLDWP2=${OLDWP:1:nlen-2} #, all my codes are without the #. Unless I might be blind, so w(here)tf did you get this?

This code: OLDWP2=${OLDWP:1:nlen-2} is working for me in approximately 20 to 30 (or even more) of my bash script applications. I usually start them with:

Code: Select all

#!/bin/bash -a
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#16 Post by technosaurus »

LazY Puppy wrote:I did never write or quote: OLDWP2=${OLDWP:1:nlen-2} #, all my codes are without the #. Unless I might be blind, so w(here)tf did you get this?
That was my comment, you can't just go throwing expressions around like that and expect it to work in a posix compliant shell. (Ubuntu had a page called "/bin/sh is dash" or something with a lot of portability tips)
For portability, whenever you do math, either use expr or enclose it in $(()) ... and for maximum portability, spell out things like i=$((i+1)) rather than simply doing i++, because even that is not widely supported.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#17 Post by LazY Puppy »

That was my comment,
Ok, I see now.

Since the '<-' did point to the '#' I just did not understand that you were made this comment.

Explanation how I do read forums '[ quote ]' (1) and forums '[ code ]' (2):

- I'm reading (1) as 'quoted text' besides
- I'm reading (2) as 'quoted (already existing) code'
you can't just go throwing expressions around like that and expect it to work in a posix compliant shell. (Ubuntu had a page called "/bin/sh is dash" or something with a lot of portability tips)
I found this in my early days when creating one of the first version of LazY Puppy and it has worked for me in all of my several LazY Puppy Systems (e.g. derivatives of Lucid 528, Precise 56 & 571, Three Headed Dog, MacPup 528, LxPup 12.x and Carolina 1.2) until I was stumbling over the issue in init script, which was the first time this didn't work. If I remember correctly this came out of a script by SFR, who is known to me as a more expert in bash than myself ever will be. So there was no reason to me why NOT to trust and NOT to use it.
For portability, whenever you do math, either use expr or enclose it in $(()) ... and for maximum portability, spell out things like i=$((i+1)) rather than simply doing i++, because even that is not widely supported.
When it comes to increase or decrease contents of a variable I usually use this:

Code: Select all

i=$(( i + 1 ))
instead of doing i++, since I've stumbled a few times and found this not working. Sometimes I used combinations of i=$(( i + 1 )) and i++ when i++ seems not to work. :lol:

So compatibility.

What for compatibility?
Where might this conflict (I 'develop' for Puppy only, not for other OS that is using bash)?

By this now it's just proven once again that I'm NOT a programmer and rather being a Hacker having fun in Puppy Linux.

How shall I ever getting able to perform patches for WoofCE and getting some of my -hopefully somehow innovative work- into WoofCE or just a newer Puppy, with such a less amount of Bash knowledge? :(

Btw: I have made a quick search for :nlen in /usr/local (where I put my bash applications usually) of my LazY Puppy and found 36 files where this is used. Since I do have also stuff in /usr/sbin, /usr/bin, /etc/xdg/openbox and /root/my-applications this number may increase up to 50 files - or even more. I have used this also like :flen or :bnlen, so it might increase once again.

Should I correct this in all my bash script applications?

Thanks for increasing my knowledge! :D
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#18 Post by greengeek »


User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#19 Post by don570 »

This page describes converting a bash script to dash

http://mywiki.wooledge.org/Bashism

______________________________________

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#20 Post by technosaurus »

LazY Puppy wrote:Should I correct this in all my bash script applications?
I wouldn't bother unless you want them to run ~8 times faster than with bash 4.x, just change the shabang to #!/bin/bash so that if anyone does want to use it with another shell (or code from it), they are hinted that they may have to do a bash-ectome. I normally put #!/bin/ash as the shabang while I am testing; then change it to #!/bin/sh when finished (not perfect, since ash does support some bashisms)
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply