全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

IP归属甄别会员请立即修改密码
查看: 617|回复: 6
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
1#
发表于 2022-11-15 14:39:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
今天拿一台机器dd了一个debain11,想要用一键脚本设置秘钥登录,因为国内鸡连不上github,几把脚本内容复制下来粘贴到记事本当中上传到小鸡上,并修改后缀名为*.sh
然后运行该脚本总是报错,但是吧脚本内容单独拿出来执行却又没有问题,请问大佬指教一下是什么原因?用的是这个一键脚本:https://github.com/KiritoMiao/SSHKEY_Installer/

以下是脚本内容:
  1. #/bin/sh
  2. apt-get update -y
  3. apt-get install curl -y
  4. yum clean all
  5. yum make cache
  6. yum install curl -y
  7. echo '============================
  8.       SSH Key Installer
  9.          V1.0 Alpha
  10.         Author:Kirito
  11. ============================'
  12. cd ~
  13. mkdir .ssh
  14. cd .ssh
  15. curl https://github.com/$1.keys > authorized_keys
  16. chmod 700 authorized_keys
  17. cd ../
  18. chmod 600 .ssh
  19. cd /etc/ssh/

  20. sed -i "/PasswordAuthentication no/c PasswordAuthentication no" sshd_config
  21. sed -i "/RSAAuthentication no/c RSAAuthentication yes" sshd_config
  22. sed -i "/PubkeyAuthentication no/c PubkeyAuthentication yes" sshd_config
  23. sed -i "/PasswordAuthentication yes/c PasswordAuthentication no" sshd_config
  24. sed -i "/RSAAuthentication yes/c RSAAuthentication yes" sshd_config
  25. sed -i "/PubkeyAuthentication yes/c PubkeyAuthentication yes" sshd_config
  26. service sshd restart
  27. service ssh restart
  28. systemctl restart sshd
  29. systemctl restart ssh
  30. cd ~
  31. rm -rf key.sh
复制代码


============================


运行报错内容:
  1. root@debian:~# bash key.sh
  2. E: Command line option '
  3.    ' [from -y
  4.    ] is not understood in combination with the other options.
  5. E: Command line option '
  6.    ' [from -y
  7.    ] is not understood in combination with the other options.
  8. key.sh: line 4: yum: command not found
  9. key.sh: line 5: yum: command not found
  10. key.sh: line 6: yum: command not found
  11. ============================
  12.       SSH Key Installer
  13.          V1.0 Alpha
  14.         Author:Kirito
  15. ============================
  16. key.sh: line 12: cd: $'~\r': No such file or directory
  17. mkdir: cannot create directory ‘.ssh\r’: File exists
  18.   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
  19.                                  Dload  Upload   Total   Spent    Left  Speed
  20. 100     9  100     9    0     0     11      0 --:--:-- --:--:-- --:--:--    11
  21. key.sh: line 17: cd: $'../\r': No such file or directory
  22. chmod: cannot access '.ssh'$'\r': No such file or directory
  23. key.sh: line 19: cd: $'/etc/ssh/\r': No such file or directory
  24. key.sh: line 20: $'\r': command not found
  25. : No such file or directory
  26. : No such file or directory
  27. : No such file or directory
  28. : No such file or directory
  29. : No such file or directory
  30. : No such file or directory
  31. sshd: unrecognized service
  32. Usage: /etc/init.d/ssh {start|stop|reload|force-reload|restart|try-restart|status}.
  33. Invalid unit name "sshd
  34. " escaped as "sshd\x0d" (maybe you should use systemd-escape?).
  35. Failed to restart sshd\x0d.service: Unit sshd\x0d.service not found.
  36. Invalid unit name "ssh
  37. " escaped as "ssh\x0d" (maybe you should use systemd-escape?).
  38. Failed to restart ssh\x0d.service: Unit ssh\x0d.service not found.
  39. key.sh: line 31: cd: $'~\r': No such file or directory
复制代码


虽然自己的目标已经达到,单独复制出脚本内容来执行的,但是运行脚本为什么会报错还不知道原因,请大佬指点一下。感谢。
推荐
发表于 2022-11-15 14:43:48 来自手机 | 只看该作者
因为我们在windows编写或修改后的脚本是dos编码,而正常的在Linux系统中执行的是unix编码,在写shell脚本的时候,在windows的上开发后,上传到服务器,可能会有文件格式不正确,通过:set ff unix可以将dos格式设置为unix下的文件格式

点评

感谢大佬指点,确实是这个问题,已解决!  发表于 2022-11-15 15:03
2#
发表于 2022-11-15 14:41:35 | 只看该作者
忽略即可,这是因为yum运行在centos系统,而你debian是apt

脚本作者是把yum apt都写上导致,优化的话,写个系统类型的判断即可。



/**
* 一只飞鸟以零点三马赫的速度划过天空,留下两道白色的翼痕。
*
* Link https://greasyfork.org/zh-CN/scripts/396933-hostloc-zsbd
*/
4#
 楼主| 发表于 2022-11-15 14:44:12 | 只看该作者
本帖最后由 nieyuli 于 2022-11-15 14:46 编辑
沙龙 发表于 2022-11-15 14:41
忽略即可,这是因为yum运行在centos系统,而你debian是apt

脚本作者是把yum apt都写上导致,优化的话,写 ...


大佬不是yum报错,apt命令的内容也报错,自己测试过多次了,
甚至于
cd ~
mkdir .ssh
这样的命令在脚本中运行的时候也报错,但是单独复制出来执行就没有问题。
5#
 楼主| 发表于 2022-11-15 14:48:15 | 只看该作者
jzelynn 发表于 2022-11-15 14:43
因为我们在windows编写或修改后的脚本是dos编码,而正常的在Linux系统中执行的是unix编码,在写shell脚本的 ...

大佬,感谢指点,茅塞顿开,我去测试下去。
6#
发表于 2022-11-15 15:10:13 | 只看该作者
用 vscode 把 CRLF 转成 LF
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2025-9-20 20:07 , Processed in 0.061945 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表