Search code examples
bashsshgrepack

Why does ack over ssh not work?


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?


Solution

  • 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