`end of file' unexpected error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting `end of file' unexpected error
# 1  
Old 09-16-2010
`end of file' unexpected error

When I am executing my really simple shell script below, I got an error:
`end of file' unexpected. Script is suppose to print out a list of supplied parameters.
Here is my script:

Code:
#!/bin/sh

a=$#    #number or parameters
b=0      #starting counter

while [ $a > $b ]
do
   b=`expr $b + 1`
   echo $b
   echo "Parameter is $[$b]"
done


Last edited by Scott; 09-16-2010 at 02:01 PM.. Reason: Please use code tags
# 2  
Old 09-16-2010
Hi.

I don't get the same error as you.

> is used for redirection.

Try changing the while [$a > $b ] line to while [ $a -gt $b ]

Code:
$ ./MyTtest a b c
1
Parameter is $[1]
2
Parameter is $[2]
3
Parameter is $[3]

With your code:
Code:
$ ./MyTest a b c
1
Parameter is $[1]
./MyTest: line 6: 1: Is a directory

I would also change b=`expr $b + 1` to b=$((b + 1)) - I'm not much fond of the expr command!
# 3  
Old 09-16-2010
The script as posted prints the message in an infinite loop incrementing $b.

If you just want to print the supplied parameters:

Code:
#!/bin/sh
echo "$*"

# 4  
Old 09-16-2010
I finally opened file in vi and noticed that it has a banch of characters:

^M

What is a quick way to clean it?

Last edited by alexstar; 09-16-2010 at 02:41 PM.. Reason: misspell
# 5  
Old 09-16-2010
This is because you edited/created in a Windows environment and every row has a Line Feed and a CR.

The command to remove this is:
Code:
dos2unix <FileName>

Just one more comment, if you want the opposite, you have:
Code:
unix2dos <FileName>


Last edited by felipe.vinturin; 09-17-2010 at 07:41 AM..
This User Gave Thanks to felipe.vinturin For This Post:
# 6  
Old 09-16-2010
try:
Code:
#!/bin/sh

#a=$#    #number or parameters
b=0      #starting counter

while [ $# -gt $b ]
do
   b=`expr $b + 1`
   echo $b
   echo "Parameter is $[$b]"
done


Last edited by john1212; 09-16-2010 at 08:02 PM.. Reason: thank you frank_rizzo
This User Gave Thanks to john1212 For This Post:
# 7  
Old 09-16-2010
john1212 -
Code:
while [ $# > $b ]

is incorrect. as scottn already stated > is a redirection operator. use -gt.
This User Gave Thanks to frank_rizzo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unexpected End of File Syntax Error

Hi, I am brand new to this so I apologize ahead of time for any formatting problems. I know there is a previous (closed) thread on here about this problem but I wasn't able to fix the error following the posts there so I thought I could create a new one. Here is the error I am getting: ... (3 Replies)
Discussion started by: SierraG
3 Replies

2. Shell Programming and Scripting

Unexpected End Of File Error

Hi guys, I am new to BASH scripting and I was wondering if anyone could have a look at this code and explain to me why I am getting an Unexpected End of File Error ? If you can that would be great / much appreciated! THANKS! #!/bin/bash USER="" PASS="" if ; then echo "You need to set... (1 Reply)
Discussion started by: spooke
1 Replies

3. Shell Programming and Scripting

syntax error unexpected end of file

I am new to unix, so thank u for ur patience I try to make it work (to duplicate two first columns in several files): #!/bin/bash for i in `seq 2 5` do awk `{ print $1,$1,$2,$2,$3,$4}` final_chr.${i} > input_${i} done and i get ./my_script3.sh: command substitution: line 5: syntax... (2 Replies)
Discussion started by: kush
2 Replies

4. Shell Programming and Scripting

Unexpected end of file error

Hi , I am new to Unix and this is my first shell script . I am facing "unexpected end of file error" while executing my code . tried removing blank spaces Unable to trace out the error . PLease help !!! #!/bin/sh echo hello if ] echo hi then var=`cat liq_table_nm.txt` ... (6 Replies)
Discussion started by: sen180185
6 Replies

5. Shell Programming and Scripting

error unexpected end of file

Sometimes while unziping some files i got an error and the further unziping stopped.might be other files are able to be unzip.i.e gunzip a.Z b.Z c.Z gunzip: a.Z: unexpected end of file it found an error on a.Z and stopped but b.Z and C.Z might be correct. why this error comes and is... (2 Replies)
Discussion started by: malikshahid85
2 Replies

6. Shell Programming and Scripting

syntax error: unexpected end of file

Hi, I am newbie to UNIX scripting. I am facing this error "syntax error: unexpected end of file" while executing the following script: ------ a=$1 if then sqlplus -s prospect_stg/prospect_stg@mdmpt <<END insert into bckup_marc_parameter_lookup select * from... (6 Replies)
Discussion started by: boopathyvasagam
6 Replies

7. Shell Programming and Scripting

unexpected end of file error

When I run the following script: #! /bin/bash for var in CA CC CD CT EC PC do cat << EOF > ${var}_full.dat echo T ${var}_high echo C age echo C sex echo C ht echo C WT_lg EOF cat << EOF > ${var}_agesex.dat ... (4 Replies)
Discussion started by: polly_falconer
4 Replies

8. Shell Programming and Scripting

unexpected end of file error

hi, i am trying to connect to sqlplus in an 'if block' from the script. it is giving unexpected end of file error. and it works fine if it is out of 'if block'. if anybody have idea on this, can you please help me to solve the error ? piece of code is given below. if then... (11 Replies)
Discussion started by: vinayakatj56
11 Replies

9. Shell Programming and Scripting

syntax error: unexpected end of file

Hi, I need ur help is this matter, i have th ebelow script, and i keep getting this error: syntax error: unexpected end of file affectedRow=`cat dbOutput.log | grep "1 row affected"` echo "affectedRow : $affectedRow" if ; then echo "Look to the next OMCDB" ... (10 Replies)
Discussion started by: Alaeddin
10 Replies

10. Shell Programming and Scripting

End of file unexpected error

Hi, I am new to unix. I have written a script whenever i am giving sh filename.sh its prompting unexpected end of file in line number 40 in that line i have echo statement what do i need to do thanks in advance (5 Replies)
Discussion started by: hamsa
5 Replies
Login or Register to Ask a Question