Unable to read user input inside a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to read user input inside a loop
# 1  
Old 10-04-2017
RedHat Unable to read user input inside a loop

Hi,

This query is a part of a much more lengthy script.

I wish to look for all the files in a folder named "data" which in this case has two files i.e. plan.war and agent.properties. For all the files found under data I wish to ask the user as to where they wish copy the files to.

Below, is my script for the same.

Code:
more run.sh
 #!/bin/bash
 cd data
find . -type f \! -name test.tar | $AWK -F/ '{print $NF}' | while IFS= read -r entry; do
 echo "FILE_CURRENT_DIR:"$entry
 read -p 'Where do you want to copy $entry to? Please enter the complete directory location: ' cppath
echo "cp -R $entry $cppath/"
cp -R $entry $cppath/
 done

However, instead of asking me for input the script considers the second file found under data folder at the target location for the copy and thus fails.

Kindly see the problematic output below.

Quote:
FILE_CURRENT_DIR: plan.war
cp -R plan.war agent.properties/
cp: cannot create agent.properties/: Not a directory
Can you please help me get user prompt for every file found under data ?
# 2  
Old 10-04-2017
You have two read, so you have to use a different file descriptor to make it work.

Another approach is to use a for loop instead:-
Code:
for file in data/*
do
        [[ "$file" =~ "test.tar" ]] && continue;

        read -p "Where do you want to copy ${file} to? enter absolute path: " cp_path

        echo "cp -R $file ${cp_path}/"
done

This User Gave Thanks to Yoda For This Post:
# 3  
Old 10-04-2017
Tools

Quote:
Originally Posted by Yoda
You have two read, so you have to use a different file descriptor to make it work.
Can you please give me an example for this approach ?

Also, if i use your code how can i include wow.tar along with test.tar ?

I mean test.tar or wow.tar if either is found do not consider them.

Thank you.
# 4  
Old 10-04-2017
Can you please give me an example for this approach ?
Code:
CWD=$(pwd)
find data/ -type f \! -name test.tar | $AWK -F/ '{print $NF}' > ${CWD}/out.txt

while read entry
do
        echo "FILE_CURRENT_DIR:"$entry
        read -p 'Where do you want to copy $entry to? Please enter the complete directory location: ' -u 3 cppath
        echo "cp -R $entry $cppath/"
done 3<&0 < ${CWD}/out.txt

Quote:
Originally Posted by mohtashims
Also, if i use your code how can i include wow.tar along with test.tar ?
Code:
if [[ "$file" =~ "test.tar" ]] || [[ "$file" =~ "wow.tar" ]]
then
        continue
fi

This User Gave Thanks to Yoda For This Post:
# 5  
Old 10-04-2017
Bug

Quote:
Originally Posted by Yoda
Can you please give me an example for this approach ?
Code:
CWD=$(pwd)
find data/ -type f \! -name test.tar | $AWK -F/ '{print $NF}' > ${CWD}/out.txt

while read entry
do
        echo "FILE_CURRENT_DIR:"$entry
        read -p 'Where do you want to copy $entry to? Please enter the complete directory location: ' -u 3 cppath
        echo "cp -R $entry $cppath/"
done 3<&0 < ${CWD}/out.txt


Code:
if [[ "$file" =~ "test.tar" ]] || [[ "$file" =~ "wow.tar" ]]
then
        continue
fi

Thank you; I will try these out tonight and let you know if they work realtime.

---------- Post updated at 11:56 AM ---------- Previous update was at 11:40 AM ----------

Quote:
Originally Posted by Yoda
You have two read, so you have to use a different file descriptor to make it work.

Another approach is to use a for loop instead:-
Code:
for file in data/*
do
        [[ "$file" =~ "test.tar" ]] && continue;

        read -p "Where do you want to copy ${file} to? enter absolute path: " cp_path

        echo "cp -R $file ${cp_path}/"
done

The files may not be in the data folder instead in its sub-directories.

Can you please confirm if replacing the find with for will still work as you suggested ?
# 6  
Old 10-04-2017
Quote:
Originally Posted by mohtashims
Can you please confirm if replacing the find with for will still work as you suggested ?
Replacing find with for will not traverse sub-directories. Use the find based approach if you want to traverse sub-directories and work on files in them.
# 7  
Old 10-04-2017
You can keep the pipe if you redirect the whole block.
Code:
{
find data/ -type f \! -name test.tar | $AWK -F/ '{print $NF}' |
 while IFS= read -r entry
 do
        echo "FILE_CURRENT_DIR:$entry"
        printf "Where do you want to copy '$entry' to? Please enter the complete directory location: "
        read cppath <&3
        echo cp "$entry" "$cppath/"
 done
} 3<&0

<&3 lets the shell do what read does with -u 3.
read -p "text" is bash-only. Portable is printf "text"; read.

Last edited by MadeInGermany; 10-11-2017 at 05:20 AM.. Reason: replace "read -p"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop inside awk to read and print contents of files

Hello, I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for... (5 Replies)
Discussion started by: jaldo0805
5 Replies

2. Shell Programming and Scripting

Update file record inside read loop

Hi, I am reading file records inside a while loop, and want to update the record when certain condition is met. How can I update a file while being read? I want to avoid using temporary files, copy, rename, ... while IFS=',' read -r f1 f2 do function(f1,f2) if then <add... (1 Reply)
Discussion started by: ysrini
1 Replies

3. Homework & Coursework Questions

How to read user keyboard input inside the case?

I need to Write a shell script that allows some system-administration tasks to be preformed automatically from a menu-driven interface. with automated following tasks: Copy directory tree Delete files or directories Output Information (this part is done ) *Copy directory tree The “Copy... (2 Replies)
Discussion started by: femchi
2 Replies

4. Shell Programming and Scripting

Unable to read the first space of a record in while loop

I have a loop like while read i do echo "$i" . . . done < tms.txt The tms.txt contians data like 2008-02-03 00:00:00 <space>00:00:00 . . . 2010-02-03 10:54:32 (2 Replies)
Discussion started by: machomaddy
2 Replies

5. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

6. UNIX for Dummies Questions & Answers

Getting user input from inside a while loop?

I'm new to BASH and i'm trying to create a script which is simply put a large find and replace file. This is what I have so far N=0 while read LINE ; do N=$((N+1)) sed 's/'$2'/'$3'/g' $LINE > .temp echo "Changes to file $N = $LINE" echo 'The following changes... (5 Replies)
Discussion started by: Azumandious
5 Replies

7. Shell Programming and Scripting

read command (input) inside the while loop

Hi, 'read' command is not working inside the while loop, How can I solve this? Rgds, Sharif. (2 Replies)
Discussion started by: sharif
2 Replies

8. UNIX for Dummies Questions & Answers

read user input from within a wile loop that is being fed from below

hi! i need to do a ksh script that uses a wile loop that is fed form below while read line do some things done < myfile inside the while loop i need to read user input to ask the user what he wants to do, but "read" reads the file, and not the standard input while read line do ... (2 Replies)
Discussion started by: broli
2 Replies

9. Shell Programming and Scripting

input inside while read loop

Hi all Does anyone have a script that will allow me to stop inside a while read loop. I want to pause the loop until a enter is pressed. e.g. While read line do echo something if LINECOUNT > 40 then read ENTER?"PRESS ENTER TO CONT..." ... (3 Replies)
Discussion started by: jhansrod
3 Replies

10. UNIX for Dummies Questions & Answers

read inside a while loop

Hi all, In a while loop, like below... while read line do read choice case $choice in 1) echo "xxx" esac done < file why I can't run the read choice???? (3 Replies)
Discussion started by: dta4316
3 Replies
Login or Register to Ask a Question