Read statement not working

hello guys,
i am having the below piece of 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:

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 .

Hi thanks for the response :slight_smile: i am using

#!/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.:confused:

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:
    text /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?

#!/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

When first reading the problem, I guessed - and then was confirmed by the script - that input was redirected. Do you see that done < temp.txt line? Your read in the error function is satisfied from temp.txt.

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:

/bin/sh -xv name.sh

or with the command line:

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. :frowning:

Mu code output will be

$sh po.sh
 
Enter PO Numbers to create text file with entetred PO. Please type 'exit' when you finished entering
12345678
exit
Checking PO's wait for a while!!!!!!
12345678
1
9
Error in PO,contains more than 7 char in line number 1 with  9 characters
Do you want to enter PO again(Y/N):
1

In the above one i need to check the input untill it match 7 char
but when its more than 7 i need give option y/Y pompt not waiting for user input

You have been told multiple times that error() is reading from temp.txt rather than from your keyboard. Having you repeat that error() is not reading from your terminal is not going to change the fact that you cannot have an interactive discussion with a user at a terminal while you are reading input from a file instead of from the terminal.

i changed while loop with

 
for i in ' cat temp.txt'

now the problem has been resolved :slight_smile:
but when i press y its calling main it just simply exit out of program.Help me

I repeat: You cannot have an interactive discussion with a user at a terminal while you are reading input from a file instead of from the terminal.

The logic of your script makes no sense.

Do you want to read a list of PO numbers from a file and save the subset of those PO numbers that meet your validation criteria in a file ( $current/PO_NO.txt )? Or, do you want to have an interactive session with someone sitting at a terminal typing in PO numbers, saving PO numbers they type in that meet your validation criteria in the same file?

Choose one of the above! The mixed approach you are currently using cannot work.

Processing the list of validated PO numbers is a separate problem we can address after you come up with a way to get a list of validated PO numbers.

Here is an alternative to your script that I think does what you were trying to do. It will interactively read a list of PO numbers if standard input for the script is a TTY, and will read a list of PO numbers from a pipe or regular file without prompting if the input is not a TTY. Each line of input will be checked to see if it contains 7 non-space characters and if it does, it will be added to a file containing validated PO numbers; otherwise the reason why the line was not accepted will be written to standard output.

This script will not work with /bin/sh on a Solaris 5.10 system, but will work with any ksh , bash , or other POSIX-conforming shell (including /usr/xpg4/bin/sh and /usr/xpg6/bin/sh on Solaris systems). This script assumes that the directory you specified by the variable current contains a subdirectory named PO_bk (which was present in comments in your script) and saves the the previous run's results in a file in that directory with the backup file's name including the date AND TIME when the backup is created.

#!/bin/ksh
set -f
IFS=""
current=/home_dir/a541101
#current="$PWD"
#DATE=$(date +"%m-%d-%y")
DATE=$(date +"%m-%d-%y@%H:%M:%S")
outf="$current/PO_NO.txt"       # Output file to hold validated PO#s
if [ -f "$outf" ]
then    mv "$outf" "$current/PO_bk/PO_NO_$DATE.txt"
fi
ilc=0                           # input line count
poc=0                           # validated PO count
if [ "not a tty" = "$(LC_ALL=POSIX tty)" ]
then    PA=''
else    PA='Enter PO# (exit or CTL-d when done): '
fi
while printf "$PA" && read PO
do      ((ilc++))
        if [ "X$PO" = "Xexit" ]
        then    break
        fi
        if [ "X$PO" != "X${PO#* }" ]
        then    printf "Error: \"%s\" on line %d contains a <space>\n" \
                        "$PO" $ilc
        elif [ ${#PO} -ne 7 ]
        then    printf "Error: \"%s\" on line %d is %d characters (not 7)\n" \
                        "$PO" $ilc ${#PO}
        else    printf "PO \"%s\" on line %d validated\n" "$PO" $ilc
                echo "$PO" >> "$outf"
                ((poc++))
        fi
done
printf "\n%d validated PO number(s) are saved in %s\n" $poc "$outf"
if [ ! -f "$outf" ]
then
        echo "No valid PO numbers entered."
        exit 1
fi
echo "Triggering the PO Publish script..."

Hopefully this will give you some ideas that you can use to create a script that will work for you.