LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Why is "\r" appended to my filename and does it matter? (https://www.linuxquestions.org/questions/linux-newbie-8/why-is-%5Cr-appended-to-my-filename-and-does-it-matter-885300/)

linuxneub 06-08-2011 07:17 PM

Why is "\r" appended to my filename and does it matter?
 
Hi every,

I am new to Linux, but was put in charge of our company's Amazon presence.

My problem is that all of the sudden, a shell script we have been using has stopped working, and returned an odd error that I simply can't decipher.

The shell script is pretty simple, and is run on our crontab (however manually running it causes the same error as well). The script name is "sendh.sh" and to run it in Linux is simply typing "sh sendh.sh". Here it is:
---------------------------------------------------
rm /home/websync/amazon/rhamus.txt
rm /home/websync/amazon/rhamuk.txt
rm /home/websync/amazon/rhamde.txt
rm /home/websync/amazon/rhamfr.txt
perl /home/websync/amazon/printus.pl >> /home/websync/amazon/pr.log 2>&1
perl /home/websync/amazon/printuk.pl
perl /home/websync/amazon/printde.pl
perl /home/websync/amazon/printfr.pl
sh /home/websync/amazon/sendus.sh
sh /home/websync/amazon/senduk.sh
sh /home/websync/amazon/sendde.sh
sh /home/websync/amazon/sendfr.sh
--------------------------------------------------


However, when I run it, I get the following output and it doesn't do any of the commands listed:
-----------------------------------------------------
apollo:/home/websync/amazon# sh sendh.sh
rm: cannot remove `/home/websync/amazon/rhamus.txt\r': No such file or directory
rm: cannot remove `/home/websync/amazon/rhamuk.txt\r': No such file or directory
rm: cannot remove `/home/websync/amazon/rhamde.txt\r': No such file or directory
: ambiguous redirect
": No such file or directorye/websync/amazon/printuk.pl
": No such file or directorye/websync/amazon/printde.pl
: No such file or directoryndus.sh
: No such file or directorynduk.sh
: No such file or directoryndde.sh
apollo:/home/websync/amazon#
-------------------------------------------------

Yes, obviously those files are in the directory, I can see them through the "ls" command (or through WinSCP as well). When run one by one, each command works fine, it's just when using this "sendh.sh" shell script then it fails.

Does anyone know why "\r" is appended to the filenames when I try to run it, or what the heck "ambiguous redirect" means?

I've tried searching Google and can't find anything similar.

Thank you very much for your time!

crts 06-08-2011 07:46 PM

Looks like the file has been edited under windows. Windows line-ending is '\r\n' while linux line-ending is just '\n'.
Run
dos2linux sendh.sh

and then try again.

linuxneub 06-08-2011 08:05 PM

Hi crts,

Thank you so much for the quick reply. Though "dos2linux" was not recognized in my system (I assume maybe I have to install it somehow?) the following command from https://linuxishbell.wordpress.com/2...o-unix-format/
worked


perl -pi -e 's/\r\n/\n/g' input.file


your advice that the file has been edited under Windows was correct, that is how I edit all my perl and sh files... though I am now not sure if this is the best way? I have to ssh into the Linux machine with the crontab and scripts through my Windows machine, and hence I just grab the files off of it and play with them on Windows. Again I am beginner-user so any tips I can learn the better!

Thank you again for your help!


Quote:

Originally Posted by crts (Post 4380425)
Looks like the file has been edited under windows. Windows line-ending is '\r\n' while linux line-ending is just '\n'.
Run
dos2linux sendh.sh

and then try again.


Tinkster 06-08-2011 08:14 PM

You could conceivably use wordpad to edit those files. It can handle
Unix line-endings, and I believe it leaves them unchanged when saving.
Or get something like Notepad++

Alternatively familiarise yourself with one of the many editors
available in Linux, and edit on the machine.



Cheers,
Tink

linuxneub 06-08-2011 08:17 PM

Hi Tinkster,

Oops sorry, I forgot to mention that I use Notepad++ for all my files that go onto the Linux machine.

Thanks!


Quote:

Originally Posted by Tinkster (Post 4380442)
You could conveivably use wordpad to edit those files. It can handle
Unix line-endings, and I believe it leaves them unchanged when saving.
Or get something like Notepad++

Alternatively familiarise yourself with one of the many editors
available in Linux, and edit on the machine.



Cheers,
Tink


Tinkster 06-08-2011 08:24 PM

Quote:

Originally Posted by linuxneub (Post 4380444)
Hi Tinkster,

Oops sorry, I forgot to mention that I use Notepad++ for all my files that go onto the Linux machine.

Thanks!


Well ... notepad++ definitely has the option to save w/ Unix line endings;
many of our testers here use it to create text files for use w/ Linux.



Cheers,
Tink

grail 06-08-2011 09:12 PM

Quote:

Again I am beginner-user
And a large percentage of us were at one time or another :)

Seeing as you are ssh'ing into the machine, you could just edit the files inside your ssh shell.
Do you have access to an editor, perhaps vi or vim or emacs?

linuxneub 06-08-2011 09:55 PM

Hmmm. I am not sure... I used "pico" to view the file on the Linux machine, I guess I can try using that more often. Would definitley save time having to send files back and forth through FTP, but at the same time I like using FTP because it lets me back things up on my Windows machine before I start making changes willy-nilly.

Quote:

Originally Posted by grail (Post 4380469)
And a large percentage of us were at one time or another :)

Seeing as you are ssh'ing into the machine, you could just edit the files inside your ssh shell.
Do you have access to an editor, perhaps vi or vim or emacs?


crts 06-09-2011 12:37 AM

Quote:

Originally Posted by linuxneub (Post 4380436)
Hi crts,

Thank you so much for the quick reply. Though "dos2linux" was not recognized in my system (I assume maybe I have to install it somehow?) the following command from https://linuxishbell.wordpress.com/2...o-unix-format/
worked


perl -pi -e 's/\r\n/\n/g' input.file


your advice that the file has been edited under Windows was correct, that is how I edit all my perl and sh files... though I am now not sure if this is the best way? I have to ssh into the Linux machine with the crontab and scripts through my Windows machine, and hence I just grab the files off of it and play with them on Windows. Again I am beginner-user so any tips I can learn the better!

Thank you again for your help!

You are welcome. Another alternative to convert from dos to unix:
Code:

sed 's/\r$//' input.file > output.file

grail 06-09-2011 01:52 AM

Quote:

before I start making changes willy-nilly.
Well it is always well advised to make a backup before using the 'willy nilly' approach :)

Just as easy to perform a cp prior to editing. Also, as with the solutions presented above, if you are making simple changes
to only portions of the script, you can use a tool like sed which can edit the file in place and make a backup at the
same time if required (see -i option)

linuxneub 06-10-2011 02:10 AM

oooohhh good suggestions! I will try these soon as I make a new sh script to run a bunch of other scripts....

thanks again!


Quote:

Originally Posted by grail (Post 4380634)
Well it is always well advised to make a backup before using the 'willy nilly' approach :)

Just as easy to perform a cp prior to editing. Also, as with the solutions presented above, if you are making simple changes
to only portions of the script, you can use a tool like sed which can edit the file in place and make a backup at the
same time if required (see -i option)



All times are GMT -5. The time now is 11:06 PM.