3

I've got a simple bash script to remove some folders on a remote server over ssh. It basically does this:

THE_HOST=12.34.56.78
ssh me@$THE_HOST "rm /the/file/path/thefile.zip"

This works perfectly well. Before I do this I often search the contents of the files in a folder for a string using ack:

ack thestring /the/folder/path/

This works perfect when I ssh into the server and run it, but when I use it in one command it doesn't work:

ssh me@$THE_HOST "ack thestring /the/folder/path/"

This seems to freeze or run forever: I get no output and the command never ends. Does anybody know why this doesn't work for ack?

kramer65
  • 50,427
  • 120
  • 308
  • 488
  • 1
    What do you mean with "it is not working?" You do not receive any output, you receive an error? – kvantour Aug 29 '18 at 07:59
  • 1
    Could be ack behaves differently when it is run in a terminal. Try `ssh -t me@$THE_HOST "ack thestring /the/folder/path/`"` – nos Aug 29 '18 at 08:03
  • @kvantour - Forgot to add that. I added it to the question: It seems to freeze or run forever: I get no output and the command never ends. – kramer65 Aug 29 '18 at 08:08
  • @nos - Yes, that's it, thank you! If you add you comment as an answer I can accept it. – kramer65 Aug 29 '18 at 08:10
  • 1
    It would be nice to know why this is the case. I searched a bit, but I could not find anything why it is not working. – kvantour Aug 29 '18 at 08:14
  • just throwing this in: if you try to use `ack` with `ansible` (ad-hoc) command - it doesn't work with the "shell" module, but works with the "raw" module – MacMartin Jun 17 '21 at 09:44

1 Answers1

3

Could be ack behaves differently when it is run in a terminal. Try using the -t argument

ssh -t me@$THE_HOST "ack thestring /the/folder/path/"

When ack detects that stdin is not a terminal(a tty device), it will attempt to read the text to search in from stdin instead of the given file/folder. That's what happens when you run it through ssh, stdin will be connected to the ssh connection, which does not look like a terminal(tty) to ack.

The -t argument to ssh instead allocates a tty and connects it to stdin/out of the program you run, ack will then think it runs in a terminal and instead use the file/folder argument for searching. See http://github.com/beyondgrep/ack2/issues/659

nos
  • 223,662
  • 58
  • 417
  • 506
  • 1
    Maybe because ack uses the Term module in Perl to output ANSI color sequences? Maybe you could try `ANSI_COLORS_DISABLED=1 ack thestring /the/folder/path/` – Gerrit Aug 29 '18 at 08:51