Issue
To run my program from Linux command-line I do:
- adb shell
- cd /data/
- ./devprog count
To stop I need to type the letter "T" then hit enter otherwise the devprog keeps running forever. I want to find a way to run the devprog for three seconds and then exit devprog properly by sending the keyboard keys "T + Enter". How can I accomplish this or is it even possible to do with the shell?
I tried the followings:
- timeout 3s adb shell ./data/devprog count
- adb shell ./data/devprog count & timeout 3s | T
I don't know what else to do to run my program for three seconds then exit the command with the keyword combination.
Note: I tried adb shell ./data/devprog count & sleep 3 | xargs adb shell kill. This will stop the program but it will not return any outputs from devporg.
Solution
#!/usr/bin/env bash
adb shell < <(
printf '%s\n' \
'cd /data || exit' \
'exec ./devprog count'
sleep 3
printf '%s\n' T
)
Explaining the nonobvious parts of this:
<(...)is a process substitution; the shell replaces it with a filename that refers to a FIFO from which the output of...can be read.adb shell < <(...)thus runsadb shell, with its input redirected from a process substitution with the embedded code.printf '%s\n' "some text" "some more text"writes two lines, firstsome text, thensome more text. We use this to write the commands we want to unconditionally run.- Using
|| exiton thecdprevents the script from continuing to run if thecdfails, so we don't invoke commands that might behave unpredictably outside their intended environment. - Using
execinexec ./devprog countreplaces the remote shell with a copy of./devprog, so we don't need to worry about what happens if./devprogexits early and puts us back in the shell: with that change made, it's guaranteed that when./devprogexits, so does the remote shell.
Why did we use a process substitution and not a heredoc? That's because a heredoc is fully evaluated before execution starts, so embedding something like $(sleep 3) in a heredoc would delay adb from starting, but it wouldn't create a delay while adb was running.
Answered By - Charles Duffy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.