0

This question already has an answer here:

I am having some trouble running a script to ssh to a remote server. If I simply ssh from the command line I have no trouble reaching the server, but when I run a script to do this I get an error, "Name or service not knownname". I have added the IP address and host name to /etc/hosts and that did not help.

My script is simply:

 #!/bin/bash 
ssh username@hostname

Am I doing something wrong? Obviously I don't need a script just for this, but I'm going to add commands to execute once I get this working.

marked as duplicate by slm Oct 11 '14 at 1:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Operating system? – Jan Oct 10 '14 at 21:12
  • 1
    huh, @val0x00ff is correct, bash acts weird if the script uses dos line ends. dos2unix script works too, if you have it. – artm Oct 10 '14 at 21:49
  • 1
    or @val0x00ff, i guess. – Faheem Mitha Oct 10 '14 at 22:01
  • 1
    @val0x00ff has nailed it, to him should be the honor – artm Oct 10 '14 at 22:03
  • 1
    @val0x00ff, if that was vim you could :set ff=dos and :set ff=unix to turn it ;-) – artm Oct 10 '14 at 22:13
7

The problem with the script is that when copying a file from another system like Windows, it adds a newline \n and a carriage return \r\n. For more about line feeds see newline entry on wikipedia.

To demonstrate the issue I've uploaded short fragment here which explains how to solve the problem.

In short: Use tr to remove those weird line endings

tr -d \\r <yourscript>newscript && mv newscript yourscript

Then run the script again.

Or as artm suggested.

While being in vim editor, use

:set ff=unix

(ff is short for fileformat) and save the file by shift+zz.

  • 2
    you'd better repeat the solution in plain text here, answers in external links tend to rot away with internet. – artm Oct 10 '14 at 22:19
  • tr is fine but requires mv. For less typing, use: sed -i s/\\r// yourscript – hackerb9 Apr 13 at 9:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.