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
# 8  
Old 10-10-2017
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

Can you please tell me why is it failing when I try the same.

Code:
#!/bin/ksh
 . ./deploy.profile
set -x
found_flag=0
 searchdir=/tmp/moht
 CWD=$(pwd)
 
 find $searchdir"/PRESENT/data" -type f \! -name client.tar | $AWK -F/ '{print $NF}'  > ${CWD}/out.txt
 while IFS= read -r entry; do
  found_flag=0
if [ $found_flag -eq 0 ]; then
read -p "Where do you want to copy $entry to? Please enter the complete directory location: " -u 3 cppath
echo "cp -R $entry $cppath/"
cp -R $entry $cppath/
fi
  echo "Displaying the value of found_flag outside while loop:$found_flag"
 done 3<&0 < ${CWD}/out.txt

Error:
Quote:
+ found_flag=0
+ searchdir=/tmp/moht
+ + pwd
CWD=/tmp/moht
+ /usr/xpg4/bin/awk -F/ {print $NF}
+ find /tmp/moht/PRESENT/data -type f ! -name client.tar
+ 1> /tmp/moht/out.txt
+ 3<& 0 0< /tmp/moht/out.txt
+ read -r entry
+ IFS=
+ found_flag=0
+ [ 0 -eq 0 ]
+ read -p Where do you want to copy bingo.txt to? Please enter the complete directory location: -u 3 cppath
./readtest.sh[24]: read: no query process
+ echo cp -R bingo.txt /
cp -R bingo.txt /
+ cp -R bingo.txt /
cp: cannot access bingo.txt
+ echo Displaying the value of found_flag outside while loop:0
Displaying the value of found_flag outside while loop:0
+ read -r entry
+ IFS=
Hope this works with the KSH shell as well. Does it ?
# 9  
Old 10-10-2017
read is a shell built-in. The KSH built-in has different options to the Bash one. Is there any reason you changed your script from Bash to the Korn shell?
# 10  
Old 10-10-2017
Quote:
Originally Posted by Scott
read is a shell built-in. The KSH built-in has different options to the Bash one. Is there any reason you changed your script from Bash to the Korn shell?
The reason for changing to KSH from BASH was my bash version is 10 yrs old and I cannot update it as I m not the system admin. I need my variable to hold global value so it persists its value outside the loop. Refer to my thread here for details.

Can you propose any solution to the error I m getting as shown in RED BOLD this POST 8.
# 11  
Old 10-10-2017
You should at-least check the ksh manual, look at the read options and give it a try before posting question:-
Code:
man ksh

       read [ -ACprsv ] [ -d delim] [ -n n] [ [ -N n] [ [ -t timeout] [ -u unit] [ vname?prompt ] [ vname ... ]

So you should modify your code to:-
Code:
read -u 3 cppath?"Where do you want to copy $entry to? Please enter the complete directory location: "

# 12  
Old 10-10-2017
Quote:
Originally Posted by Yoda
You should at-least check the ksh manual, look at the read options and give it a try before posting question:-
Code:
man ksh

       read [ -ACprsv ] [ -d delim] [ -n n] [ [ -N n] [ [ -t timeout] [ -u unit] [ vname?prompt ] [ vname ... ]

So you should modify your code to:-
Code:
read -u 3 cppath?"Where do you want to copy $entry to? Please enter the complete directory location: "

I tried that but it gives me this error:

Code:
+ read -u 3?cppath Where do you want to copy bingo.txt to? Please enter the complete directory location:
./readtest.sh[25]: 3: is not an identifier

My ksh man page says:

Code:
     read [ -prsu[ n ] ] [ name?prompt ] [ name ... ]


Last edited by mohtashims; 10-10-2017 at 11:59 PM..
# 13  
Old 10-10-2017
I noticed the ? symbol right after the descriptor instead of variable name:-
Code:
+ read -u 3?cppath Where do you want to copy bingo.txt to? Please enter the complete directory location:
./readtest.sh[25]: 3: is not an identifier

Also you need to check your ksh version and manual for read options, I tested this code in ksh93 and it works fine:-
Code:
$ strings /bin/ksh | grep "[V]ersion"
@(#)$Id: Version AJM 93t+ 2010-06-21 $

# 14  
Old 10-11-2017
Hammer & Screwdriver

My ksh version is:

Code:
strings /bin/ksh | grep "[V]ersion"
@(#)Version M-11/16/88i

the variable name is cppath.

I tried
Code:
read -u 3 cppath "Where do you want to copy $entry to? Please enter the complete directory location: "

but this also fails with the same error : 3: is not an identifier. Can you tell me what syntax should I try ?

Last edited by mohtashims; 10-11-2017 at 12:36 AM..
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