The easiest way to detect if your program is running interactively is to
use https://github.com/mattn/go-isatty and test if stdin (fd 0) is
connected to a terminal. The Elvish shell uses this function to handle both
Unix and Windows:

// IsATTY determines whether the given file is a terminal.
func IsATTY(fd uintptr) bool {
    return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
}


On Thu, Jun 8, 2023 at 11:19 AM Rich <rma...@gmail.com> wrote:

> Hi,
>
> I have a program I am writing that stops and asks the user for input, in
> this case it's a passphrase used to encrypt output.
>
>  I want the program to also be able to be used in a script, and if in a
> script use a predefined value as the passphrase. What I'd like to know is
> how to detect if running in a script or not.  I've tried something like
> this:
> runPid := os.Getpid()
> parPid := os.Getppid()
> val := runPid - parPid //normally I check if runPid is > parPid in my code.
> if val < 20 {
> fmt.Println("Not running in a script")
> }
> This works, but only if the script ran quickly. Wondering if there is a
> better way to do this?
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/c8ae1be5-5a6b-45af-9249-ccdb02283d97n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/c8ae1be5-5a6b-45af-9249-ccdb02283d97n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD-7LYBA5TqJdJu2p1e5FeCab8Vc0SthQu_On4sX92kXLg%40mail.gmail.com.

Reply via email to