[fx][emu] Fix terminal check script for "stty sane".

Previously we check if the shell contains option "-i" to determine
whether the script is running in interactive mode, and only reset
the terminal if script runs in interactive mode.

However when we run "fx emu", though the script is running in non-
interactive mode (because we spawn a new shell, instead of sourcing
the script in the interactive shell), the input and output are both
to a terminal; so the terminal still could be broken by QEMU, and
thus we still need a "stty sane" command after QEMU quits.

We fix this by changing the teriminal check script: we use "-t"
test operator to check if stdin is associated with a terminal
and only do stty sane when it is true.

TEST=fx emu (stty sane called)
     fx emu-remote (stty sane not called; no error messages).

Change-Id: Ie8ed877d7fe5db699e8b79e225bf53aa8773df47
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/453516
Reviewed-by: Wayne Piekarski <waynepie@google.com>
Reviewed-by: Renato Mangini Dias <mangini@google.com>
Testability-Review: Wayne Piekarski <waynepie@google.com>
Commit-Queue: Yilong Li <liyl@google.com>
diff --git a/tools/devshell/emu b/tools/devshell/emu
index f7f0180..6142bd0 100755
--- a/tools/devshell/emu
+++ b/tools/devshell/emu
@@ -458,7 +458,10 @@
   fx-error "Failed to create temporary directory"
   exit 1
 fi
-trap 'rm -Rf "${img_dir}"; [[ "${GRPCWEBPROXY_PID}" ]] && kill "${GRPCWEBPROXY_PID}"; [[ $- == *i* ]] && stty sane' EXIT
+
+# Restore the terminal only if the standard input (fd 0) is associated with a
+# terminal device. We check this using test operator "-t".
+trap 'rm -Rf "${img_dir}"; [[ "${GRPCWEBPROXY_PID}" ]] && kill "${GRPCWEBPROXY_PID}"; [[ -t 0 ]] && stty sane' EXIT
 
 KERNEL_ZBI="${img_dir}/fuchsia-ssh.zbi"
 "${ZBI_TOOL}" -o "${KERNEL_ZBI}" "${ZBI_IMAGE}" \