I have created a sh script that basically what it does is read a plain text file in which is the ip, the user and the password of the servers to which I want to send the public key to connect to them without requiring a password for issues of automation with ansible and vagrant.
#!/bin/sh
file="$1"
lines=$(cat $file)
file_tr=`cat $file | tr " " ","`
for line in $file_tr
do
ip=`echo $line | cut -d "," -f1`
user=`echo $line | cut -d "," -f2`
pass=`echo $line | cut -d "," -f3`
sshpass -p $pass ssh-copy-id -i ~/.ssh/id_rsa.pub -oStrictHostKeyChecking=no $user@$ip
done
I have done this script with visual studio code in Ubuntu 20.04 and I have run it on it without any problem, that is, it worked perfectly.
Later I downloaded my repository where this script is in windows 10 and I opened the repository with visual studio code, then with visual studio code I opened a terminal with git bash and I did ssh to a ubuntu 20.04 server, automatically deployed with vagrant and with the folder shared repository on this server, I ran the script and it detects characters that do not exist in the script itself. As we see here.
vagrant@server1:~$ sh multi-copy-ssh-public-key.sh hosts0.txt
cat: 'hosts0.txt'$'\r': No such file or directory
cat: 'hosts0.txt'$'\r': No such file or directory
multi-copy-ssh-public-key.sh: 6: Syntax error: word unexpected (expecting "do")
I was doing the same process with ubuntu 20.04 with a graphical environment and it didn't give me any error. This error that is giving me does not make any sense since I do not see those characters in the script that I have written. So please, someone explain it to me because I am getting desperate and this is the strangest thing that has happened to me so far as a computer scientist.
I have done everything like copy and paste the script in the notepad, try to delete some characters and the only solution has been to rewrite the script again, exactly the same and it works. All I want is to know why this strange thing happened to me.