Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've got a handful of servers which have a log file I'd like to monitor. Because the project is new and most of the errors so far have been unique, I'd like to monitor these logs manually for a while. I was hoping to save a little bit of time by putting all of the IPs to monitor in a text file, then doing something simple like:

#!/bin/bash
while read ip; do
    scp user@"$ip":/var/log/file.log ./
    nano -w file.log
    rm -f file.log
done < ips.txt

However, Nano (and Vi) absolutely flip out, as it appears the Bash loop does not wait for me to exit them before continuing on to the next command.

How do I make the Bash loop wait for me to exit the text editor before continuing? Feel free to be technical in explaining the answer; part of the reason I'm asking is because I don't know enough about Bash to know why this is happening, but I'd like to.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

I think I know what it is. Because you are redirecting the input to the while loop, nano no longer sees your terminal at its stdin. Try replacing

nano -w file.log

with

nano -w file.log < /dev/tty
share|improve this answer
    
Excellent; thanks! Although I vaguely knew about input/output redirection, I'd never stopped to consider what that magical while loop formula I oh so frequently copied was doing. –  Hammer Bro. Mar 7 '12 at 1:39

Consider...

#!/bin/bash
while read ip; do
    scp user@"$ip":/var/log/file.log ./
    nano -w file.log && rm -f file.log
done < ips.txt

The && will wait for a command to complete successfully (return with 0) before doing the statements to the right of it.

edit: woops, after actually running the command, @glennjackman is right. Also, if you want to learn some bash stuff, consider reading TLDP's beginner Bash and TLDP's Advanced Bash

share|improve this answer
    
You mean nano -w file.log && rm -f file.log -- get rid of the $(). –  glenn jackman Mar 6 '12 at 19:18
    
@glennjackman Neither of those quite work. The poster's solution gets "Received SIGHUP or SIGTERM" just after SCPing the file, yours actually opens it in Nano before "Received SIGHUP or SIGTERMustify". Either one bombs out at that point, although the $() leaves my session in a strange state. –  Hammer Bro. Mar 6 '12 at 19:25

Wrap it in a call to time

Example:

time nano -w file.log

There'll be some output spewed, which I suppose you could redirec to /dev/null if you like, but "time" should be considered to still be running, unlike the way the editors behave.

share|improve this answer
    
That's a nifty command I was unfamiliar with, but plugging it into the loop in my original question does not cause it to wait until completion -- Nano still gets a prompt "Received SIGHUP or SIGTERMustify" then time's output is displayed (0.489s later, by the way). I'm beginning to wonder if there's some strange Bash configuration at play here; the loop doesn't seem to be waiting for these commands to complete like people are expecting. Have you tried this on your local machine? Even a 127.0.0.1 SCP should demonstrate it... –  Hammer Bro. Mar 7 '12 at 0:34

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.