Read statement not working in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read statement not working in a script
# 1  
Old 01-06-2010
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 reading the contents of file b line wise. let say contents of b is Run Pause. Pause is a third file whose content is Suspend.
so in 1.sh i am extracting the keyword Suspend and then calling a script halt .sh who content is

Code:
echo "Press y to continue"
while read ans
do
  if [ $ans = 'y' ]
  then
    exit 1
  else
    echo "Incorrect entry"
  fi
done
echo "Exiting"

Now here the script doesnt stop at read and continue executing till the last.Pls tell what should i do in this case?

Last edited by Franklin52; 01-06-2010 at 04:12 AM.. Reason: Please indent your code and use code tags!
# 2  
Old 01-06-2010
Hi,

I have tried your code and its executing fine..
According to your code when you type y(input) it is exiting and when you type any different character other than y it is in the continuous loop.
# 3  
Old 01-06-2010
hi Anjan1
have u tried executing the code in the same way as i have described. bcoz if you execute it directly it works fine but if you do it as i have tod its giving problem.
try it in this wway
make i file 1.sh having content

cat $1 | while read LINE
arg=`echo $LINE | cut -f 1 -d' '`
if [ $arg = 'Run' ]
then
cat $arg | while read LINE1
arg1= `echo $LINE1 | cut -f 1 -d' '`
if [ $arg1 = 'Suspend' ]
then
sh halt.sh
fi
fi

make a file a having content
Run Pause

Make a file Pause having content
Suspend

now execute
1.sh a
# 4  
Old 01-06-2010
Hi Sumit,

Can you please tell us for what u r using $1 and why u r trying to open that $1. So that it will easy for us to valiadate the code.

Thanks,
Subhendu
# 5  
Old 01-06-2010
Your code is not complete .. you didn't put the "do&done" in while syntax as
below in red bold font

Code:
cat $1 | while read LINE
do
arg=`echo $LINE | cut -f 1 -d' '`
if [ $arg = 'Run' ]
then
cat $arg | while read LINE1
do
arg1= `echo $LINE1 | cut -f 1 -d' '`
if [ $arg1 = 'Suspend' ]
then
sh halt.sh
fi
done
fi
done


Last edited by ahmad.diab; 01-06-2010 at 07:14 AM..
# 6  
Old 01-06-2010
hi all
that $1 is actually file with name a whose contents i have mentioned.
what i am trying here is that i am reading the contents of file a line by line and then taking appropriate action depending upon the contents of a.


P.S: regarding 'done; in my code,it is there.I forgot to add it in the code i published here.
# 7  
Old 01-08-2010
Guys
Pls help me in this.
I havnt got the solution yet.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Why this script is not working as 'expected' when doing ssh with while read ... really confused?

Hi, I have a script below that is running ssh <host> <command> on some servers. Below is more or less the script. I have to modify it somehow to get rid of the 'confidential' hostnames check_log.bash #!/bin/bash # myPID=$$ parse_log () { sub="parse_log" host=${1} ... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. 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

3. 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

4. Shell Programming and Scripting

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 ... (11 Replies)
Discussion started by: mohanalakshmi
11 Replies

5. 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

6. Shell Programming and Scripting

Read SQL statement in Script

Hi Guys.. need some urgent help... I am stuck in something badly I need to write a script which would read a sql statement (which might be a join/inner join/select/sub select etc. ) I need to read that sql statement ... and in the output I want all the table names and columns (doesn't... (4 Replies)
Discussion started by: freakygs
4 Replies

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

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