LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2010, 04:52 AM   #1
adidoosh100
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Rep: Reputation: 0
syntax error: unexpected end of file


Hi,
I started writing bash script and got this error.
My script is:

#!/bin/bash
cat $1 |while read line; do echo "${line}"
done
echo "hello world" >> "Result.txt"


Why do I get this error and how do I fix it?

Thank you,
Adi.
 
Old 08-07-2010, 05:09 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,796

Rep: Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065
I ran your code as is and had no issues. Also, when posting code please use [code][/code] tags.

Try the following and see how you go:
Code:
#!/bin/bash

while read line
do
    echo "$line"
done<$1

echo "hello world" >> Result.txt
Firstly, as you can see it is a little clearer with some spacing and indentation.

Moving the $1 away from the cat pipe allows you to not get caught later if you use a variable in the loop and need to use it outside.
 
Old 08-07-2010, 05:39 AM   #3
adidoosh100
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hi,
thank you for your reply.

I wrote this code now:

Code:
#!/bin/bash
cat $1 |while read line; do echo "$line"
done <$1
and I get an error "No such file or directory", but without the <$1 the script runs OK (but then I can't add more commands because I get the previous error).

Please help me.

Thank you.
 
Old 08-07-2010, 05:49 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399
Hi,

You seem to mix your first approach and grail's approach.

It is either:
Code:
#!/bin/bash
cat $1 |while read line; do echo "$line"
done
or:
Code:
#!/bin/bash
while read line; do echo "$line"
done <$1
Your code in post #3 uses a combination of both ($1 is used twice as input).

Hope this helps.
 
Old 08-07-2010, 06:03 AM   #5
adidoosh100
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hi,
your second example still gets an error of 'No such file or directory'.
 
Old 08-07-2010, 06:09 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,796

Rep: Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065
Quote:
your second example still gets an error of 'No such file or directory'
So how are you running the script? You have indicated, by using $1, that you are passing the name of a file.

This message is quite clear in stating that the file you are attempting to use does not exist.
 
Old 08-07-2010, 06:14 AM   #7
adidoosh100
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
The file does exist. The first example works just fine (but when I add the code echo "hello world" >> "Result.txt" - I get the error message in my first message).
 
Old 08-07-2010, 06:27 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,796

Rep: Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065
Try it without the quotes around the file you are appending too.
 
Old 08-07-2010, 08:29 AM   #9
adidoosh100
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hi,
thank you all.
Now i've got another problem.
I wrote this code in the While loop:
Code:
FirstComma=`expr index $line ","`
FirstComma=`expr $FirstComma + 1`
I get an error "non-integer argument", although the value of the argument is 2.
How can I make the script "know" that this is an integer?

Thanks!
 
Old 08-07-2010, 08:46 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,796

Rep: Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065
Well seeing that you are running through a loop I would be guessing (seeing as you have never shown) that your input file has a line without a comma.
 
Old 08-07-2010, 09:29 AM   #11
adidoosh100
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hi,
fixed it.
Now I've got a new problem

I defined few params at the begining of the script.
In the loop I changed their values, and outside the loop I printed these values.
Inside the loop - when I echo the values - the values are OK, but outside the loop - the values doesn't change and remain like they were before the loop.

Can anyone explain me why?

Thank you.
 
Old 08-07-2010, 10:07 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,796

Rep: Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065
Maybe you should re-read my first post as you are obviously still using the cat pipe solution you started with.
 
Old 08-07-2010, 10:15 AM   #13
adidoosh100
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Yeah, You're right.
I've changed that and now it all works great.

Thank you all!
 
Old 08-07-2010, 11:27 PM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,796

Rep: Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065
Cool ... please mark as SOLVED
 
Old 08-08-2010, 12:44 AM   #15
adidoosh100
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
No problem.
FYI, all of my problems were solved after running the dos2unix command.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
why i get syntax error: unexpected end of file ?? freephys Linux - Newbie 3 06-24-2010 09:03 AM
-bash: *.sh: line 25: syntax error: unexpected end of file prashanth212 Linux - General 8 04-05-2010 11:52 PM
bash line 74: syntax error: unexpected end of file help? andycol Linux - General 5 09-14-2009 08:12 AM
syntax error: unexpected end of file ygdrazil Linux - Newbie 3 07-23-2009 05:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration