User input while reading from a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers User input while reading from a file
# 1  
Old 02-20-2015
User input while reading from a file

I am not able to capture the user input in this script(bash).There is prompt for user input.Could some one help me capture user input while reading afile?

Code:
while read line

do
    
	echo "$i"
	path1=$line
	path2=`echo $line|sed s/new_dir/old_dir/`
	
	
    echo  "Do you want to replace?";
	
    read p;

	if [ "$choice" = "y" ];
	then 
	echo "mv -f $path1 $path2"
	else 
	echo "skipped"
  fi	
done < "$file"


Last edited by Corona688; 02-20-2015 at 04:17 PM..
# 2  
Old 02-20-2015
read does not work that way. It takes a variable name. You are asking it to put values into variable $p.

-p tells it to prompt, in bash anyway.

If you have redirected standard input, you can read from /dev/tty to connect directly to the console.

Code:
read -p "prompt" choice </dev/tty

# 3  
Old 02-20-2015
Hi

Use read choice instead.
That is because you do compare 'y' with 'choice' rather than with 'p' wich you read.

hth
# 4  
Old 02-21-2015
You can read via another descriptor (here:3) from the file
Code:
while read line <&3
do
...
    path2=`echo "$line"|sed "s/new_dir/old_dir/"`
 ...
    read choice

    if [ "$choice" = "y" ]
    then 
    echo mv -f "$path1" "$path2"
...
done 3< "$file"

# 5  
Old 02-22-2015
Solved

Thanks a lot everyone
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to embed data instead of reading user input from an array?

Hello, I am running under ubuntu1 14.04 and I have a script which is sending given process names to vanish so that I'd see less output when I run most popular tools like top etc in terminal window. In usual method it works. Whenever I restart the system, I have to enter the same data from... (2 Replies)
Discussion started by: baris35
2 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

reading yum user input

I am writing a script where it uses yum to install. I need to read the user input for yum ie "y or n". If the user types "y", the script should continue running. If the user types "n" then the whole script should be terminated. line1 line2 yum install package line3 line4 From above,... (5 Replies)
Discussion started by: anilcliff
5 Replies

4. UNIX for Dummies Questions & Answers

Help in reading the date from the input file name

Hi, I need to read the date from the input file. The format of the input file is as follows: a_b_c_yyyymmdd.txt I need to read the date(yyyymmdd) part from the name of the input file. Would really appreciate if someone can help me in this regard Thanks a lot. (1 Reply)
Discussion started by: Sunny_teotia
1 Replies

5. Shell Programming and Scripting

awk- reading input file twice

Hello, I've been trying to come up with a solution for the following problem; I have an input file with two columns and I want to print as an output the first column without any changes but for the second column, I want to divide it by its last value. Example input: 1 9 2 10 3 11 4 12 5... (14 Replies)
Discussion started by: acsg
14 Replies

6. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

7. Shell Programming and Scripting

reading input from a file

I am trying to read input for a C program (that expects input from the user) from a file using the shell command: progname < filename but it seems that the program considers the char '<' as the first input, hence causing an "error" in my program. I checked it with another program and it... (2 Replies)
Discussion started by: nadbar
2 Replies

8. Shell Programming and Scripting

Reading input from user

how do we read input from a user e.g i want to ask a user to enter 6 sets of numbers how do i control information from the user? i have this....... #!/bin/bash echo "Please enter six numbers" read number echo $number >> file1 but this stops after the first number..how can i... (2 Replies)
Discussion started by: vadharah
2 Replies

9. Shell Programming and Scripting

Help reading an input file in KSH

First I' d like to say you guys are awesome. :) I have a word document that I cut and paste into Textpad and it removed all the fancy formatting which is fine with me. I WinScp'd it to the box and and called it inputfile.txt. Opened it in vi and don't see any special characters or stuff that... (2 Replies)
Discussion started by: zilla30066
2 Replies

10. Shell Programming and Scripting

Script for reading an input file

#!/bin/sh rpt="/export/home/legato/rpt_offsite"/test_eject.tape cat <$rpt while read line do echo $line perform routine done I am trying to read the contents of this file line by line and perform a routine for each line read. The file contents are numbers.. What is wrong with my... (1 Reply)
Discussion started by: gzs553
1 Replies
Login or Register to Ask a Question