debain运行脚本报错,把脚本中的命令单独执行就没有问题

讨论 未结 5 37
nieyuli
nieyuli 会员 2022年11月15日 06:50 发表
今天拿一台机器dd了一个debain11,想要用一键脚本设置秘钥登录,因为国内鸡连不上github,几把脚本内容复制下来粘贴到记事本当中上传到小鸡上,并修改后缀名为*.sh<br> 然后运行该脚本总是报错,但是吧脚本内容单独拿出来执行却又没有问题,请问大佬指教一下是什么原因?用的是这个一键脚本:https://github.com/KiritoMiao/SSHKEY_Installer/<br> <br> 以下是脚本内容:<br> <div class="blockcode"> <div id="code_Zru"> <ol> <li>#/bin/sh<br> </li> <li>apt-get update -y<br> </li> <li>apt-get install curl -y<br> </li> <li>yum clean all<br> </li> <li>yum make cache<br> </li> <li>yum install curl -y<br> </li> <li>echo '============================<br> </li> <li>&nbsp; &nbsp;&nbsp; &nbsp;SSH Key Installer<br> </li> <li>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;V1.0 Alpha<br> </li> <li>&nbsp; &nbsp; &nbsp; &nbsp; Author:Kirito<br> </li> <li>============================'<br> </li> <li>cd ~<br> </li> <li>mkdir .ssh<br> </li> <li>cd .ssh<br> </li> <li>curl https://github.com/$1.keys &gt; authorized_keys<br> </li> <li>chmod 700 authorized_keys<br> </li> <li>cd ../<br> </li> <li>chmod 600 .ssh<br> </li> <li>cd /etc/ssh/<br> </li> <li><br> </li> <li>sed -i "/PasswordAuthentication no/c PasswordAuthentication no" sshd_config<br> </li> <li>sed -i "/RSAAuthentication no/c RSAAuthentication yes" sshd_config<br> </li> <li>sed -i "/PubkeyAuthentication no/c PubkeyAuthentication yes" sshd_config<br> </li> <li>sed -i "/PasswordAuthentication yes/c PasswordAuthentication no" sshd_config<br> </li> <li>sed -i "/RSAAuthentication yes/c RSAAuthentication yes" sshd_config<br> </li> <li>sed -i "/PubkeyAuthentication yes/c PubkeyAuthentication yes" sshd_config<br> </li> <li>service sshd restart<br> </li> <li>service ssh restart<br> </li> <li>systemctl restart sshd<br> </li> <li>systemctl restart ssh<br> </li> <li>cd ~<br> </li> <li>rm -rf key.sh</li> </ol> </div><em onclick="copycode($('code_Zru'));">复制代码</em> </div><br> <br> ============================<br> <br> <br> 运行报错内容:<br> <div class="blockcode"> <div id="code_VFf"> <ol> <li>root@debian:~# bash key.sh<br> </li> <li>E: Command line option '<br> </li> <li>&nbsp; &nbsp;' [from -y<br> </li> <li>&nbsp; &nbsp;] is not understood in combination with the other options.<br> </li> <li>E: Command line option '<br> </li> <li>&nbsp; &nbsp;' [from -y<br> </li> <li>&nbsp; &nbsp;] is not understood in combination with the other options.<br> </li> <li>key.sh: line 4: yum: command not found<br> </li> <li>key.sh: line 5: yum: command not found<br> </li> <li>key.sh: line 6: yum: command not found<br> </li> <li>============================<br> </li> <li>&nbsp; &nbsp;&nbsp; &nbsp;SSH Key Installer<br> </li> <li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;V1.0 Alpha<br> </li> <li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Author:Kirito<br> </li> <li>============================<br> </li> <li>key.sh: line 12: cd: $'~\r': No such file or directory<br> </li> <li>mkdir: cannot create directory ‘.ssh\r’: File exists<br> </li> <li>&nbsp;&nbsp;% Total&nbsp; &nbsp; % Received % Xferd&nbsp;&nbsp;Average Speed&nbsp; &nbsp;Time&nbsp; &nbsp; Time&nbsp; &nbsp;&nbsp;&nbsp;Time&nbsp;&nbsp;Current<br> </li> <li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Dload&nbsp;&nbsp;Upload&nbsp; &nbsp;Total&nbsp; &nbsp;Spent&nbsp; &nbsp; Left&nbsp;&nbsp;Speed<br> </li> <li>100&nbsp; &nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;100&nbsp; &nbsp;&nbsp;&nbsp;9&nbsp; &nbsp; 0&nbsp; &nbsp;&nbsp;&nbsp;0&nbsp; &nbsp;&nbsp;&nbsp;11&nbsp; &nbsp;&nbsp; &nbsp;0 --:--:-- --:--:-- --:--:--&nbsp; &nbsp; 11<br> </li> <li>key.sh: line 17: cd: $'../\r': No such file or directory<br> </li> <li>chmod: cannot access '.ssh'$'\r': No such file or directory<br> </li> <li>key.sh: line 19: cd: $'/etc/ssh/\r': No such file or directory<br> </li> <li>key.sh: line 20: $'\r': command not found<br> </li> <li>: No such file or directory<br> </li> <li>: No such file or directory<br> </li> <li>: No such file or directory<br> </li> <li>: No such file or directory<br> </li> <li>: No such file or directory<br> </li> <li>: No such file or directory<br> </li> <li>sshd: unrecognized service<br> </li> <li>Usage: /etc/init.d/ssh {start|stop|reload|force-reload|restart|try-restart|status}.<br> </li> <li>Invalid unit name "sshd<br> </li> <li>" escaped as "sshd\x0d" (maybe you should use systemd-escape?).<br> </li> <li>Failed to restart sshd\x0d.service: Unit sshd\x0d.service not found.<br> </li> <li>Invalid unit name "ssh<br> </li> <li>" escaped as "ssh\x0d" (maybe you should use systemd-escape?).<br> </li> <li>Failed to restart ssh\x0d.service: Unit ssh\x0d.service not found.<br> </li> <li>key.sh: line 31: cd: $'~\r': No such file or directory</li> </ol> </div><em onclick="copycode($('code_VFf'));">复制代码</em> </div><br> <br> 虽然自己的目标已经达到,单独复制出脚本内容来执行的,但是运行脚本为什么会报错还不知道原因,请大佬指点一下。感谢。
收藏(0)  分享
相关标签: 主机交流
注意:本文归作者所有,未经作者允许,不得转载
5个回复
  • 沙龙
    2022年11月15日 06:50
    忽略即可,这是因为yum运行在centos系统,而你debian是apt 脚本作者是把yum apt都写上导致,优化的话,写个系统类型的判断即可。 /** * 一只飞鸟以零点三马赫的速度划过天空,留下两道白色的翼痕。 * * Link https://greasyfork.org/zh-CN/scripts/396933-hostloc-zsbd */
    0 0
  • jzelynn
    2022年11月15日 06:50
    因为我们在windows编写或修改后的脚本是dos编码,而正常的在Linux系统中执行的是unix编码,在写shell脚本的时候,在windows的上开发后,上传到服务器,可能会有文件格式不正确,通过:set ff unix可以将dos格式设置为unix下的文件格式
    0 0
  • nieyuli
    2022年11月15日 06:50
    本帖最后由 nieyuli 于 2022-11-15 14:46 编辑 沙龙 发表于 2022-11-15 14:41 忽略即可,这是因为yum运行在centos系统,而你debian是apt 脚本作者是把yum apt都写上导致,优化的话,写 ... 大佬不是yum报错,apt命令的内容也报错,自己测试过多次了, 甚至于 cd ~ mkdir .ssh 这样的命令在脚本中运行的时候也报错,但是单独复制出来执行就没有问题。
    0 0
  • nieyuli
    2022年11月15日 06:50
    jzelynn 发表于 2022-11-15 14:43 因为我们在windows编写或修改后的脚本是dos编码,而正常的在Linux系统中执行的是unix编码,在写shell脚本的 ... 大佬,感谢指点,茅塞顿开,我去测试下去。
    0 0
  • 920
    2022年11月15日 07:12
    用 vscode 把 CRLF 转成 LF
    0 0