The issue is due to how the script test.sh is structured and how input/output is handled in Bash. Let's break it down:
Problem Analysis
adb shell exit:
This command attempts to exit an adb shell session. If there is no active adb shell session, this command will fail silently (or with an error, depending on your environment).
However, it does not terminate the script itself. The script continues to the next line.
read test:
The read command waits for user input from stdin (standard input).
In your case, you are piping echo hey into the script (echo hey | ./test.sh), so the script receives hey as input for the read command.
However, the read command consumes the entire input (hey), but the script does not explicitly output anything after reading it.
echo $test:
This line prints the value of the variable test, which was set by the read command.
However, the output of echo $test is not being captured or displayed in your terminal because the script's output is not being explicitly directed to stdout in a way that is visible in this context.
Why No Output?
The script reads the input (hey) into the variable test, but the echo $test output is not being displayed because:
The script is being executed in a subshell (due to the pipe |), and the output of echo $test is not being passed back to the terminal.
The script does not explicitly return or print anything to stdout in a way that is visible when run via a pipe.
How to Fix It
If you want the script to output the value of test when run with echo hey | ./test.sh, you need to ensure the script explicitly prints the output to stdout. For example:
bash
#!/bin/bash
adb shell exit
read test
echo "$test" # Explicitly print the value of $test
Now, running:
bash
echo hey | ./test.sh
will output:
text
hey
Key Takeaway
The script was not explicitly printing the output of echo $test in a way that was visible when run via a pipe.
The read command consumed the input, but the script did not explicitly return or display the output.
Jul 18, 6:47pm
This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.