6

I'm writing a script for zsh that essentially just needs to know the exit code of the following gradle command: ./gradlew installDebug. In other words, I don't want any output to the console while running this. I've tried the following: ./gradlew installDebug -q > /dev/null; 2>&1

This eliminates 99% of the output, but I still get lines like the following:

Note: Generating a MembersInjector for com.example.ui.BaseActivity. Prefer to run the dagger processor over that class instead.
Note: Generating a MembersInjector for com.example.widget.nav.NavigationDrawerActivity. Prefer to run the dagger processor over that class instead.
Note: Generating a MembersInjector for com.example.fragment.ShiftListFragment. Prefer to run the dagger processor over that class instead.

These warnings are coming from lint, which, in general, I want to keep (for when I build manually, without this script). I simply want to suppress the output in this one specific case.

I feel like I'm simply not redirecting output for a given stream correctly.

jwir3
  • 6,019
  • 5
  • 47
  • 92

1 Answers1

4

As long as you are using zsh you can use

./gradlew installDebug -q &> /dev/null

For POSIX compliant shells (including zsh) you can use

./gradlew installDebug -q > /dev/null 2>&1

The reason it did not work is the semicolon. A ; separates two commands but redirections only work per command and not per command-line. So what actually happens is equivalent to the following:

./gradlew installDebug -q > /dev/null
2>&1
  • In the first command the standard output of ./gradlew is redirected to /dev/null but standard error is not changed and thus printed normally.

  • The second is actually just a redirection without a command. In this case the behavior depends on the setting of the the parameter NULLCMD and the options SH_NULLCMD and CSH_NULLCMD. By default zsh runs cat with the given redirection applied.

Sebastiano
  • 12,289
  • 6
  • 47
  • 80
Adaephon
  • 16,929
  • 1
  • 54
  • 71
  • @JorgeMachado Can you please explain why this would not work with `println`? Or is this more of an observation that it does not seem to work? – Adaephon Dec 12 '17 at 12:53
  • Because I had the same issue. Running this command as single let's say so it will work. If you use it in the a serie of commands it will break and in my case I was build a version.txt with version=. What I end up having was version=. The way to do it properly is not to println on your gradle file. use logger.info("some message instead of println") – Jorge Machado Dec 12 '17 at 13:02