tags:

views:

232

answers:

5

I am facing a problem in a bash shell script. This script is supposed to execute another shell script (./script here) and the output of the script is redirected to a file(tmp). Then the file should be read line by line and for each of the lines the same script(./script) should be executed giving the line as argument and the result should be stored in a file(tmp1). Eventually these results should be appended to the first file(tmp).

I am pasting my script below:

./script $1 $2 > tmp
cat tmp | while read a
do
 ./script $a $2 >> tmp1
done

cat tmp1 | while read line
do
 ./script $line $2 >> tmp
done

I get the following error when I execute the script "./script: line 11: syntax error: unexpected end of file"

Can anyone please help me out in this??

Thanks a lot in advance.

A: 

The shell splits on whitespace normally. You can make it split on newlines by writing, towards the top,

IFS='
'

However, I think what you're trying to do might not be appropriate for shell, and might be better served by Perl or Ruby.

Pi
hey.. thanks for your reply. The problem with Perl or Ruby is that I do not know any of those two languages :P, so it will take some time learning them.
assassin
what you want to do can be done with the shell, if you don't know other languages, stick to shell.
ghostdog74
hey... using your solution, I get the following errors": command not found: ./wiki_tree: line 12: syntax error near unexpected token `done'./wiki_tree: line 12: `done'"
assassin
@ghostdog74: From the description given, it's a recursive shell script, which is a little boggling.@assassin: Yeah, gonna need some more information here, then.
Pi
+1  A: 

lose all the cats! they are unnecessary. And i suppose summ_tmp is an existing file?

#!/bin/bash
set -x
./wiki $1 $2 > tmp
while read -r a
do
    ./wiki $a $2 >> tmp1
done < summ_tmp

while read -r line
do
    ./wiki $line $2 >> tmp
done < tmp1

With what you are doing, you might want to refactor your "./script" to eliminate unnecessary steps. If its not too long, show what your "./script" does. Show your desired output and show examples of relevant input files where possible

Put set -x in your script (wiki and ./script) to help you debug.

ghostdog74
Hi,I get the following error when I execute your script:./wiki_tree: line 5: syntax error near unexpected token `done' '/wiki_tree: line 5: `done < summ_tmp
assassin
comment out all the while loops, and execute only wiki. What did you see? Show all relevant information, like what is wiki, and script.
ghostdog74
after commenting out all the while loops, it works.... btw ./wiki and ./script are the same (I m sorry fr this mistake). I renamed script to wiki...
assassin
script (or wiki, watever) calls a runs a java class file.
assassin
set -x... ummm could you please temme where should I put it? I am pretty new to shell scripting.
assassin
so add in the while loops one at a time and run the script until the point error occurs. show your java output if possible. As for the set -x, see my edit. Is your wiki script a full shell script or does it run only one java command? If its a full shell script, put set -x as well.
ghostdog74
for the set -x line, this is the error I get:: invalid optione 3: set: -set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]Rest other errors are the same as before
assassin
set -x is a bash option. If you have error, then you are not using bash. check your shebang make sure its #!/bin/bash. tell us what shell you are using.!
ghostdog74
As far as I know, I am using bash shell... I checked ps -x for that... but what is shebang? and where is it?
assassin
see my code. its the first line. #!/bin/bash. make sure you have it in all your scripts if you are calling your script with dot slash (./script)
ghostdog74
I get this error if I put that in the first line.: bad interpreter: No such file or directory
assassin
on the command line, type `which bash`. use that path instead of /bin/bash if its different.
ghostdog74
I typed the command which bash and got the output as "/bin/bash" !!
assassin
why don't you show how you run your scripts!!
ghostdog74
"bad interpreter"? Your OS doesn't want to execute bash scripts. That always gets in the way of getting bash to solve your problems, you see? ghostdog's code is righteous (though I follow the way of `while read -a`). An idea: what does `ls -l $(which bash)` return? And another: what does `cat /etc/shells` return?
Charles Stewart
A: 

Alternatively you could use xargs - this executes a command on every line in a file, which is exactly what you want.

You can replace

cat tmp1 | while read line
do
    ./script $line $2 >> tmp
done

with

xargs <tmp1 -n1 -IXXX ./script XXX $2 >>tmp

-n1 means read one line at a time from the input,

-IXXX means substitute XXX with the line that was read in - the default is to append it to the end of the command line.

Dave Kirby
it still shows this error: line 5: syntax error: unexpected end of file. How do we denote that our script has ended in bash scripting?? Maybe thats the problem
assassin
@OP, put set -x in your script to help you debug
ghostdog74
set -x... ummm could you please temme where should I put it? I am pretty new to shell scripting
assassin
A: 

What is on line 11 of ./script?

Randy Proctor
+2  A: 

The script file has DOS/Windows line endings. Try this command, then run your script:

dos2unix ./script

You're probably editing the file using a Windows editor and that's adding \r (0x0d) to the end of each line. This can be removed by using dos2unix.

Dennis Williamson
i didn't think of that, and you might be correct.
ghostdog74
@ghostdog74: I created a test script consisting of a `while` loop and ran the reverse (`unix2dos`) on it. When I ran the result under `dash` and `bash` they both gave unexpected EOF errors.
Dennis Williamson