Read statement not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read statement not working
# 8  
Old 06-23-2013
Mu code output will be
Code:
$sh po.sh

Code:
 
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
# 9  
Old 06-23-2013
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.

Last edited by Don Cragun; 06-23-2013 at 04:37 AM..
# 10  
Old 06-23-2013
i changed while loop with
Code:
 
for i in ' cat temp.txt'

now the problem has been resolved Smilie
but when i press y its calling main it just simply exit out of program.Help me
# 11  
Old 06-23-2013
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.
# 12  
Old 06-23-2013
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.

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