LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-03-2010, 04:22 AM   #1
m3ll0
LQ Newbie
 
Registered: Feb 2010
Location: Spain
Posts: 4

Rep: Reputation: 0
Question read from keyboard while reading from file in SHELL SCRIPTING


hi all, i'm writing a script and i'm stuck. i need to read from keyboard to ask for confirmation while im reading from a file. my code is this:
while read linea
do
#code
echo "¿you sure? (y/n)"
read answer
#code
done < $"agenda.dat"
the problem is that stdinput has been redirected to file agenda.dat so when i do "read answer" it reads the next line from the file. any idea to solve it?? thanks
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-03-2010, 04:30 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Code:
#!/bin/bash
while read line
do
    read -p 'You sure? ' answer < /dev/tty
    echo "DEBUG: line is '$line', answer is '$answer'"
done < input.txt
 
2 members found this post helpful.
Old 02-03-2010, 05:47 AM   #3
m3ll0
LQ Newbie
 
Registered: Feb 2010
Location: Spain
Posts: 4

Original Poster
Rep: Reputation: 0
hey, that's it!! thanks a lot!!!!
 
Old 02-04-2010, 01:26 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
probably a cleaner one:
Code:
#!/bin/bash
while read -u 4 line
do
    read -p 'You sure? ' answer
    echo "DEBUG: line is '$line', answer is '$answer'"
done 4< input.txt
 
Old 02-04-2010, 05:18 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by konsolebox View Post
probably a cleaner one:
Code:
#!/bin/bash
while read -u 4 line
do
    read -p 'You sure? ' answer
    echo "DEBUG: line is '$line', answer is '$answer'"
done 4< input.txt
That's nice; it leaves the option of providing automated input to the script.
 
1 members found this post helpful.
Old 02-10-2010, 05:03 AM   #6
m3ll0
LQ Newbie
 
Registered: Feb 2010
Location: Spain
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
probably a cleaner one:
Code:
#!/bin/bash
while read -u 4 line
do
    read -p 'You sure? ' answer
    echo "DEBUG: line is '$line', answer is '$answer'"
done 4< input.txt
thanks for the answer, but i'd like to know, what is the "4" exactly for and -u option? i didn't find too much info about it. thanks again anyway
 
Old 02-10-2010, 07:01 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Like most programs, bash reads and writes from files. It labels them with numbers called file descriptors or "fd"s. By default, three files are open and are connected to the terminal
  1. "standard input" or stdin for short on fd 0
  2. "standard output" or stdout for short on fd 1
  3. "standard error" or stderr for short on fd 2
These can be changed. Most usually using Redirections, as is often seen, for example, when discarding error messages, sending all output to a file or taking input from a file:
Code:
my_command 2>/dev/null
my_command > output.tx 2>&1
my_command < input.txt
In the konsolebox's code a new file is opened on fd 4. The 4< input.txt tells bash to open input.txt for reading on fd 4 and the read command's -u 4 on the tells it to read from fd 4.

The exec command can also be used to open files on file descriptors and to duplicate file descriptors. See Bourne Shell Builtins and scroll down to exec.

The current file descriptors may be seen in the /proc file system
Code:
c:~$ ls -l /proc/$$/fd
total 0
lrwx------ 1 c users 64 Feb 10 18:24 0 -> /dev/pts/0
lrwx------ 1 c users 64 Feb 10 18:24 1 -> /dev/pts/0
lrwx------ 1 c users 64 Feb 10 18:24 2 -> /dev/pts/0
lrwx------ 1 c users 64 Feb 10 18:24 255 -> /dev/pts/0
lrwx------ 1 c users 64 Feb 10 18:24 4 -> /dev/ptmx
c:~$ exec 7<~/.bashrc
c:~$ ls -l /proc/$$/fd
total 0
lrwx------ 1 c users 64 Feb 10 18:24 0 -> /dev/pts/0
lrwx------ 1 c users 64 Feb 10 18:24 1 -> /dev/pts/0
lrwx------ 1 c users 64 Feb 10 18:24 2 -> /dev/pts/0
lrwx------ 1 c users 64 Feb 10 18:24 255 -> /dev/pts/0
lrwx------ 1 c users 64 Feb 10 18:24 4 -> /dev/ptmx
lr-x------ 1 c users 64 Feb 10 18:25 7 -> /home/c/.bashrc
Notes:
  1. $$ is s special shell variable containing the process ID (PID) of the current shell.
  2. I don't know the significance of "fd"s 4 and 255. Anybody?

Last edited by catkin; 02-10-2010 at 07:03 AM. Reason: Comprehensibility and tidiness. Next to cleanliness and godliness.
 
1 members found this post helpful.
Old 02-10-2010, 09:47 AM   #8
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
ptmx is supposedly the "pseudo-terminal master", but I'm not sure what significance it has. None of the systems I've been on has ever opened 4 like that..

Anyhow, fd 255 refers to a shell script or generally the location commands are read from.
 
1 members found this post helpful.
Old 02-10-2010, 11:23 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by tuxdev View Post
ptmx is supposedly the "pseudo-terminal master", but I'm not sure what significance it has. None of the systems I've been on has ever opened 4 like that..

Anyhow, fd 255 refers to a shell script or generally the location commands are read from.
Thanks for the info

Good to learn about fd 255.

The system showing "4 -> /dev/ptmx" is Slackware 13.0 32-bit so likely straightforward unless mrxvt is up to something cunning. Given that fd 4 is commonly used it can easily be clobbered (and is not replaced)
Code:
c:~$ exec 4<~/.bashrc
c:~$ ls -l /proc/$$/fd
total 0
lrwx------ 1 c users 64 Feb 10 22:46 0 -> /dev/pts/2
lrwx------ 1 c users 64 Feb 10 22:46 1 -> /dev/pts/2
lrwx------ 1 c users 64 Feb 10 22:46 2 -> /dev/pts/2
lrwx------ 1 c users 64 Feb 10 22:46 255 -> /dev/pts/2
lr-x------ 1 c users 64 Feb 10 22:46 4 -> /home/c/.bashrc
 
Old 02-10-2010, 12:56 PM   #10
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
Quote:
The system showing "4 -> /dev/ptmx" is Slackware 13.0 32-bit so likely straightforward unless mrxvt is up to something cunning. Given that fd 4 is commonly used it can easily be clobbered (and is not replaced)
I think it is in fact something about mrxvt.. and its tabbing stuff. It kind of makes sense according to the master-slave terminology. I only use xterm, rxvt, or rxvt-unicode so I never see it myself.
 
Old 02-12-2010, 07:55 AM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Guys you can also do that in any available fd number not just 4.
 
Old 10-30-2010, 08:30 AM   #12
Slavian
LQ Newbie
 
Registered: Oct 2010
Posts: 1

Rep: Reputation: 0
Reading from the keyboard without using any additional scripts

As I anderstood from the written above, the /dev/tty command calls for some kind of script.
I should read from the keyboard without additional files of scripts. Is it possible? Is it possible to alter every time between input from file or keyboard?
Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell scripting - read choose next mravljica Programming 12 04-22-2009 02:11 PM
read command in shell script what to do for reading a particular char Ashok_mittal Linux - Newbie 11 01-21-2008 05:01 AM
Shell scripting,;problem reading from database and exporting to html sunksullen Programming 7 05-18-2007 02:16 PM
Reading text from a file using shell scripting. mrobertson Programming 11 06-29-2005 12:12 PM
bash scripting read from file cadj Programming 2 02-29-2004 10:42 PM


All times are GMT -5. The time now is 03:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration