Testing for EOF during a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Testing for EOF during a while loop
# 8  
Old 10-07-2010
This command should tell you what Operating System you have on any unix or Linux platfrom:
Code:
uname -a


And the Shell (works on most modern platforms):
Code:
echo ${SHELL}


Last edited by methyl; 10-07-2010 at 11:11 PM.. Reason: layout
# 9  
Old 11-14-2010
Hi,

I am new to shell scripting, and I was trying the same thing where I want to read whether I have entered more than one line or not,
i.e. I have a command called grader
Code:
grader "add1
> add2"

shoudl give me a reply of 1, but it gives me output of 0

but If I write
Code:
grader
>add1
>add2

and then Ctrl + d it gives me output of "2"

please let me know what can be done if I want to even count within quotes that it is a name with new line in it.

I have used the same code as posted here
Code:
#!/bin/bash

i=0
while read ans
do 
  i=$(( i+1 ))
done
echo $i

Moderator's Comments:
Mod Comment In future, please start a new thread instead of hijacking someone else's. Thanks

Last edited by Scott; 11-14-2010 at 12:46 PM.. Reason: Please use code tags
# 10  
Old 11-14-2010
The code you have is designed to prompt and accept lines on standard input. The first example you give (the one with the quotes) is passing parameters to the program not input. The following should be used instead:

Code:
$ echo "add1
> add2" | grader
2

# 11  
Old 11-14-2010
If you make a small adjustment to your script then it should be able to read the input on the command line.
Code:
#!/bin/bash
exec 3<&0
if [ $# -gt 0 ]; then
  exec 0<<<"$@"
fi
i=0
while read ans
do
  i=$(( i+1 ))
done
echo $i
exec 0<&3

Code:
$ ./grader "add1
add2"
2


Last edited by Scrutinizer; 11-14-2010 at 02:00 PM.. Reason: added exec and put exec 3<&0 before if...
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 11-14-2010
Code:
#!/usr/bin/ksh
while read ans?"Enter something:"
do
if [[ -n $ans ]]; then let i++
else break
fi
done
echo "\nYou answered $i times"


Last edited by ctsgnb; 11-14-2010 at 12:31 PM..
This User Gave Thanks to ctsgnb For This Post:
# 13  
Old 11-14-2010
---------- Post updated at 11:41 AM ---------- Previous update was at 11:39 AM ----------

Quote:
Originally Posted by Scrutinizer
If you make a small adjustment to your script then it should be able to read the input on the command line.
Code:
#!/bin/bash
if [ $# -gt 0 ]; then
  exec 3<&0 0<<<"$@"
fi
i=0
while read ans
do
  i=$(( i+1 ))
done
echo $i
0<&3

Code:
$ ./grader "add1
add2"
2


Thank you Scrutinizer that worked!!

though, I did not understand what does this do?
Code:
exec 3<&0 0<<<"$@"

with <<< are you trying to shift?

and why did you use
Code:
0<&3

in the end? even if I don't have it, also then it works.

Thanks!!

Last edited by Scott; 11-14-2010 at 12:47 PM.. Reason: Code tags
# 14  
Old 11-14-2010
<<< is a so-called here-string, which is a ksh93/bash construct. It redirects stdin from a string.

Regarding 0<&3 you could have used:
Code:
#!/bin/bash
if [ $# -gt 0 ]; then
  0<<<"$@"
fi
i=0
while read ans
do
  i=$(( i+1 ))
done
echo $i

But then stdin would be no longer usable later on in your script. That is why I used file descriptor 3 to save where stdin was reading from and I redirected stdin to its old value at the end..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

help using EOF inside a for loop

I'm having problems with this example: #!/bin/bash for i in server1 server2 server3 do ssh $i <<EOF >> logfile.txt dat1=`date '+%m/%d/%y'` hst=`hostname` df -Ph | awk -v OFS=',' -v dat="$dat1,$hst" ' {print dat,$1,$6,$3,$4 } ' exit EOF done I'm... (1 Reply)
Discussion started by: kuliksco
1 Replies

2. Shell Programming and Scripting

confused with << EOF EOF

Hi friends , I am confused with << EOF EOF Most of the cases I found sqlplus $db_conn_str << EOF some sql staments EOF another exapmle is #!/bin/sh echo -n 'what is the value? ' read value sed 's/XXX/'$value'/' <<EOF The value is XXX EOF (1 Reply)
Discussion started by: imipsita.rath
1 Replies

3. Shell Programming and Scripting

Until eof do????

Hey! Can I write a routine that allows me to in a txt file check line by line until the end of file? something like until do ---some code--- done maybe it is a stupid question but I never learned shell scripts and I need this :p thanks in advance (1 Reply)
Discussion started by: ruben.rodrigues
1 Replies

4. Programming

Eof

How can I write EOF into a file? I tryed to do as follows: size=sizeof(EOF); end_of_file=EOF; write(fdMutexRichieste, &end_of_file, size); But it does non work correctly, becouse in the next cicle (using lseek(..., SEEK_END) of my code it seems to ignore my EOF and use the LAST... (5 Replies)
Discussion started by: DNAx86
5 Replies

5. UNIX for Dummies Questions & Answers

Testing For Loop condition

How do I test the return condition in the script if no files are found: for file in `Find ${LANDING_FILE_DIR}${BTIME_FILENAME_PATTERN}` do ... .. done I want to capture the return code so I can echo the error or condition. Using if ] always returns zero no matter where it's placed. ... (4 Replies)
Discussion started by: mavsman
4 Replies

6. Shell Programming and Scripting

What is << EOF

I'm trying to connect to oracle with the following code in the script: checksqlerror{ if echo $1 exit fi } $SQLPLUS username/password@schemaname checksqlerror "failed to connect to oracle" If I'm passing wrong schema name,instead of executing checksqlerror it stops and expects user... (2 Replies)
Discussion started by: BhawanaAggarwal
2 Replies

7. Shell Programming and Scripting

Eof

hi, in a shell script i came accross the following bit of code 1.shift $(($OPTIND - 1)) 2.if ; then 3. cat << EOF >&2 4.Usage: $0 lockfilename 5.EOF 6. exit 1 7.fi I am not able to understand the meaning of lines(1,3,5). Can any one of u tell me the purpose of above said lines.... (1 Reply)
Discussion started by: ravi raj kumar
1 Replies

8. UNIX for Dummies Questions & Answers

Eof

hello all, how end of a file is detected in UNIX system? does it make use of any special symbols to identify the EOF? :( thank you all (5 Replies)
Discussion started by: compbug
5 Replies

9. Shell Programming and Scripting

EOF use

Hi, I'd like to access a windows directory from aix with samba client. To allow direct access (not interactive), i'm using EOF like: smbclient \\\\winserver\\windir 'passwd' -U usersmb << EOF cd subwindir put myfile EOF The access is correct but does somebody know how to trap errors... (1 Reply)
Discussion started by: jo_aze
1 Replies
Login or Register to Ask a Question