首页 > 解决方案 > mac上主机的shell脚本不起作用

问题描述

我一直在处理一些主机文件,以将本地网络 ips 添加到主机,使我的同事更容易在不记住 ips 的情况下访问我们的内部设备

Hosts.sh 文件

#!/bin/sh
if grep -Fxq "wiki" /etc/hosts
then
echo 'wiki er allerede tilføjet'
else
echo 'Tilføjer wiki til hosts'
sudo bash -c 'echo -e "192.168.2.13\twiki\n" >> /etc/hosts'
echo 'wiki er blevet tilføjet'
fi
if grep -Fxq "taxatilbus-docs" /etc/hosts
then
echo 'taxatilbus-docs er allerede tilføjet'
else
echo 'Tilføjer taxatilbus-docs til hosts'
sudo bash -c 'echo -e "192.168.2.12\ttaxatilbus-docs\n" >> /etc/hosts'
echo 'wiki er blevet tilføjet'
fi
if grep -Fxq "docucolor" /etc/hosts
then
echo 'docucolor er allerede tilføjet'
else
echo 'Tilføjer docucolor til hosts'
sudo bash -c 'echo -e "192.168.1.7\tdocucolor\n" >> /etc/hosts'
echo 'wiki er blevet tilføjet'
fi
if grep -Fxq "epson" /etc/hosts
then
echo 'epson er allerede tilføjet'
else
echo 'Tilføjer epson til hosts'
sudo bash -c 'echo -e "192.168.1.8\tepson\n" >> /etc/hosts'
echo 'wiki er blevet tilføjet'
fi
if grep -Fxq "brother" /etc/hosts
then
echo 'brother er allerede tilføjet'
else
echo 'Tilføjer brother til hosts'
sudo bash -c 'echo -e "192.168.1.9\tbrother\n" >> /etc/hosts'
echo 'wiki er blevet tilføjet'
fi
if grep -Fxq "mathias-laptop" /etc/hosts
then
echo 'mathias-laptop er allerede tilføjet'
else
echo 'Tilføjer mathias-laptop til hosts'
sudo bash -c 'echo -e "192.168.1.18\tmathias-laptop\n" >> /etc/hosts'
echo 'wiki er blevet tilføjet'
fi
if grep -Fxq "mathias-desktop" /etc/hosts
then
echo 'mathias-desktop er allerede tilføjet'
else
echo 'Tilføjer mathias-desktop til hosts'
sudo bash -c 'echo -e "192.168.1.19\tmathias-desktop\n" >> /etc/hosts'
echo 'wiki er blevet tilføjet'
fi
if grep -Fxq "unifi" /etc/hosts
then
echo 'unifi er allerede tilføjet'
else
echo 'Tilføjer unifi til hosts'
sudo bash -c 'echo -e "192.168.1.11\tunifi\n" >> /etc/hosts'
echo 'wiki er blevet tilføjet'
fi

我通知他们把 hosts.sh 文件放在桌面上,然后去终端写

cd Desktop
./hosts.sh

但是它只是说找不到该命令,并将整个脚本粘贴到终端中会将 wiki 添加到主机文件中,而不是其余的

标签: macosshellterminalsh

解决方案


推荐阅读