LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   read from keyboard while reading from file in SHELL SCRIPTING (https://www.linuxquestions.org/questions/programming-9/read-from-keyboard-while-reading-from-file-in-shell-scripting-786628/)

m3ll0 02-03-2010 04:22 AM

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

catkin 02-03-2010 04:30 AM

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


m3ll0 02-03-2010 05:47 AM

hey, that's it!! thanks a lot!!!!

konsolebox 02-04-2010 01:26 AM

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


catkin 02-04-2010 05:18 AM

Quote:

Originally Posted by konsolebox (Post 3851882)
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.

m3ll0 02-10-2010 05:03 AM

Quote:

Originally Posted by konsolebox (Post 3851882)
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

catkin 02-10-2010 07:01 AM

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?

tuxdev 02-10-2010 09:47 AM

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.

catkin 02-10-2010 11:23 AM

Quote:

Originally Posted by tuxdev (Post 3859136)
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


tuxdev 02-10-2010 12:56 PM

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.

konsolebox 02-12-2010 07:55 AM

Guys you can also do that in any available fd number not just 4.

Slavian 10-30-2010 08:30 AM

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.


All times are GMT -5. The time now is 06:59 AM.