FORUMS
Remove All Ads from XDA

[TOOLS][ZIPS][SCRIPTS] osm0sis' Odds and Ends [Multiple Devices/Platforms]

13,695 posts
Thanks Meter: 30,276
 
By osm0sis, Recognized Developer / Recognized Contributor on 18th April 2013, 12:37 AM
Post Reply Email Thread
26th August 2017, 03:12 PM |#1441  
Recognized Contributor
Thanks Meter: 3,236
 
More
Quote:
Originally Posted by ale5000

Hi,
I know shell scripts a bit but I find difficult to understand what these lines do.

Can you please explain them?

Ultimately what they do is look to see if the zygote process is running. If yes set BOOTMODE to true. If no set it to false.

ps is the 'process status' command. It returns a list of running processes with information about them, for purposes here only the name is relevant.

Sidenote: the busybox version of ps (which is what runs within TWRP) is more full featured than the toybox version. One example - toybox version won't accept the '-A' parameter. I think the only thing it does accept is a pid.

For more information on ps check Google. Try 'ps command Linux' or 'ps man page'. For now just know it returns a list of processes, by default ones which belong to the user running the command.

So the output from ps is passed to grep which checks the output for 'zygote' (which is the "master" process when you boot), this output is then passed to fgrep again to remove any lines with 'grep' in them.

Next the return code from the grep is checked. If return code is 0 (&& test) set BOOTMODE to true. If not 0 (|| test) set to false.

The second line starts by testing the value of BOOTMODE. If it's false it runs the 'ps -A' command, tests further for zygote, sets to true if found.

As noted above this second line won't really do anything because 'ps -A' will throw an error in a booted system since toybox will reject it. But what I described is the intent.

Hope this helps. If you have a specific question about part of this then ask.
The Following 5 Users Say Thank You to jcmm11 For This Useful Post: [ View ] Gift jcmm11 Ad-Free
26th August 2017, 09:00 PM |#1442  
Member
Thanks Meter: 40
 
More
Hey!

Was just wondering if it is possible to get the BusyBox Installer Script (Magisk) to install Busybox to su/xbin or su/bin instead of system/xbin.
26th August 2017, 10:30 PM |#1443  
Senior Member
Thanks Meter: 1,012
 
Donate to Me
More
@jcmm11

I now undestand but there is a point that I miss about this line:
Code:
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true
Shell doesn't really understand true/false as text so "$BOOTMODE" will always evaluate to true, but it would work doing this:
Code:
[ $BOOTMODE == false ] && ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true
Am I miss something or is there an error?
26th August 2017, 11:12 PM |#1444  
Recognized Contributor
Thanks Meter: 3,236
 
More
Quote:
Originally Posted by ale5000

. . .
Shell doesn't really understand true/false as text
. . .

Are you sure about that 🤔
Code:
bullhead:/ $
bullhead:/ $ var1=true
bullhead:/ $ var2=false
bullhead:/ $ $var1&&echo yes||echo no
yes
bullhead:/ $ $var2&&echo yes||echo no
no
bullhead:/ $
true and false are built-in shell constants. They have to be all lower case. Neither True nor TRUE would work the same way.
The Following 3 Users Say Thank You to jcmm11 For This Useful Post: [ View ] Gift jcmm11 Ad-Free
26th August 2017, 11:37 PM |#1445  
Senior Member
Thanks Meter: 1,012
 
Donate to Me
More
Quote:
Originally Posted by jcmm11

Are you sure about that 🤔

Code:
bullhead:/ $
bullhead:/ $ var1=true
bullhead:/ $ var2=false
bullhead:/ $ $var1&&echo yes||echo no
yes
bullhead:/ $ $var2&&echo yes||echo no
no
bullhead:/ $
true and false are built-in shell constants. They have to be all lower case. Neither True nor TRUE would work the same way.

I was wrong but there is one thing that can be improved, it set BOOTMODE=true even when it is already true.

It can be fixed by this:
Code:
$BOOTMODE || (ps 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true)
26th August 2017, 11:59 PM |#1446  
Recognized Contributor
Thanks Meter: 3,236
 
More
Quote:
Originally Posted by ale5000

Are you sure about that 🤔
I was wrong but there is one thing that can be improved, it set BOOTMODE=true even when it is already true.

It can be fixed by this:

Couple of points here. First, if you look at the 2nd line I think you'll see it already starts with the || test.
Second, as I said in the original explanation, I don't think the second line gets you anything since toybox will throw an error on ps -A and TWRP won't have zygote. So I'd just drop the second line completely.
But it's also not my code 😉 (and I may have missed something myself)
The Following User Says Thank You to jcmm11 For This Useful Post: [ View ] Gift jcmm11 Ad-Free
27th August 2017, 12:02 AM |#1447  
Senior Member
Thanks Meter: 1,012
 
Donate to Me
More
Quote:
Originally Posted by jcmm11

Couple of points here. First, if you look at the 2nd line I think you'll see it already starts with the || test.
Second, as I said in the original explanation, I don't think the second line gets you anything since toybox will throw an error on ps -A and TWRP won't have zygote. So I'd just drop the second line completely.
But it's also not my code (and I may have missed something myself)

I don't know if it is needed but you can see that by running this:
Code:
$BOOTMODE || echo '1' && echo '2'
2 is always shown with both true/false.
27th August 2017, 12:20 AM |#1448  
Recognized Contributor
Thanks Meter: 3,236
 
More
Quote:
Originally Posted by ale5000

I don't know if it is needed but you can see that by running this:

2 is always shown with both true/false.

If you're going to include both tests then && has to come first. If only one test you can just use ||
That's just how it works
27th August 2017, 12:32 AM |#1449  
Senior Member
Thanks Meter: 1,012
 
Donate to Me
More
Quote:
Originally Posted by jcmm11

If you're going to include both tests then && has to come first. If only one test you can just use ||
That's just how it works

Generally yes, but not in this case where && apply (if I undestand the original idea) only to second clause.

The line
Code:
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true;
in @osm0sis post is conceptually wrong despite doesn't causing any problem.
27th August 2017, 01:00 AM |#1450  
Recognized Contributor
Thanks Meter: 3,236
 
More
Quote:
Originally Posted by ale5000

Generally yes, but not in this case where && apply (if I undestand the original idea) only to second clause.

The line in @osm0sis post is conceptually wrong despite doesn't causing any problem.

Code:
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true;
Right. In this case it's fine because of intermediate clauses. Conceptually it's also fine. Let's break it down.
Test BOOTMODE
If it's true we're done. Nothing more to do.
If it's false we run ps again, this time telling it to give us all the running processes. (First time we didn't use -A so it's a restricted listing. This still assumes no toybox, but I've beat that horse enough)
We, once again, pass the output to grep filtering for zygote and removing grep. If we still have output after that (return code of 0 from the last grep) that means we found a running zygote instance and we set BOOTMODE to true.
Conceptually this is fine. If the first check didn't find zygote we check again against the full process list. I don't see a conceptual problem here.
The Following User Says Thank You to jcmm11 For This Useful Post: [ View ] Gift jcmm11 Ad-Free
27th August 2017, 02:32 AM |#1451  
Senior Member
Thanks Meter: 1,012
 
Donate to Me
More
Quote:
Originally Posted by jcmm11

Code:
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true;
Right. In this case it's fine because of intermediate clauses. Conceptually it's also fine. Let's break it down.
Test BOOTMODE
If it's true we're done. Nothing more to do.
If it's false we run ps again, this time telling it to give us all the running processes. (First time we didn't use -A so it's a restricted listing. This still assumes no toybox, but I've beat that horse enough)
We, once again, pass the output to grep filtering for zygote and removing grep. If we still have output after that (return code of 0 from the last grep) that means we found a running zygote instance and we set BOOTMODE to true.
Conceptually this is fine. If the first check didn't find zygote we check again against the full process list. I don't see a conceptual problem here.

IF BOOTMODE is already true it doesn't check again but it still set BOOTMODE=true.
It doesn't make any difference but it set it to true again despite being senseless.

Try and see the text displayed:
Code:
BOOTMODE=true
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && echo 'true again'
The Following User Says Thank You to ale5000 For This Useful Post: [ View ] Gift ale5000 Ad-Free
Post Reply Subscribe to Thread

Tags
automation, batch, flashable zip, script, tool

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes