Read not prompting/ pausing for input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read not prompting/ pausing for input
# 1  
Old 03-29-2013
Read not prompting/ pausing for input

Heya people,
So my script is one of those make-user-scripts, making a bunch of directories in the home folder and copying some files over into the new directories.
However, part of the thing my script need to do is check the home folder if a username matches with a directory which already exists.
e.g. if the username 'user1' has a directory in the home folder under the same name, it will prompt the user if he/she wants to keep or delete the directory (and its contents) before moving on with the script.

Here is the part of the script which does this:
Code:
cat $1 | while read line; do 

    username=$line
    if [ -d $4/$username ];
        then
        echo WARNING username $username already has a directory.
        read -p "Would you like to KEEP or DELETE?" answer
        
        if [ "$answer" == "DELETE" ];
            then
            rm -fr $4/$username
        elif [ "$answer" == "KEEP" ];
            then
            echo "Keeping directory $username"
        else
            echo "Unknown answer assuming keeping file"
        fi

    fi

done

Where:
$1 = path to the username_list file
$4 = path to the home directory for user creation

Now as the thread title suggests, the problem is the read line is not prompting for user input.
I suspect it may be something to do with the fact I'm piping via the while loop, but I don't know I'm just a newbie to all this. Can anyone suggest a solution to this?

Thanks in advance,

Cloudy
# 2  
Old 03-29-2013
Try running the following command to debug your script:

Code:
sh -x <your script>

# 3  
Old 03-29-2013
Quote:
Originally Posted by in2nix4life
Try running the following command to debug your script:

Code:
sh -x <your script>

Nope that doesn't help one little bit.
# 4  
Old 03-29-2013
Give this one go, https://www.unix.com/302107299-post2.html
It worked fine with /bin/sh and /bin/bash on Debian Testing
# 5  
Old 03-29-2013
You've got two reads in your code snippet; which one does not operate the way you'd expect it to? In your question text, you name the read line, but this one is not prompting, whilst the other, prompting one is read answer.

Please look again at your script and guess/find out where the second read is redirected to; you may find the answer by yourself.

And, BTW, it doesn't help a little bit either to give snappy answers to people offering help...
# 6  
Old 03-29-2013
Quote:
Originally Posted by Cloudy_Cat
Code:
cat $1 | while read line; do 
    ...
    read ...
    ...
done

In case you are not aware, the redirections that affect the while-loop statement are inherited by all of the commands within it. So, in your case, the pipe between cat and while-read redirects the standard input of the entire while-loop to cat's output. Both of your read commands will be taking their input from cat. Line 1 from cat will be consumed by the first read statement. Line 2 from cat will be consumed by the second read statement, etc. Is that intentional? I assume it's not, since if that were the case, there would be no need to prompt cat for a response.

Regards,
Alister
# 7  
Old 03-29-2013
Hi,

Instead of:

cat $1 | while read line; do
Try:

Code:
for line in `cat $1`
do

That way you don't have the "read inside a read loop issue" that alister pointed out. BTW well spotted alister - that one didn't register with me but once you pointed it out it seemed the best way to fix the script was to have an outer loop not using reads so the inner loop could issue reads.

Hope this helps.

-Andy.

Last edited by mcp; 03-29-2013 at 05:51 PM.. Reason: Spelt loop as look - D'Oh!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read input from Keyboard, do not proceed if no input

Hi, I am working on a script, which requests users to enter input. Ex: read -p "Please enter your email id:" email I don't want users skipping this entry, this has to be mandatory.I dont want to proceed without input. I can do a check if variable $email is empty and proceed if not.But, i... (7 Replies)
Discussion started by: aravindadla
7 Replies

2. UNIX for Dummies Questions & Answers

Input for read command

Hello, I want to use the read command in order to input a keyword from the keyboard. I then want to use this keyword in a grep command. How to I input a phrase as a keyword? For example if I use read keyword "today is" and then use grep, I get "No such file or directory" error. (6 Replies)
Discussion started by: FelipeAd
6 Replies

3. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

4. Shell Programming and Scripting

Read Input and go BG

My script needs to get some input from the user and go as a background process and run. I have something like read input. do while ... if .. fi done can i use nohup inside the script?? (7 Replies)
Discussion started by: Antergen
7 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. Shell Programming and Scripting

Read input

I have a script. #! /bin/bash echo "Enter a word: " read word echo $word That outputs like this.. Enter a word: hello hello But how can i read on the same line the question is printed? Like this.. Enter a word: hello hello (2 Replies)
Discussion started by: cbreiny
2 Replies

7. UNIX for Dummies Questions & Answers

read input file

echo "enter employee #:/c" read employee grep -w $employee /tmp/file.txt when it asked me employee #, i typed employee, worked fine. when it asked me employee #, i type ENTER, it just sit there. if someone type in NULL or ENTER key, i want to exit out. (2 Replies)
Discussion started by: tjmannonline
2 Replies

8. Shell Programming and Scripting

read is not pausing to read

Can anyone tell me why this section of my script doesn't work? The script is designed to accept piped data, store it in a temporary location, and then prompt the user for their user name. It doesn't stop to read the username, however. It just finishes. It is called like: cat datafile | myscript... (3 Replies)
Discussion started by: davegerdt
3 Replies

9. UNIX for Dummies Questions & Answers

Prompting for Input - Getting Undefined Variable

#!/bin/csh -f echo "Enter MEUPS User Id :-" read UID echo "You entered $UID" --------------------------------------- Whn executed, I get an error message "UID: Undefined variable" (1 Reply)
Discussion started by: Kartheg
1 Replies

10. Shell Programming and Scripting

pausing a script

hello all, I have a script (Solaris v8 ksh) monitoring an inbound directory for reports. The script will then capture the report based on a set of circumstances. However I am unable to capture a report larger then 2-Gig in size due to program limitations. What I need to do is pause the monitor... (6 Replies)
Discussion started by: gozer13
6 Replies
Login or Register to Ask a Question