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

28次阅读

共计 3164 个字符,预计需要花费 8 分钟才能阅读完成。

今天拿一台机器 dd 了一个 debain11,想要用一键脚本设置秘钥登录,因为国内鸡连不上 GitHub,几把脚本内容复制下来粘贴到记事本当中上传到 VPS 上,并修改后缀名为 *.sh 然后运行该脚本总是报错,但是吧脚本内容单独拿出来执行却又没有问题,请问大佬指教一下是什么原因?用的是这个一键脚本:https://github.com/KiritoMiao/SSHKEY_Installer/ 以下是脚本内容:#/bin/shapt-get update -yapt-get install curl -yyum clean allyum make cacheyum install curl -yecho \’============================      SSH Key Installer         V1.0 Alpha        Author:Kirito============================\’cd ~mkdir .sshcd .sshcurl https://github.com/$1.keys > authorized_keyschmod 700 authorized_keyscd ../chmod 600 .sshcd /etc/ssh/sed -i "/PasswordAuthentication no/c PasswordAuthentication no" sshd_configsed -i "/RSAAuthentication no/c RSAAuthentication yes" sshd_configsed -i "/PubkeyAuthentication no/c PubkeyAuthentication yes" sshd_configsed -i "/PasswordAuthentication yes/c PasswordAuthentication no" sshd_configsed -i "/RSAAuthentication yes/c RSAAuthentication yes" sshd_configsed -i "/PubkeyAuthentication yes/c PubkeyAuthentication yes" sshd_configservice sshd restartservice ssh restartsystemctl restart sshdsystemctl restart sshcd ~rm -rf key.sh 复制代码 ============================ 运行报错内容:root@hostname:~# bash key.shE: Command line option \’   \’ [from -y   ] is not understood in combination with the other options.E: Command line option \’   \’ [from -y   ] is not understood in combination with the other options.key.sh: line 4: yum: command not foundkey.sh: line 5: yum: command not foundkey.sh: line 6: yum: command not found============================      SSH Key Installer         V1.0 Alpha        Author:Kirito============================key.sh: line 12: cd: $\’~\\r\’: No such file or directorymkdir: cannot create directory‘.ssh\\r’: File exists  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100     9  100     9    0     0     11      0 –:–:– –:–:– –:–:–    11key.sh: line 17: cd: $\’../\\r\’: No such file or directorychmod: cannot access \’.ssh\’$\’\\r\’: No such file or directorykey.sh: line 19: cd: $\’/etc/ssh/\\r\’: No such file or directorykey.sh: line 20: $\’\\r\’: command not found: No such file or directory: No such file or directory: No such file or directory: No such file or directory: No such file or directory: No such file or directorysshd: unrecognized serviceUsage: /etc/init.d/ssh {start|stop|reload|force-reload|restart|try-restart|status}.Invalid unit name "sshd" escaped as "sshd\\x0d" (maybe you should use systemd-escape?).Failed to restart sshd\\x0d.service: Unit sshd\\x0d.service not found.Invalid unit name "ssh" escaped as "ssh\\x0d" (maybe you should use systemd-escape?).Failed to restart ssh\\x0d.service: Unit ssh\\x0d.service not found.key.sh: line 31: cd: $\’~\\r\’: No such file or directory 复制代码虽然自己的目标已经达到,单独复制出脚本内容来执行的,但是运行脚本为什么会报错还不知道原因,请大佬指点一下。感谢。沙龙 2022-11-15 14:41 2 忽略即可,这是因为 yum 运行在 CentOS 系统,而你 Debian 是 apt 脚本作者是把 yum apt 都写上导致,优化的话,写个系统类型的判断即可。jzelynn 2022-11-15 14:43 3 因为我们在 Windows 编写或修改后的脚本是 dos 编码,而正常的在 Linux 系统中执行的是 Unix 编码,在写 Shell 脚本的时候,在 Windows 的上开发后,上传到服务器,可能会有文件格式不正确,通过:set ff unix 可以将 dos 格式设置为 Unix 下的文件格式 nieyuli 2022-11-15 14:44 4 内容最后由 nieyuli 于 2022-11-15 14:46 编辑 沙龙 发表于 2022-11-15 14:41 忽略即可,这是因为 yum 运行在 CentOS 系统,而你 Debian 是 apt 脚本作者是把 yum apt 都写上导致,优化的话,写 … 大佬不是 yum 报错,apt 命令的内容也报错,自己测试过多次了,甚至于 cd ~mkdir .ssh 这样的命令在脚本中运行的时候也报错,但是单独复制出来执行就没有问题。nieyuli 2022-11-15 14:48 5jzelynn 发表于 2022-11-15 14:43 因为我们在 Windows 编写或修改后的脚本是 dos 编码,而正常的在 Linux 系统中执行的是 Unix 编码,在写 Shell 脚本的 … 大佬,感谢指点,茅塞顿开,我去测试下去。920 2022-11-15 15:10 6 用 vscode 把 CRLF 转成 LF

正文完
 0