Find the answer to your Linux question:
Results 1 to 6 of 6
I am having trouble with the read command. For some reason it is not accepting any input. For example if I run it directly from the bash prompt as follows ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    5

    Unable to accept input during a script (bash "read")


    I am having trouble with the read command. For some reason it is not accepting any input.

    For example if I run it directly from the bash prompt as follows
    read test
    and type
    123
    then run the set command I can see that an environment variable named test has been created but it is blank.

    Once the read command has exited the string I typed (123 in this example) is then output to the bash prompt resulting in a command not found error.

    The same thing happens in a script. For example
    echo "enter password"
    read password
    echo $password

    Will echo nothing and then whatever I typed during the script will then be entered at the next prompt with a "command not found" error.

    Any ideas? Its pretty hard to google for something like this since the word "read" is so common.

  2. #2
    Linux Engineer aliov's Avatar
    Join Date
    Dec 2006
    Location
    Geneva,Beirut
    Posts
    1,078
    did you check first if your default Shell is bash , run this echo $SHELL , and also try to run your script sh script.sh

    please let me know about your problem

    Regards.
    Linux is not only an operating system, it's a philosophy.
    Archost.

  3. #3
    Just Joined!
    Join Date
    Nov 2007
    Posts
    5
    The following commands were run on the effected machine.

    /var/tmp# echo $0
    /bin/bash
    /var/tmp# echo $SHELL
    /bin/sh

    I also created a file named script1.sh containing
    echo "ABC"
    read TEST2
    echo $TEST2
    echo TEST2

    and ran it using sh script1.sh but it had the same problem.

  4. #4
    Linux Engineer aliov's Avatar
    Join Date
    Dec 2006
    Location
    Geneva,Beirut
    Posts
    1,078
    This is very strange

    open a terminal as a normal user and then type bash , and try to run your script , by the way wish distro you are using .

    Regards.
    Linux is not only an operating system, it's a philosophy.
    Archost.

  5. #5
    Just Joined!
    Join Date
    Nov 2007
    Posts
    5
    The following shows the computer output in red and my input as black

    /var/tmp# exec /bin/bash
    /var/tmp#
    /var/tmp# 'read' ans
    123

    /var/tmp# 123
    bash: 123: command not found
    /var/tmp#
    /var/tmp#
    echo $ans

    /var/tmp#

    I was VERY careful to accurately differentiate my input from the ouput using color. Notice the line above "command not found"
    Here is the truncated output of "set"
    ...
    _=echo
    ans=
    prom=r
    ...

    Notice that "ans" is getting set by read but the input is getting dumped back to the next bash prompt. The bash version is
    /var/tmp# echo $BASH_VERSION
    2.02.0(1)-release

  6. #6
    Just Joined!
    Join Date
    Nov 2007
    Posts
    5
    I feel like an idiot. I found the problem. It was an issue with my telnet client

    By default many telnet clients including Windows telnet.exe send both a carriage return and a line feed when you press enter. This was being picked up by the system as two separate events and causing the problem I described.

    Thanks for the help. I hope someone can read this thread in the future and solve their own problem

    edit: For windows Telnet.exe
    unset crlf - return key only sends CR
    and
    set crlf - return key sends CR LF (normal operation)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •