Read statement not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read statement not working
# 1  
Old 06-22-2013
Read statement not working

hello guys,
i am having the below piece of code
Code:
error
()
{
echo"Press y /n"
read ans
case $ans in
y) main;;
n) exit
esac
}

In the abve code, read statement is not working i.e not waiting for user to enter input.
,i tested exit status its 1.
could anyone help me to do this

Thanks in advance
# 2  
Old 06-23-2013
Quote:
Originally Posted by mohanalakshmi
hello guys,
i am having the below piece of code
Code:
error
()
{
echo"Press y /n"
read ans
case $ans in
y) main;;
n) exit
esac
}

In the abve code, read statement is not working i.e not waiting for user to enter input.
,i tested exit status its 1.
could anyone help me to do this

Thanks in advance
You haven't said what shell you're using, but assuming it is a shell that recognizes basic Bourne shell syntax you have several problems. You didn't mention that this script complained about an unknown error command and an unknown echoPress y /n command. Had used correct syntax for defining a function, you also need a main function or utility that is not shown in this code fragment. To get correct the syntax errors, try:
Code:
error() {
    echo "Press y/n"
    read ans
    case "$ans" in
        (y) main;;
        (n) exit;;
        (*) echo "Unexpected response: \"$ans\""
            exit 1;;
    esac
}

Although some of the changes here are editorial, the error() must appear on a single line with no space between the final "r" and the "()", and there must be a space after the echo.
# 3  
Old 06-23-2013
Hi thanks for the response Smilie i am using
Code:
#!/bin/sh

my problem is read didnt expect user input.exit status comes 1 what might be the reason??

i tried with correct syntax as u give.But its not working as expected.Smilie
# 4  
Old 06-23-2013
Quote:
Originally Posted by mohanalakshmi
Hi thanks for the response Smilie i am using
Code:
#!/bin/sh

my problem is read didnt expect user input.exit status comes 1 what might be the reason??

i tried with correct syntax as u give.But its not working as expected.Smilie
As noted in your earlier thread Bash vs shell, if you don't give us anything but "its not working as expected", we can't guess at what (if anything) is wrong. As I requested in the previous thread, please show us:
  1. the entire contents of your shell script (not just this function),
  2. the results of executing the script using the command:
    Code:
    /bin/sh -xv YourScriptName [argument...]

    where YourScriptName is the name of the file containing your script and argument... is the list of arguments you passed to your script (if there were any),
  3. the input you provided when running your script (if there was any), and
  4. the output you expected when running your script.
Am I correct in assuming that you are still using a Solaris SunOS release 5.10 system?

In your last thread you said that you thought the problem might have been a for statement or a variable assignment (neither of which appear in this function) that were causing your problem. How did you determine that they (as opposed to the error function above) were not the problem in your script? If you didn't run the script with tracing as I suggested before, what makes you think that the function above, the for statement, or the variable assignment is the problem rather than something else in your script that you haven't shown us?
# 5  
Old 06-23-2013
Code:
#!/bin/sh
current=/home_dir/a541101
#POfile=PO_NO.txt
error(){
echo "Do you want to enter PO again(Y/N):"
read answer
echo $?
case $answer in
Y) main;;
y) main;;
N) exit;;
n) exit;;
*) exit ;;
esac
}
main()
{
DATE=`date +"%m-%d-%y"`
if [ -f $current/temp.txt ]
then
rm $current/temp.txt
fi
if [ -f $current/PO_NO.txt ]
then
#cp $current/PO_NO.txt $current/PO_bk/PO_NO_$DATE.txt
rm $current/PO_NO.txt
fi
echo "Enter PO Numbers to create text file with entetred PO. Please type 'exit' when you finished entering"
while read line
do
if [ "$line" != "exit" ]
then
echo "$line" >> $current/temp.txt
else
break
fi
done
echo "Checking PO's wait for a while!!!!!!"
IFS=$'\n'
set -f
var=0
#for i in `cat temp.txt`
while read i
do
echo "$i"
var=`expr $var + 1`
len=`echo $i | wc -m`
#len=`expr $i : ".*"`
echo $var;
echo $len;
case ${i} in
*\ * ) echo "PO has at least one space in line number $var" ; error;
esac
if [ "$len" -gt 7 ]
then
echo "Error in PO,contains more than 7 char in line number $var with $len characters"
error
elif [ "$len" -lt 7 ]
then
echo "Error in PO hasless than 7 char in line number $var with $len characters "
 error
elif [ "$len" -eq 7 ]
then
echo $i >> $current/PO_NO.txt
fi
m=$var
done < temp.txt
echo "for loop over"
if [ -f $current/PO_NO.txt ]
then
echo "Output text file has been created check $current/PO_NO.txt for output file"
echo "PO_NO.txt contains $m lines";
fi
echo "Triggering the PO Publish script "
echo " "
}
main

Hi the above is my entire code, whenever i run this code by callig error read answer is not allowing me to enter user input.
when i execute the prog as sh name.sh its not working
but with bash name.sh
its working fine
thank u
# 6  
Old 06-23-2013
When first reading the problem, I guessed - and then was confirmed by the script - that input was redirected. Do you see that done < temp.txtline? Your read in the error function is satisfied from temp.txt.
# 7  
Old 06-23-2013
I agree with RudiC; the input to your error() function is coming from the file temp.txt. What I can't understand is why this script works if you use bash instead of sh??? But, since you refuse to show us the output produced when you run this script, refuse to show us the contents of temp.txt, and refuse to show us the results of running this script using the command line:
Code:
/bin/sh -xv name.sh

or with the command line:
Code:
bash -xv name.sh

all that we can say is that the results you're describing (works with bash; fails with sh) do not make any sense with what we have seen. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

2. Shell Programming and Scripting

[Solved] While read line and if statement not working

I'm looking for some help in figuring why my little bit of code will not process any entries other then the first one in my list. while read line ;do hostname=${line//\"} a=`ssh user@$hostname uptime;echo $?` if ];then dt=`date` touch... (6 Replies)
Discussion started by: whegra
6 Replies

3. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

4. Shell Programming and Scripting

If statement is not working in KSH

#! /bin/ksh rm -f ./xyz file --- this line is working // Below any if stmt is not working. if then echo " blah blah " fi or I replaced above if with if then echo "dir exists" fi This is also not working. I am new to KSH. So can someone help why if stmt is not... (31 Replies)
Discussion started by: saggy9583
31 Replies

5. Shell Programming and Scripting

Cshell if statement not working

Hi .I am trying to check the first arguments =-s and the third =-d,but it doesnt work ,any idea why It gives me if: Missing file name Thanks #case -s and files if( $1 == "-s" && $3 != "-d" ) then echo "case s" endif (1 Reply)
Discussion started by: lio123
1 Replies

6. Shell Programming and Scripting

If statement is not working.

Hi. With the help of this group I have created a shell script to find the factorial of a number. OK. Then I got wild.;) I tried to put in a check to make sure the entry is a number. read num If )) then echo "This is not a valid number. Try again." fi while (( $var <= $num)) more... (5 Replies)
Discussion started by: Ccccc
5 Replies

7. Shell Programming and Scripting

Read statement not working in a script

I have a script consisting of certain functions whose input is a file at same location. In that file i have written the name of anothe file at same location. The third file contains a word which act as a function in the first script.Let me give an example i have a scrip file say 1.sh in which i am... (7 Replies)
Discussion started by: sumitdua
7 Replies

8. Shell Programming and Scripting

read statement not working in a function

Pls this is emergency.I have written a script which is taking input from another script. and the contents of my second script are acting as functions to my main script.Now the problem is that in one of the functions i want the script ececution to stop and start when user enters any character r... (2 Replies)
Discussion started by: sumitdua
2 Replies

9. UNIX for Dummies Questions & Answers

until statement not working

im trying to write an until statement which dont go onto the next stage until the user inputs a certain phrase. It is then stored in an array. Ive come up with this code so far but its not working and i dont know why. read in1 until do echo "Incorrect, try again" ... (2 Replies)
Discussion started by: strasner
2 Replies

10. Shell Programming and Scripting

If statement not working

I understand this question probably poses some child like stupidity, but I can't get this if statement to work for love or money. #!/bin/ksh echo "Input either 1 or 2" read Num if ; then echo "Message 1" if ; then echo "Message 2" else echo "false" fi $ ksh decisions Input either 1... (6 Replies)
Discussion started by: Hazmeister
6 Replies
Login or Register to Ask a Question