所以,我是一个中级 Linux 用户,我刚刚发现 sed 并想使用它在我通常的安装/配置脚本中自动执行一些配置。
我浏览了所有论坛,找到了与我类似的问题,但是我没有看到很多使用与我相同的格式的论坛,我也不知道为什么。我通过查看许多其他 sed 脚本并使用与我尝试执行的操作兼容的脚本找到了下面的脚本。然后我将其放入脚本中,它抛出了错误。
我无法在此 sed 脚本中找到问题:
sudo sed -i 's/#AllowTcpForwarding yes/AllowTcpForwarding no/g' "/etc/ssh/sshd_config"
sudo sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g' "/etc/ssh/sshd_config"
sudo sed -i 's/X11Forwarding yes/#X11Forwarding yes/g' "/etc/ssh/sshd_config"
sudo sed -i 's/#StrictModes yes/StrictModes yes/g' "/etc/ssh/sshd_config"
sudo sed -i 's/#IgnoreRhosts yes/IgnoreRhosts yes/g' "/etc/ssh/sshd_config"
sudo sed -i 's/#HostbasedAuthentication no/HostbasedAuthentication no/g' "/etc/ssh/sshd_config"
sudo sed -i 's/#RhostsRSAAuthentication no/RhostsRSAAuthentication no/g' "/etc/ssh/sshd_config"
我也写了如下命令:
sudo sed -i 's/#AllowTcpForwarding yes/AllowTcpForwarding no/g;s/#PermitRootLogin yes/PermitRootLogin no/g;s/X11Forwarding yes/#X11Forwarding yes/g;s/#StrictModes yes/StrictModes yes/g;s/#IgnoreRhosts yes/IgnoreRhosts yes/g;s/#HostbasedAuthentication no/HostbasedAuthentication no/g;s/#RhostsRSAAuthentication no/RhostsRSAAuthentication no/g' "/etc/ssh/sshd_config"
我犯了同样的错误:
: No such file or directoryhd_config
在脚本中运行其他参数时,所有命令均能顺利完成。我可以单独成功运行这两个命令。
完整脚本如下:
#!/bin/bash
# Title: Ubuntu Setup
# Author: Matthew Williams
# Date: 10/31/2016
echo "***Ubuntu Setup Script Log***" `date +%d%m%Y_%H:%M.%S` | tee UbuntuSetupLog.txt
sudo apt-get install -y libpcsclite1 pcscd pcsc-tools libssl-dev libpam0g-dev pkg-config libpcsclite-dev gdebi opensc unity-tweak-tool gnome-do openssh-server openssh-client byobu | tee -a UbuntuSetupLog.txt
echo "***Files Installed***" `date +%d%m%Y_%H:%M.%S` | tee -a UbuntuSetupLog.txt
#
# Configure Networking / SSH
#
echo "***Configuring Networking/SSH***" | tee -a UbuntuSetupLog.txt
#
sudo sed -i 's/#AllowTcpForwarding yes/AllowTcpForwarding no/g;s/#PermitRootLogin yes/PermitRootLogin no/g;s/X11Forwarding yes/#X11Forwarding yes/g;s/#StrictModes yes/StrictModes yes/g;s/#IgnoreRhosts yes/IgnoreRhosts yes/g;s/#HostbasedAuthentication no/HostbasedAuthentication no/g;s/#RhostsRSAAuthentication no/RhostsRSAAuthentication no/g' "/etc/ssh/sshd_config"
sudo service sshd restart 2>&1 | tee -a UbuntuSetupLog.txt
#
echo "***Configuring Networking/SSH Complete***" `date +%d%m%Y_%H:%M.%S` | tee -a UbuntuSetupLog.txt
#
echo "***Script Complete***"
完整回报为:
***Ubuntu Setup Script Log*** 02112016_15:28.38
Reading package lists...
Building dependency tree...
Reading state information...
byobu is already the newest version (5.106-0ubuntu1).
libpam0g-dev is already the newest version (1.1.8-3.2ubuntu2).
libpcsclite-dev is already the newest version (1.8.14-1ubuntu1).
libpcsclite1 is already the newest version (1.8.14-1ubuntu1).
pkg-config is already the newest version (0.29.1-0ubuntu1).
gdebi is already the newest version (0.9.5.7ubuntu1).
gnome-do is already the newest version (0.95.3-5).
opensc is already the newest version (0.15.0-1ubuntu1).
pcsc-tools is already the newest version (1.4.25-1).
pcscd is already the newest version (1.8.14-1ubuntu1).
unity-tweak-tool is already the newest version (0.0.7ubuntu2).
libssl-dev is already the newest version (1.0.2g-1ubuntu4.5).
openssh-client is already the newest version (1:7.2p2-4ubuntu2.1).
openssh-server is already the newest version (1:7.2p2-4ubuntu2.1).
0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
***Files Installed*** 02112016_15:28.38
***Configuring Networking/SSH***
: No such file or directoryhd_config
***Configuring Networking/SSH Complete*** 02112016_15:28.38
***Script Complete***
我需要这个脚本来自动化其他一些机器。我知道有更好的方法,每个人都有自己喜欢的方法。我唯一的问题是我在 sed 中做错了什么?我不确定我需要做什么来更改这个脚本,我已经在网上搜索了大约一个星期却毫无结果。
我相信错误在于我调用文件的方式,但不确定如何做。
答案1
您的脚本使用 CR+LF (Windows) 行尾保存。shell 无法理解这些行尾 - 它仅使用 LF 作为行尾标记,并且 CR 成为命令。 因此:
- 您的日志正在写入文件
UbuntuSetupLog.txt<CR>
。(遗憾的是,文件名中允许使用 CR 和 LF...) - 你的sed命令正在尝试编辑文件
/etc/ssh/sshd_config<CR>
,并且 CR 字节在打印时会破坏错误消息(充当实际的回车符)。
使用dos2unix
或frodos
转换脚本。或者如果您愿意:
sed -i 's/\r$//' myscript.sh