Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a shell script that has to find the newest file on a remote host. I have this bit:

ssh -q $HOSTNAME -t ls -t -1 /path/to/somewhere/ | head -n 1

It prints the first line, then waits and doesn't terminate.

However if I remove the | head -n 1, then it'll print all the files in order and then terminate regularly and return control flow to my terminal. So the SSH command part is terminating fine, but not the head part. What's going on? How do I get the head to work?

share|improve this question
    
Works for me in bash, outside of a script. Perhaps your variables aren't what you expect, or ssh is broken somehow, try removing the -q or perhaps do 2>&1 to catch stderr too.. –  Tom O'Connor Mar 2 '12 at 13:39
    
Why do you need -t for executing just ls? –  adamo Mar 2 '12 at 14:17
add comment

3 Answers

up vote 1 down vote accepted

It hangs because it doesn't get any input. Try doing just head -n 1 .

This will ensure that piping to head will occur in the pseudo-tty

ssh -q $HOSTNAME -t "ls -t -1 /path/to/somewhere/ | head -n 1"

I have to admit that in Debian Squeeze and Ubuntu server 10.4 I wasn't able to reproduce that.

Care to give more information like without the "-q",distro,etc.

share|improve this answer
add comment

How about

ssh -q $HOSTNAME -t "ls -t -1 /path/to/somewhere/" | head -n 1
share|improve this answer
    
No change, same affect, head hangs –  Rory Mar 2 '12 at 12:58
2  
What about wrapping the | head -n1 inside the double-quoted bit too. Why does that bit need to happen locally? –  Tom O'Connor Mar 2 '12 at 13:41
add comment

Why not try it this way:

ssh -q $HOSTNAME -t ls -tr -1 /some/path | tail -n 1

This will let you know if the head command is at fault. Your previous command seems to work fine on my machine. Strange stuff.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.