2010年2月12日 星期五

Read into read

0 意見
I encountered a case where I'm in the middle of a loop that read through a file through the 'read' command, like,

  while read a b c
  do
    .. (do something) ..
    # need user to input something
    read X
    .. (do some more things) ..
  done < file.txt

The 'read X' inside the loop will read data from the file 'file.txt' instead of console.

To solve the problem, simple change the 'read X' into
  read X < /dev/console
that forces the script reads the input from the terminal.

If you are connecting using SSH, replace /dev/console with your current console device.  You can see your current terminal device file by the environment variable $SSH_TTY (for OpenSSH) that usually is /dev/pts/0, /dev/pts/1 and so on.

0 意見:

張貼留言

 
My Unix Notes | © 2011 Design by DheTemplate.com and Theme 2 Blog

Find more free Blogger templates at DheTemplate.com - Daily Updates Free Blogger Templates