0

Is it possible to prevent a shell command from interacting with user?

For example when I try to unzip a file and the destination directory has size limits, I got y/n prompt.

unzip my_file.zip -d /var/tmp/my_dir

write error (disk full?).  Continue? (y/n)

Is it possible to terminate the shell command immediately by sending all its prompt to /dev/nul?

Khachatur
  • 921
  • 1
  • 12
  • 30
  • 1
    You could do `unzip my_file.zip -d /var/tmp/my_dir < /dev/null` to make `unzip` read input from `/dev/null`, but minitech’s solution is better. – yellowantphil Dec 05 '14 at 06:08
  • The < /dev/null is exactly what I was looking for. Please post it as answer and I will accept it. One more question: How Can I disable the password prompt? For example I want to clone a git repository, but want fail if it asks for password. I tried dev/null but this doesn't work: git clone https://my_login@my_gitrep.git < /dev/null – Khachatur Dec 06 '14 at 05:36

2 Answers2

2

You can use yes n to answer “no” to every prompt:

yes n | unzip my_file.zip -d /var/tmp/my_dir
Ry-
  • 218,210
  • 55
  • 464
  • 476
1

You can make unzip read input from /dev/null by redirecting its standard input:

unzip my_file.zip -d /var/tmp/my_dir < /dev/null

Another way to do this is with cat:

cat /dev/null | unzip my_file.zip -d /var/tmp/my_dir

That is slightly less efficient, since it starts another process (cat), but it’s perhaps easier to read. There are endless arguments about whether using cat in this way is a bad idea.

If a program asks for a password and input file redirection doesn’t stop it from prompting you, then it gets trickier. Programs sometimes open your terminal directly using /dev/tty, rather than relying on standard input and output. In that case, you can use expect to send input to the program. I don’t know how to use expect, so I can’t provide a sample script. Or you can see if there is a way to prevent your program from asking for a password.

Community
  • 1
  • 1
yellowantphil
  • 1,483
  • 5
  • 21
  • 30
  • I asked why redirection doesn’t seem to work on password prompts on the [Unix & Linux Stack Exchange](http://unix.stackexchange.com/questions/171835/how-does-ssh-prompt-for-a-password-when-all-input-and-output-is-redirected). – yellowantphil Dec 06 '14 at 17:14