Problem while taking input from User


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem while taking input from User
# 1  
Old 12-26-2009
Problem while taking input from User

Hi All,

I am facing a problem while taking input from user in Shell Script. Basically , I want to read a file line by line. And I want to remove certain lines of that file.But before removing those lines , I need to ask the user whether user wants to remove the lines. If user inputs "Yes" , the Shell Script will remove the line.

But the Shell Script that I have written is not stopping it's execution to take input from user.

e.g.

If a file abc.txt has following content :
Code:
1
2
3
4
5
6
7

then I want to remove the line with content as 3.
The Shell Script , I am using is :
Code:
while read fileContent
do
	if test "$fileContent" -eq "3"
	then
		echo " Do you want to remove this line ? (Y/N ) : "
	
		read inputline
		
		if test "$inputline" = "Y"
		then
			[Code to remove line from file ....]
		fi
		echo $what
	fi
done < abc.txt

The Shell Script execution is not allowing user to enter input , instead it's displaying content of line 4 and terminating.

Can anyone please solve above issue ?

TIA
# 2  
Old 12-26-2009
The problem is that when you redirect standard input for the while loop with "done < abc.txt", that redirection is in effect for all the commands executed within it. The internal read statement's stdin is not reading from the terminal, it too redirected to read from abc.txt.

A couple of solutions:

1. You can save the original source of stdin for use by the internal read statement so that it can read from the terminal:

while read fileContent
....
read -u3 inputline
....
done 3<&0 < abc.txt

2. Use a different descriptor (instead of stdin) for reading the file.

exec 3<abc.txt
while read -u3 fileContent
...
read inputline
...
done

Regards,
alister
# 3  
Old 12-26-2009
Or you can use:
Code:
read inputline < /dev/tty

# 4  
Old 12-27-2009
Bug

Thanks Scrutinizer and alister. Quite helpful replies Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in taking input Shell script

I was writing a shell script, where i need to run a command through script and then take input from user and later terminate it. i am not sure how to take input in a proper format immediately after a command. example: below command line is used to import some data in to database (LDAP)... (3 Replies)
Discussion started by: Mridul17
3 Replies

2. Shell Programming and Scripting

Reading user input...problem with tab key

Hi all, I have a little problem with my shell script (reading user input, save user input to variable, invisible characters in the log file :() printf "1. What's your file path?" /path/to/my/file read -e FILE I have invisible characters in my log file (e.g. <ESC> or ^G) when I'm... (3 Replies)
Discussion started by: splendid
3 Replies

3. Shell Programming and Scripting

Process running time by taking user input

Need help in scripting . Below is the situation and need your inputs Checking all the processes, scripts running time based on user input time . Below Example ps -aef -o user,pid,etime,stime,args| grep sleep <user> 28995 01:24 14:14:39 sleep 120 <user> 29385 00:52 14:15:10... (8 Replies)
Discussion started by: ajayram_arya
8 Replies

4. Shell Programming and Scripting

Script to delete files older than x days and also taking an input for multiple paths

Hi , I am a newbie!!! I want to develop a script for deleting files older than x days from multiple paths. Now I could reach upto this piece of code which deletes files older than x days from a particular path. How do I enhance it to have an input from a .txt file or a .dat file? For eg:... (12 Replies)
Discussion started by: jhilmil
12 Replies

5. Shell Programming and Scripting

Case Contruct Problem user input

This is supposed to be a simple bank script. Whenever I try the case construct options it wont work for the deposit option after typing the amount it just goes straight back into the menu, same with withdrawal option. Option 3 which should just display the balance amount doesn't echo anything. The... (2 Replies)
Discussion started by: jcnewton13
2 Replies

6. Shell Programming and Scripting

Taking one input at a time

Legends, Please help me to come out of the below Bermuda triangle. I have four inputs in a shell script: A B C D Now, If A is passed by user then, B C D will be ignored. If C is passed by user, then A B D will be ignored. Regards, Sandy (11 Replies)
Discussion started by: sdosanjh
11 Replies

7. Shell Programming and Scripting

usinf STDIN or ARGV, taking the input, calling the library and printing results

Good morning!! I wrote a script and Im not the best at Perl so I would like someone to look it over....just in case. Ive been working on this script forever!! The script is supposed to: Have the user enter a number using STDIN. Calculate the average of the numbers, the total of all of the... (0 Replies)
Discussion started by: bigben1220
0 Replies

8. UNIX for Dummies Questions & Answers

Taking a Users input to for the basis of a Grep

Hi I'm currently putting together a script that will grep a set of data for a particular input, specified by the user. I'm getting myself all tangled up with my Echo's and reads. What i'm looking to do is get it so that it prompts the user to enter a value (echo) and then uses their input... (5 Replies)
Discussion started by: Great Uncle Kip
5 Replies

9. Shell Programming and Scripting

Problem taking input from file with for loop

I am trying to take input from a file and direct it into a bash script. This script is meant to be a foreach loop. I would like the script to process each item in the list one by one and direct the output to a file. # cat 1loop #!/bin/bash # this 2>&1 to redirect STDERR & STDOUT to file... (4 Replies)
Discussion started by: bash_in_my_head
4 Replies

10. Shell Programming and Scripting

put an interactive script in background after taking input

i am trying to find a way to put an interactive script in the background after taking input from the user a few test lines i was trying: date echo "Enter location" LOCATION= read LOCATION sleep 100 sleep 200 date for this small example i want the script to as the user for the... (7 Replies)
Discussion started by: epsilonaurigae
7 Replies
Login or Register to Ask a Question