tags:

views:

656

answers:

5
#!/bin/bash
echo 'first line' >foo.xml
echo 'second line' >>foo.xml

I am a total newbie to shell scripting.

I am trying to run the above script in cygwin. I want to be able to write one line after the other to a new file.

However, when I execute the above script, I see the follwoing contents in foo.xml:

second line

The second time I run the script, I see in foo.xml:

second line
second line

and so on.

Also, I see the following error displayed at the command prompt after running the script:

: No such file or directory.xml

I will eventually be running this script on a unix box, I am just trying to develop it using cygwin. So I would appreciate it if you could point out if it is a cygwin oddity and if so, should I avoid trying to use cygwin for development of such scripts?

Thanks in advance.

+1  A: 

But there must be something else that is wrong here. When I try it, it works as expected.

> foo.xml puts the line into foo.xml, replacing any previous contents. >> foo.xml appends to file

jakobengblom2
Are you in cywgin?
Kapsh
A: 

It sounds like you may have a typo somewhere. Also keep in mind that while the Windows command prompt can be forgiving about paths with embedded spaces, cygwin's shells will not be, so if you have a filename that contains embedded spaces, you need to either quote the filename or escape the spaces:

echo 'first line' > 'My File.txt'
echo 'first line' > My\ File.txt

The same goes for certain "special" characters including quotes, ampersand (&), semicolons (;) and generally most punctuation other than period/full-stop (.).

So if you are seeing those issues using the exact script that you are running (i.e. you copy and pasted it, there is no possibility of transcription errors) then something truly strange may be happening that I can't explain. Otherwise, there may be a misplaced space or unquoted character somewhere.

Adam Batkin
A: 

I cannot reproduce your results. The script you quote looks correct, and indeed works as expected in my installation of Cygwin here, producing the file foo.xml containing the lines first line and second line; implying that what you are actually running differs from what you quoted in some way that is causing the problem.

The error message implies some sort of problem with the filename in the first echo line. Do you have some nonprintable characters in the script you are running? Have you missed escaping a space in the filename? Are you subsituting shell variables and mistyping the name of the variable or failing to escape the resulting string?

moonshadow
+4  A: 

Run dos2unix on your shell script. That will fix the problem.

Don Branson
Yes. That was it! Thank you!!
Kapsh
Glad I could help, dude.
Don Branson
+1  A: 

The above should work normally.. However you can always specify a heredoc:

#!/bin/bash
cat <<EOF > foo.xml
first line
second line
EOF
Amro