Error if script is waiting for password input or input

I am developing a script that should not interact with the user, and if the program inside my script requires the user to enter something, the script should immediately exit the system and exit it.

I already closed STDIN at the top of my script, and this works for some commands, but there are some that are waiting for input in another file descriptor (usually those that request passwords, such as git or sudo ).

Currently my script is:

 #!/bin/bash # close STDIN exec 0<&- # test that $1 exists and it is a valid folder containing a git repository # ... cd "$1" git fetch # if git is not correctly configured, it asks for a password and # my script waits for it :( 

So how can I avoid this?

Note. I am not asking how to make a password-free configuration in git or sudo or whatever , but to prevent a program in bash (or sh ), wait for user input and crash immediately if that happens.

+4
source share
3 answers

Instead of trying to close standard input or standard error, you might be better off just using a short pending script that helps out if it sees a password prompt. For instance:

 #!/bin/bash cd "$1" expect -c ' log_user 0 spawn git fetch expect -timeout 30 -re "ass(phrase|word)" { exit 1 } ' # Do something based on the exit status of the expect script. [[ $? -eq 1 ]] && { echo 'Password prompt detected!' >&2; exit 1; } 

The wait script will return with exit status 1 if it sees a password or passphrase request. The bash script can then act in a similar way or take other measures.

Hope this helps!

+2
source

Let me first say that I do not think this is the right way to approach your problem. At all. There must be a better solution for a particular application, and you really have to find it.

The main problem is that, not knowing which programs you want to "force close", you will not know how to make them "exit the system." And if you know the software, the CodeGnome solution seems great.

Also note that not all programs read input directly from STDIN. For example, the Python getpass module opens an additional file descriptor directly to /dev/tty , so closing or redirecting STDIN does not matter. It also means that sudo accepts passwords, and I believe that ncurses works that way. Reading in this way allows you to receive passwords without displaying them on the screen.

For hints that are not directly readable from /dev/tty (e.g. bash read ), just redirecting STDIN from /dev/null (e.g. ./prompt.sh </dev/null ) can take you somewhere. In addition, you can do what you do and close STDIN, but how the program works when it detects a closed STDIN or a redirected one consisting only of EOF,. I hope the program will be mistaken as you wish, but perhaps it will continue the cycle, waiting for the actual input. Who knows?

Moreover, neither closure nor redirection worldwide will work if that happens. Some programs may fail because you are hoping for closed STDINs; others may need EOF from /dev/null

Thus, the way to do this is not a general, after all, type of solution, but one intended for the programs that you placed in this place. Even better, do not use try (to run the program), then catch (the case when the program asks for input), just call the programs in ways that you know they will not ask for input.

In any case, all that is said, one of the possibilities that can work is to close STDIN and execute the background process. For instance:

 #!/bin/bash exec 0<&- ./prompt.sh & 

or, conversely, redirect STDIN to / dev / null and execute the background process. For instance:

 #!/bin/bash ./prompt.sh </dev/null & 

Then you will need to check the return code of the programs (using $? ) To make sure they exit correctly. (We hope that the unknown list of programs that you use matches the standard return value scheme)

Closing / redirecting STDIN will handle the case when STDIN is used to receive input and background processing the process will handle the case where /dev/tty (since the background process does not have tty).

You yourself, if the program offers you to use some other method (opens FD directly to your pseudo-terminal, a window with graphic input pops up, presents you with a sound prompt, etc.).

+3
source

In most (all?) * NIX has a tty command that displays the name of the tty shell and exits from 0 if the shell has an attached tty, otherwise it exits from 1 . p>

 if ! tty>&/dev/null; then echo "Not a terminal; goodbye."; exit 1; fi 
0
source

All Articles