Shell Script Running adb commands break the while loop [duplicate]
This question already has an answer here:
Why does ADB commands break a bash script loop?
2 answers
I am writing a bin bash shell script to collect the information from multiple devices at once. However I notice if there is any adb commands executed in the while loop will break it. Not sure why as the loop is clearly not complete. I know there are multiple feeds discussing this topic but I am unable to find similar issue like my one. Any advise will be helpful. Thank you.
#!/bin/bash
echo "# Collecting attached devices under adb and record the project name..."
adb devices | while read line
do
if [ "$line" != "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
then
device_sn=`echo $line | awk '{print $1}'`
project_name=`adb -s $device_sn shell getprop ro.product.device` #This line will break the while loop. If I remove it I can have all connected devices reboot to bootloader
echo "$project_name: $device_sn is rebooting to bootloader"
adb -s $device_sn reboot bootloader
fi
done
android bash shell adb
marked as duplicate by Alex P.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 11:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does ADB commands break a bash script loop?
2 answers
I am writing a bin bash shell script to collect the information from multiple devices at once. However I notice if there is any adb commands executed in the while loop will break it. Not sure why as the loop is clearly not complete. I know there are multiple feeds discussing this topic but I am unable to find similar issue like my one. Any advise will be helpful. Thank you.
#!/bin/bash
echo "# Collecting attached devices under adb and record the project name..."
adb devices | while read line
do
if [ "$line" != "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
then
device_sn=`echo $line | awk '{print $1}'`
project_name=`adb -s $device_sn shell getprop ro.product.device` #This line will break the while loop. If I remove it I can have all connected devices reboot to bootloader
echo "$project_name: $device_sn is rebooting to bootloader"
adb -s $device_sn reboot bootloader
fi
done
android bash shell adb
marked as duplicate by Alex P.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 11:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does ADB commands break a bash script loop?
2 answers
I am writing a bin bash shell script to collect the information from multiple devices at once. However I notice if there is any adb commands executed in the while loop will break it. Not sure why as the loop is clearly not complete. I know there are multiple feeds discussing this topic but I am unable to find similar issue like my one. Any advise will be helpful. Thank you.
#!/bin/bash
echo "# Collecting attached devices under adb and record the project name..."
adb devices | while read line
do
if [ "$line" != "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
then
device_sn=`echo $line | awk '{print $1}'`
project_name=`adb -s $device_sn shell getprop ro.product.device` #This line will break the while loop. If I remove it I can have all connected devices reboot to bootloader
echo "$project_name: $device_sn is rebooting to bootloader"
adb -s $device_sn reboot bootloader
fi
done
android bash shell adb
This question already has an answer here:
Why does ADB commands break a bash script loop?
2 answers
I am writing a bin bash shell script to collect the information from multiple devices at once. However I notice if there is any adb commands executed in the while loop will break it. Not sure why as the loop is clearly not complete. I know there are multiple feeds discussing this topic but I am unable to find similar issue like my one. Any advise will be helpful. Thank you.
#!/bin/bash
echo "# Collecting attached devices under adb and record the project name..."
adb devices | while read line
do
if [ "$line" != "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
then
device_sn=`echo $line | awk '{print $1}'`
project_name=`adb -s $device_sn shell getprop ro.product.device` #This line will break the while loop. If I remove it I can have all connected devices reboot to bootloader
echo "$project_name: $device_sn is rebooting to bootloader"
adb -s $device_sn reboot bootloader
fi
done
This question already has an answer here:
Why does ADB commands break a bash script loop?
2 answers
android bash shell adb
android bash shell adb
edited Nov 21 at 6:50
rvazquezglez
9641123
9641123
asked Nov 21 at 6:01
Lien Shawn
32
32
marked as duplicate by Alex P.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 11:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Alex P.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 11:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I'm guessing here, but it might be that the adb
command is reading the input that's intended for while read
. This is a fairly common problem with the while read
type loop, and the symptoms you describe match. One possibility is to redirect input on the internal command so it can't read from the unintended source, like this:
project_name=`adb -s $device_sn shell getprop ro.product.device` </dev/null
But my favorite solution to this is to pass the input to the loop over something other than standard input by changing the while read
loop like this:
while read line <&3
do
# normal loop contents here...
done 3< <(adb devices)
Those redirects pass input over file descriptor #3 (#0 is standard input, #1 is standard output, #2 is standard error, #3 is normally unused). With this form, you don't have to know which commands inside the loop might read from standard input, so no surprises (and if they do need to read from the Terminal, they can!). Also, unlike a standard pipe, this runs the loop in the main shell process rather than a subshell, avoiding a number of oddities (like variables set in the loop not being available after the loop).
There's one disadvantage, though: process substitution, <( )
, is a bash-only feature. It's not even available in bash when it's run under the name sh
! So you must use a bash shebang in the script (#!/bin/bash
or #!/usr/bin/env bash
), and don't override that by running the script with sh
!
The first suggestion still break the while loop, but your favorite solution works! Thank you!
– Lien Shawn
Nov 21 at 10:02
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm guessing here, but it might be that the adb
command is reading the input that's intended for while read
. This is a fairly common problem with the while read
type loop, and the symptoms you describe match. One possibility is to redirect input on the internal command so it can't read from the unintended source, like this:
project_name=`adb -s $device_sn shell getprop ro.product.device` </dev/null
But my favorite solution to this is to pass the input to the loop over something other than standard input by changing the while read
loop like this:
while read line <&3
do
# normal loop contents here...
done 3< <(adb devices)
Those redirects pass input over file descriptor #3 (#0 is standard input, #1 is standard output, #2 is standard error, #3 is normally unused). With this form, you don't have to know which commands inside the loop might read from standard input, so no surprises (and if they do need to read from the Terminal, they can!). Also, unlike a standard pipe, this runs the loop in the main shell process rather than a subshell, avoiding a number of oddities (like variables set in the loop not being available after the loop).
There's one disadvantage, though: process substitution, <( )
, is a bash-only feature. It's not even available in bash when it's run under the name sh
! So you must use a bash shebang in the script (#!/bin/bash
or #!/usr/bin/env bash
), and don't override that by running the script with sh
!
The first suggestion still break the while loop, but your favorite solution works! Thank you!
– Lien Shawn
Nov 21 at 10:02
add a comment |
I'm guessing here, but it might be that the adb
command is reading the input that's intended for while read
. This is a fairly common problem with the while read
type loop, and the symptoms you describe match. One possibility is to redirect input on the internal command so it can't read from the unintended source, like this:
project_name=`adb -s $device_sn shell getprop ro.product.device` </dev/null
But my favorite solution to this is to pass the input to the loop over something other than standard input by changing the while read
loop like this:
while read line <&3
do
# normal loop contents here...
done 3< <(adb devices)
Those redirects pass input over file descriptor #3 (#0 is standard input, #1 is standard output, #2 is standard error, #3 is normally unused). With this form, you don't have to know which commands inside the loop might read from standard input, so no surprises (and if they do need to read from the Terminal, they can!). Also, unlike a standard pipe, this runs the loop in the main shell process rather than a subshell, avoiding a number of oddities (like variables set in the loop not being available after the loop).
There's one disadvantage, though: process substitution, <( )
, is a bash-only feature. It's not even available in bash when it's run under the name sh
! So you must use a bash shebang in the script (#!/bin/bash
or #!/usr/bin/env bash
), and don't override that by running the script with sh
!
The first suggestion still break the while loop, but your favorite solution works! Thank you!
– Lien Shawn
Nov 21 at 10:02
add a comment |
I'm guessing here, but it might be that the adb
command is reading the input that's intended for while read
. This is a fairly common problem with the while read
type loop, and the symptoms you describe match. One possibility is to redirect input on the internal command so it can't read from the unintended source, like this:
project_name=`adb -s $device_sn shell getprop ro.product.device` </dev/null
But my favorite solution to this is to pass the input to the loop over something other than standard input by changing the while read
loop like this:
while read line <&3
do
# normal loop contents here...
done 3< <(adb devices)
Those redirects pass input over file descriptor #3 (#0 is standard input, #1 is standard output, #2 is standard error, #3 is normally unused). With this form, you don't have to know which commands inside the loop might read from standard input, so no surprises (and if they do need to read from the Terminal, they can!). Also, unlike a standard pipe, this runs the loop in the main shell process rather than a subshell, avoiding a number of oddities (like variables set in the loop not being available after the loop).
There's one disadvantage, though: process substitution, <( )
, is a bash-only feature. It's not even available in bash when it's run under the name sh
! So you must use a bash shebang in the script (#!/bin/bash
or #!/usr/bin/env bash
), and don't override that by running the script with sh
!
I'm guessing here, but it might be that the adb
command is reading the input that's intended for while read
. This is a fairly common problem with the while read
type loop, and the symptoms you describe match. One possibility is to redirect input on the internal command so it can't read from the unintended source, like this:
project_name=`adb -s $device_sn shell getprop ro.product.device` </dev/null
But my favorite solution to this is to pass the input to the loop over something other than standard input by changing the while read
loop like this:
while read line <&3
do
# normal loop contents here...
done 3< <(adb devices)
Those redirects pass input over file descriptor #3 (#0 is standard input, #1 is standard output, #2 is standard error, #3 is normally unused). With this form, you don't have to know which commands inside the loop might read from standard input, so no surprises (and if they do need to read from the Terminal, they can!). Also, unlike a standard pipe, this runs the loop in the main shell process rather than a subshell, avoiding a number of oddities (like variables set in the loop not being available after the loop).
There's one disadvantage, though: process substitution, <( )
, is a bash-only feature. It's not even available in bash when it's run under the name sh
! So you must use a bash shebang in the script (#!/bin/bash
or #!/usr/bin/env bash
), and don't override that by running the script with sh
!
answered Nov 21 at 7:37
Gordon Davisson
67.6k97793
67.6k97793
The first suggestion still break the while loop, but your favorite solution works! Thank you!
– Lien Shawn
Nov 21 at 10:02
add a comment |
The first suggestion still break the while loop, but your favorite solution works! Thank you!
– Lien Shawn
Nov 21 at 10:02
The first suggestion still break the while loop, but your favorite solution works! Thank you!
– Lien Shawn
Nov 21 at 10:02
The first suggestion still break the while loop, but your favorite solution works! Thank you!
– Lien Shawn
Nov 21 at 10:02
add a comment |