To `/sdcard/` or `/data/local/tmp/`, see #52.
As most times we are not `root` and it is the equivalent of `~`.
[Benjamin_Loison/adb/issues/51#issuecomment-4431](https://gitea.lemnoslife.com/Benjamin_Loison/adb/issues/51#issuecomment-4431) may help.
```
adb shell -T whoami
```
```
shell
```
```
adb shell -t whoami
```
```
shell
```
```
adb shell -tt whoami
```
```
shell
```
None hangs.
```
echo whoami | adb shell
```
```
shell
```
does not hang.
Command 'expect' not found, but can be installed with:
sudo apt install expect
interact
interact: command not found
spawn
Output:
Command 'spawn' not found, did you mean:
command 'spaln' from deb spaln (2.4.13f+dfsg-1)
Try: sudo apt install <deb name>
adb shell pwd
/
./adb-cmd 'cd /sdcard/'
/usr/bin/env: ‘expect’: No such file or directory
sudo apt install -y expect
Output:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
tcl-expect
The following NEW packages will be installed
expect tcl-expect
0 to upgrade, 2 to newly install, 0 to remove and 7 not to upgrade.
Need to get 247 kB of archives.
After this operation, 538 kB of additional disk space will be used.
Get:1 http://mirrors.ircam.fr/pub/ubuntu/archive noble/universe amd64 tcl-expect amd64 5.45.4-3 [110 kB]
Get:2 http://mirrors.ircam.fr/pub/ubuntu/archive noble/universe amd64 expect amd64 5.45.4-3 [137 kB]
Fetched 247 kB in 0s (622 kB/s)
Selecting previously unselected package tcl-expect:amd64.
(Reading database ... 952710 files and directories currently installed.)
Preparing to unpack .../tcl-expect_5.45.4-3_amd64.deb ...
Unpacking tcl-expect:amd64 (5.45.4-3) ...
Selecting previously unselected package expect.
Preparing to unpack .../expect_5.45.4-3_amd64.deb ...
Unpacking expect (5.45.4-3) ...
Setting up tcl-expect:amd64 (5.45.4-3) ...
Setting up expect (5.45.4-3) ...
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for libc-bin (2.39-0ubuntu8.6) ...
hostname
localhost
./adb-cmd 'hostname'
Output:
spawn adb shell
FP4:/ $ whoami
After a few seconds get the additional output:
hostname
ami
localhost
FP4:/ $ whoami
shell
FP4:/ $
```
adb shell 'bash'
```
<details>
<summary>Output:</summary>
```
whoami
shell
whoami
shell
```
</details>
[The Stack Overflow answer 48565814](https://stackoverflow.com/a/48565814):
```
expect
```
<details>
<summary>Output:</summary>
```
Command 'expect' not found, but can be installed with:
sudo apt install expect
```
</details>
```
interact
```
```
interact: command not found
```
```
spawn
```
<details>
<summary>Output:</summary>
```
Command 'spawn' not found, did you mean:
command 'spaln' from deb spaln (2.4.13f+dfsg-1)
Try: sudo apt install <deb name>
```
</details>
```
adb shell pwd
```
```
/
```
```bash
./adb-cmd 'cd /sdcard/'
```
```
/usr/bin/env: ‘expect’: No such file or directory
```
```
sudo apt install -y expect
```
<details>
<summary>Output:</summary>
```
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
tcl-expect
The following NEW packages will be installed
expect tcl-expect
0 to upgrade, 2 to newly install, 0 to remove and 7 not to upgrade.
Need to get 247 kB of archives.
After this operation, 538 kB of additional disk space will be used.
Get:1 http://mirrors.ircam.fr/pub/ubuntu/archive noble/universe amd64 tcl-expect amd64 5.45.4-3 [110 kB]
Get:2 http://mirrors.ircam.fr/pub/ubuntu/archive noble/universe amd64 expect amd64 5.45.4-3 [137 kB]
Fetched 247 kB in 0s (622 kB/s)
Selecting previously unselected package tcl-expect:amd64.
(Reading database ... 952710 files and directories currently installed.)
Preparing to unpack .../tcl-expect_5.45.4-3_amd64.deb ...
Unpacking tcl-expect:amd64 (5.45.4-3) ...
Selecting previously unselected package expect.
Preparing to unpack .../expect_5.45.4-3_amd64.deb ...
Unpacking expect (5.45.4-3) ...
Setting up tcl-expect:amd64 (5.45.4-3) ...
Setting up expect (5.45.4-3) ...
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for libc-bin (2.39-0ubuntu8.6) ...
```
</details>
```
hostname
```
```
localhost
```
```
./adb-cmd 'hostname'
```
<details>
<summary>Output:</summary>
```
spawn adb shell
FP4:/ $ whoami
```
</details>
<details>
<summary>After a few seconds get the additional output:</summary>
```
hostname
ami
localhost
FP4:/ $ whoami
shell
FP4:/ $
```
</details>
EXPECT(1) General Commands Manual EXPECT(1)
NAME
expect - programmed dialogue with interactive programs, Version 5
SYNOPSIS
expect [ -dDinN ] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]
INTRODUCTION
Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what
can be expected from a program and what the correct response should be. An interpreted language provides branching and high-
level control structures to direct the dialogue. In addition, the user can take control and interact directly when desired, af‐
terward returning control to the script.
...
Expect is generalized so that it can be run as a user-level command with any program and task in mind. Ex‐
pect can actually talk to several programs at the same time.
...
expect [[-opts] pat1 body1] ... [-opts] patn [bodyn]
waits until one of the patterns matches the output of a spawned process, a specified time period has passed, or an end-of-
file is seen. If the final body is empty, it may be omitted.
Patterns from the most recent expect_before command are implicitly used before any other patterns. Patterns from the most
recent expect_after command are implicitly used after any other patterns.
If the arguments to the entire expect statement require more than one line, all the arguments may be "braced" into one so
as to avoid terminating each line with a backslash. In this one case, the usual Tcl substitutions will occur despite the
braces.
If a pattern is the keyword eof, the corresponding body is executed upon end-of-file. If a pattern is the keyword time‐
out, the corresponding body is executed upon timeout. If no timeout keyword is used, an implicit null action is executed
upon timeout. The default timeout period is 10 seconds but may be set, for example to 30, by the command "set timeout
30". An infinite timeout may be designated by the value -1. If a pattern is the keyword default, the corresponding body
is executed upon either timeout or end-of-file.
If a pattern matches, then the corresponding body is executed. expect returns the result of the body (or the empty string
if no pattern matched). In the event that multiple patterns match, the one appearing first is used to select a body.
Each time new output arrives, it is compared to each pattern in the order they are listed. Thus, you may test for absence
of a match by making the last pattern something guaranteed to appear, such as a prompt. In situations where there is no
prompt, you must use timeout (just like you would if you were interacting manually).
Patterns are specified in three ways. By default, patterns are specified as with Tcl's string match command. (Such pat‐
terns are also similar to C-shell regular expressions usually referred to as "glob" patterns). The -gl flag may may be
used to protect patterns that might otherwise match expect flags from doing so. Any pattern beginning with a "-" should
be protected this way. (All strings starting with "-" are reserved for future options.)
For example, the following fragment looks for a successful login. (Note that abort is presumed to be a procedure defined
elsewhere in the script.)
expect {
busy {puts busy\n ; exp_continue}
failed abort
"invalid password" abort
timeout abort
connected
}
Quotes are necessary on the fourth pattern since it contains a space, which would otherwise separate the pattern from the
action. Patterns with the same action (such as the 3rd and 4th) require listing the actions again. This can be avoid by
using regexp-style patterns (see below). More information on forming glob-style patterns can be found in the Tcl manual.
Regexp-style patterns follow the syntax defined by Tcl's regexp (short for "regular expression") command. regexp patterns
are introduced with the flag -re. The previous example can be rewritten using a regexp as:
expect {
busy {puts busy\n ; exp_continue}
-re "failed|invalid password" abort
timeout abort
connected
}
Both types of patterns are "unanchored". This means that patterns do not have to match the entire string, but can begin
and end the match anywhere in the string (as long as everything else matches). Use ^ to match the beginning of a string,
and $ to match the end. Note that if you do not wait for the end of a string, your responses can easily end up in the
middle of the string as they are echoed from the spawned process. While still producing correct results, the output can
look unnatural. Thus, use of $ is encouraged if you can exactly describe the characters at the end of a string.
Note that in many editors, the ^ and $ match the beginning and end of lines respectively. However, because expect is not
line oriented, these characters match the beginning and end of the data (as opposed to lines) currently in the expect
matching buffer. (Also, see the note below on "system indigestion.")
The -ex flag causes the pattern to be matched as an "exact" string. No interpretation of *, ^, etc is made (although the
usual Tcl conventions must still be observed). Exact patterns are always unanchored.
The -nocase flag causes uppercase characters of the output to compare as if they were lowercase characters. The pattern
is not affected.
While reading output, more than 2000 bytes can force earlier bytes to be "forgotten". This may be changed with the func‐
tion match_max. (Note that excessively large values can slow down the pattern matcher.) If patlist is full_buffer, the
corresponding body is executed if match_max bytes have been received and no other patterns have matched. Whether or not
the full_buffer keyword is used, the forgotten characters are written to expect_out(buffer).
If patlist is the keyword null, and nulls are allowed (via the remove_nulls command), the corresponding body is executed
if a single ASCII 0 is matched. It is not possible to match 0 bytes via glob or regexp patterns.
Upon matching a pattern (or eof or full_buffer), any matching and previously unmatched output is saved in the variable ex‐
pect_out(buffer). Up to 9 regexp substring matches are saved in the variables expect_out(1,string) through ex‐
pect_out(9,string). If the -indices flag is used before a pattern, the starting and ending indices (in a form suitable
for lrange) of the 10 strings are stored in the variables expect_out(X,start) and expect_out(X,end) where X is a digit,
corresponds to the substring position in the buffer. 0 refers to strings which matched the entire pattern and is gener‐
ated for glob patterns as well as regexp patterns. For example, if a process has produced output of "abcdefgh\n", the re‐
sult of:
expect "cd"
is as if the following statements had executed:
set expect_out(0,string) cd
set expect_out(buffer) abcd
and "efgh\n" is left in the output buffer. If a process produced the output "abbbcabkkkka\n", the result of:
expect -indices -re "b(b*).*(k+)"
is as if the following statements had executed:
set expect_out(0,start) 1
set expect_out(0,end) 10
set expect_out(0,string) bbbcabkkkk
set expect_out(1,start) 2
set expect_out(1,end) 3
set expect_out(1,string) bb
set expect_out(2,start) 10
set expect_out(2,end) 10
set expect_out(2,string) k
set expect_out(buffer) abbbcabkkkk
and "a\n" is left in the output buffer. The pattern "*" (and -re ".*") will flush the output buffer without reading any
more output from the process.
Normally, the matched output is discarded from Expect's internal buffers. This may be prevented by prefixing a pattern
with the -notransfer flag. This flag is especially useful in experimenting (and can be abbreviated to "-not" for conve‐
nience while experimenting).
The spawn id associated with the matching output (or eof or full_buffer) is stored in expect_out(spawn_id).
The -timeout flag causes the current expect command to use the following value as a timeout instead of using the value of
the timeout variable.
By default, patterns are matched against output from the current process, however the -i flag declares the output from the
named spawn_id list be matched against any following patterns (up to the next -i). The spawn_id list should either be a
whitespace separated list of spawn_ids or a variable referring to such a list of spawn_ids.
For example, the following example waits for "connected" from the current process, or "busy", "failed" or "invalid pass‐
word" from the spawn_id named by $proc2.
expect {
-i $proc2 busy {puts busy\n ; exp_continue}
-re "failed|invalid password" abort
timeout abort
connected
}
The value of the global variable any_spawn_id may be used to match patterns to any spawn_ids that are named with all other
-i flags in the current expect command. The spawn_id from a -i flag with no associated pattern (i.e., followed immedi‐
ately by another -i) is made available to any other patterns in the same expect command associated with any_spawn_id.
The -i flag may also name a global variable in which case the variable is read for a list of spawn ids. The variable is
reread whenever it changes. This provides a way of changing the I/O source while the command is in execution. Spawn ids
provided this way are called "indirect" spawn ids.
Actions such as break and continue cause control structures (i.e., for, proc) to behave in the usual way. The command
exp_continue allows expect itself to continue executing rather than returning as it normally would.
This is useful for avoiding explicit loops or repeated expect statements. The following example is part of a fragment to
automate rlogin. The exp_continue avoids having to write a second expect statement (to look for the prompt again) if the
rlogin prompts for a password.
expect {
Password: {
stty -echo
send_user "password (for $user) on $host: "
expect_user -re "(.*)\n"
send_user "\n"
send "$expect_out(1,string)\r"
stty echo
exp_continue
} incorrect {
send_user "invalid password or account\n"
exit
} timeout {
send_user "connection to $host timed out\n"
exit
} eof {
send_user \
"connection to host failed: $expect_out(buffer)"
exit
} -re $prompt
}
For example, the following fragment might help a user guide an interaction that is already totally automated. In this
case, the terminal is put into raw mode. If the user presses "+", a variable is incremented. If "p" is pressed, several
returns are sent to the process, perhaps to poke it in some way, and "i" lets the user interact with the process, effec‐
tively stealing away control from the script. In each case, the exp_continue allows the current expect to continue pat‐
tern matching after executing the current action.
stty raw -echo
expect_after {
-i $user_spawn_id
"p" {send "\r\r\r"; exp_continue}
"+" {incr foo; exp_continue}
"i" {interact; exp_continue}
"quit" exit
}
By default, exp_continue resets the timeout timer. The timer is not restarted, if exp_continue is called with the -con‐
tinue_timer flag.
...
```bash
man expect
```
<details>
<summary>Output:</summary>
```
EXPECT(1) General Commands Manual EXPECT(1)
NAME
expect - programmed dialogue with interactive programs, Version 5
SYNOPSIS
expect [ -dDinN ] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]
INTRODUCTION
Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what
can be expected from a program and what the correct response should be. An interpreted language provides branching and high-
level control structures to direct the dialogue. In addition, the user can take control and interact directly when desired, af‐
terward returning control to the script.
...
Expect is generalized so that it can be run as a user-level command with any program and task in mind. Ex‐
pect can actually talk to several programs at the same time.
...
expect [[-opts] pat1 body1] ... [-opts] patn [bodyn]
waits until one of the patterns matches the output of a spawned process, a specified time period has passed, or an end-of-
file is seen. If the final body is empty, it may be omitted.
Patterns from the most recent expect_before command are implicitly used before any other patterns. Patterns from the most
recent expect_after command are implicitly used after any other patterns.
If the arguments to the entire expect statement require more than one line, all the arguments may be "braced" into one so
as to avoid terminating each line with a backslash. In this one case, the usual Tcl substitutions will occur despite the
braces.
If a pattern is the keyword eof, the corresponding body is executed upon end-of-file. If a pattern is the keyword time‐
out, the corresponding body is executed upon timeout. If no timeout keyword is used, an implicit null action is executed
upon timeout. The default timeout period is 10 seconds but may be set, for example to 30, by the command "set timeout
30". An infinite timeout may be designated by the value -1. If a pattern is the keyword default, the corresponding body
is executed upon either timeout or end-of-file.
If a pattern matches, then the corresponding body is executed. expect returns the result of the body (or the empty string
if no pattern matched). In the event that multiple patterns match, the one appearing first is used to select a body.
Each time new output arrives, it is compared to each pattern in the order they are listed. Thus, you may test for absence
of a match by making the last pattern something guaranteed to appear, such as a prompt. In situations where there is no
prompt, you must use timeout (just like you would if you were interacting manually).
Patterns are specified in three ways. By default, patterns are specified as with Tcl's string match command. (Such pat‐
terns are also similar to C-shell regular expressions usually referred to as "glob" patterns). The -gl flag may may be
used to protect patterns that might otherwise match expect flags from doing so. Any pattern beginning with a "-" should
be protected this way. (All strings starting with "-" are reserved for future options.)
For example, the following fragment looks for a successful login. (Note that abort is presumed to be a procedure defined
elsewhere in the script.)
expect {
busy {puts busy\n ; exp_continue}
failed abort
"invalid password" abort
timeout abort
connected
}
Quotes are necessary on the fourth pattern since it contains a space, which would otherwise separate the pattern from the
action. Patterns with the same action (such as the 3rd and 4th) require listing the actions again. This can be avoid by
using regexp-style patterns (see below). More information on forming glob-style patterns can be found in the Tcl manual.
Regexp-style patterns follow the syntax defined by Tcl's regexp (short for "regular expression") command. regexp patterns
are introduced with the flag -re. The previous example can be rewritten using a regexp as:
expect {
busy {puts busy\n ; exp_continue}
-re "failed|invalid password" abort
timeout abort
connected
}
Both types of patterns are "unanchored". This means that patterns do not have to match the entire string, but can begin
and end the match anywhere in the string (as long as everything else matches). Use ^ to match the beginning of a string,
and $ to match the end. Note that if you do not wait for the end of a string, your responses can easily end up in the
middle of the string as they are echoed from the spawned process. While still producing correct results, the output can
look unnatural. Thus, use of $ is encouraged if you can exactly describe the characters at the end of a string.
Note that in many editors, the ^ and $ match the beginning and end of lines respectively. However, because expect is not
line oriented, these characters match the beginning and end of the data (as opposed to lines) currently in the expect
matching buffer. (Also, see the note below on "system indigestion.")
The -ex flag causes the pattern to be matched as an "exact" string. No interpretation of *, ^, etc is made (although the
usual Tcl conventions must still be observed). Exact patterns are always unanchored.
The -nocase flag causes uppercase characters of the output to compare as if they were lowercase characters. The pattern
is not affected.
While reading output, more than 2000 bytes can force earlier bytes to be "forgotten". This may be changed with the func‐
tion match_max. (Note that excessively large values can slow down the pattern matcher.) If patlist is full_buffer, the
corresponding body is executed if match_max bytes have been received and no other patterns have matched. Whether or not
the full_buffer keyword is used, the forgotten characters are written to expect_out(buffer).
If patlist is the keyword null, and nulls are allowed (via the remove_nulls command), the corresponding body is executed
if a single ASCII 0 is matched. It is not possible to match 0 bytes via glob or regexp patterns.
Upon matching a pattern (or eof or full_buffer), any matching and previously unmatched output is saved in the variable ex‐
pect_out(buffer). Up to 9 regexp substring matches are saved in the variables expect_out(1,string) through ex‐
pect_out(9,string). If the -indices flag is used before a pattern, the starting and ending indices (in a form suitable
for lrange) of the 10 strings are stored in the variables expect_out(X,start) and expect_out(X,end) where X is a digit,
corresponds to the substring position in the buffer. 0 refers to strings which matched the entire pattern and is gener‐
ated for glob patterns as well as regexp patterns. For example, if a process has produced output of "abcdefgh\n", the re‐
sult of:
expect "cd"
is as if the following statements had executed:
set expect_out(0,string) cd
set expect_out(buffer) abcd
and "efgh\n" is left in the output buffer. If a process produced the output "abbbcabkkkka\n", the result of:
expect -indices -re "b(b*).*(k+)"
is as if the following statements had executed:
set expect_out(0,start) 1
set expect_out(0,end) 10
set expect_out(0,string) bbbcabkkkk
set expect_out(1,start) 2
set expect_out(1,end) 3
set expect_out(1,string) bb
set expect_out(2,start) 10
set expect_out(2,end) 10
set expect_out(2,string) k
set expect_out(buffer) abbbcabkkkk
and "a\n" is left in the output buffer. The pattern "*" (and -re ".*") will flush the output buffer without reading any
more output from the process.
Normally, the matched output is discarded from Expect's internal buffers. This may be prevented by prefixing a pattern
with the -notransfer flag. This flag is especially useful in experimenting (and can be abbreviated to "-not" for conve‐
nience while experimenting).
The spawn id associated with the matching output (or eof or full_buffer) is stored in expect_out(spawn_id).
The -timeout flag causes the current expect command to use the following value as a timeout instead of using the value of
the timeout variable.
By default, patterns are matched against output from the current process, however the -i flag declares the output from the
named spawn_id list be matched against any following patterns (up to the next -i). The spawn_id list should either be a
whitespace separated list of spawn_ids or a variable referring to such a list of spawn_ids.
For example, the following example waits for "connected" from the current process, or "busy", "failed" or "invalid pass‐
word" from the spawn_id named by $proc2.
expect {
-i $proc2 busy {puts busy\n ; exp_continue}
-re "failed|invalid password" abort
timeout abort
connected
}
The value of the global variable any_spawn_id may be used to match patterns to any spawn_ids that are named with all other
-i flags in the current expect command. The spawn_id from a -i flag with no associated pattern (i.e., followed immedi‐
ately by another -i) is made available to any other patterns in the same expect command associated with any_spawn_id.
The -i flag may also name a global variable in which case the variable is read for a list of spawn ids. The variable is
reread whenever it changes. This provides a way of changing the I/O source while the command is in execution. Spawn ids
provided this way are called "indirect" spawn ids.
Actions such as break and continue cause control structures (i.e., for, proc) to behave in the usual way. The command
exp_continue allows expect itself to continue executing rather than returning as it normally would.
This is useful for avoiding explicit loops or repeated expect statements. The following example is part of a fragment to
automate rlogin. The exp_continue avoids having to write a second expect statement (to look for the prompt again) if the
rlogin prompts for a password.
expect {
Password: {
stty -echo
send_user "password (for $user) on $host: "
expect_user -re "(.*)\n"
send_user "\n"
send "$expect_out(1,string)\r"
stty echo
exp_continue
} incorrect {
send_user "invalid password or account\n"
exit
} timeout {
send_user "connection to $host timed out\n"
exit
} eof {
send_user \
"connection to host failed: $expect_out(buffer)"
exit
} -re $prompt
}
For example, the following fragment might help a user guide an interaction that is already totally automated. In this
case, the terminal is put into raw mode. If the user presses "+", a variable is incremented. If "p" is pressed, several
returns are sent to the process, perhaps to poke it in some way, and "i" lets the user interact with the process, effec‐
tively stealing away control from the script. In each case, the exp_continue allows the current expect to continue pat‐
tern matching after executing the current action.
stty raw -echo
expect_after {
-i $user_spawn_id
"p" {send "\r\r\r"; exp_continue}
"+" {incr foo; exp_continue}
"i" {interact; exp_continue}
"quit" exit
}
By default, exp_continue resets the timeout timer. The timer is not restarted, if exp_continue is called with the -con‐
tinue_timer flag.
...
```
</details>
DuckDuckGo and Google search *\$ vs # shell*, *\$ vs # shell user vs root* and *shell prefix user vs root*.
[The Unix Stack Exchange answer 291733](https://unix.stackexchange.com/a/291733) helps.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
To
/sdcard/or/data/local/tmp/, see #52.As most times we are not
rootand it is the equivalent of~.Benjamin_Loison/adb/issues/51#issuecomment-4431 may help.
None hangs.
does not hang.
DuckDuckGo search Linux SSH interactive but execute a command first.
I don't want
~/.bashrclike solutions.Shell:
I don't want having to
exittwice.Output:
The Stack Overflow answer 48565814:
Output:
Output:
Output:
Output:
After a few seconds get the additional output:
Output:
Output:
Changing
#with$.Output:
DuckDuckGo and Google search
Linux "expect" comment.Specifying a comment as
#seems to work fine.Output:
I added the Stack Overflow comment 140838674.
Output:
I posted the Stack Overflow comment 140838679.
DuckDuckGo and Google search $ vs # shell, $ vs # shell user vs root and shell prefix user vs root.
The Unix Stack Exchange answer 291733 helps.
Output:
Set default folder to `/sdcard/`to Set default folder