How are the default answers [Yn] for the Ruby command "gem clean"?

I regularly use the Ruby gem clean command to keep the local gem repository in shape.

However, due to dependency problems, the command returns a query many times, for example:

 XXXXX-1.0.6 depends on [YYYYYY (~> 0.8.4)] If you remove this gems, one or more dependencies will not be met. Continue with Uninstall? [Yn] 

Although this is simple enough, manual intervention is required (for [Yn] answer), and therefore it prevents me from creating a simple cron script to automate this process.

Any ideas on how to cancel the response for these gem requests?

+4
source share
3 answers

You should have the yes command, the OSX version should say:

 YES(1) BSD General Commands Manual YES(1) NAME yes -- be repetitively affirmative SYNOPSIS yes [expletive] DESCRIPTION yes outputs expletive, or, by default, ``y'', forever. HISTORY The yes command appeared in 4.0BSD. 4th Berkeley Distribution June 6, 1993 4th Berkeley Distribution 

So maybe this will work:

 yes n | gem clean 

gem clean can be read directly from the terminal, and not from standard input. In this case, you might have expect :

Waiting is a program that β€œtalks” to other interactive programs according to a script. After the script, Waiting knows what to expect from the program and what the correct answer should be. The interpreted language provides branching and higher-level control structures for directing dialogue. In addition, the user can control and interact directly as desired, after which control returns to the script.

So, you can write an expect script to answer the expected hints using "y" or "n" as desired.

+7
source

This should work:

 echo|gem clean 

It will act as a return hit on the command line. 'y' is the default value, it will complete gem clean .

+1
source

Tilo's answer is the simplest. However, if you need to use "n", you're out of luck. Some searches have found this thread with some sample Java code: Java: detecting a user prompt when starting a script package from Java

I changed it for this situation, and I think it will allow you a more conservative option of using "n" as the default choice. Of course, this code can be changed in a bash script if you prefer not to have a java class working in your cron jobs, but I will leave this for someone more experienced with bash.

I have no easy way to check this code right now, so let me know how it works. :)

 public static void main(final String... args) throws IOException, InterruptedException { final Runtime runtime = Runtime.getRuntime(); final String command = "..."; // cmd.exe final String matchString = "Continue with Uninstall? [Yn] "; final String response = "n"; final Process proc = runtime.exec(command, null, new File(".")); final BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream())); final BufferedWriter output = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())); StringBuilder sb = new StringBuilder(); char[] cbuf = new char[100]; while (input.read(cbuf) != -1) { sb.append(cbuf); if (sb.substring(sb.length() - matchString.length(), sb.length()).equals(matchString)) { output.write(response); output.newLine(); output.flush(); } } System.out.println(sb); } 
0
source

Source: https://habr.com/ru/post/1379796/


All Articles