第一篇文章。提前感谢您的所有帮助。
这是我试图编写的基本脚本,它将改变CentOS系统的DNS设置(/etc/resolv.conf)。该脚本应该执行以下操作:
此脚本正确获取DNS1,询问是否有其他DNS。如果不是,则正确写入文件。如果不是,它会继续询问DNS2。
这是它不再有效的地方。在读取DNS2后,它不会继续使用脚本来提示是否有其他DNS。它冻结了。如果你能在这个过程中看到错误,请告诉我。
# #!/bin/bash
# FUNCTION TO GRAB THE FIRST DNS
oneDNS()
{
read -p "Enter DNS: (looks like 123.231.132.1) " firstDNS
}
# FUNCTION TO GRAB THE SECOND DNS
twoDNS()
{
read -p "Enter second DNS: (looks like 123.231.132.2) " secondDNS
}
# FUNCTION TO GRAB THE THIRD DNS
threeDNS()
{
read -p "Enter third DNS: (looks like 123.321.132.3) " thirdDNS
}
# FUNCTION WHICH WRITE /etc/resolv.conf IF ONLY
writeOneResolveFile()
{
cat << EOF > $1
nameserver $firstDNS
EOF
#don't use any space before of after 'EOF' in the previous line
echo ""
echo "Your informatons was saved in '$1' file."
echo ""
exit 0
}
writeTwoResolveFile()
{
cat << EOF > $1
nameserver $firstDNS
nameserver $secondDNS
EOF
#don't use any space before of after 'EOF' in the previous line
echo ""
echo "Your informatons was saved in '$1' file."
echo ""
exit 0
}
writeThreeResolveFile()
{
cat << EOF > $1
nameserver $firstDNS
nameserver $secondDNS
nameserver $thirdDNS
EOF
#don't use any space before of after 'EOF' in the previous line
echo ""
echo "Your information was saved in '$1' file."
echo ""
exit 0
}
file="/etc/resolv.conf"
if [ ! -f $file ]; then
echo ""
echo "The file '$file' doesn't exist!"
echo ""
exit 1
fi
clear
echo "Let's set up the DNS your server"
echo ""
oneDNS
echo ""
echo "So DNS settings are:"
echo "DNS Address is: $firstDNS"
echo "Do you have another DNS? [y/n]: "
while true; do
read yn
case $yn in
[Yy]* ) twoDNS;;
[Nn]* ) writeOneResolveFile $file;;
* ) echo "Please enter y or n!";;
esac
done
echo ""
echo "So DNS settings are:"
echo "First DNS Address is: $firstDNS"
echo "Second DNS Address is: $secondDNS"
echo "Do you have another DNS? [y/n]: "
while true; do
read yn
case $yn in
[Yy]* ) threeDNS;;
[Nn]* ) writeTwoResolveFile $file;;
* ) echo "Please enter y or n!";;
esac
done
echo ""
echo "So DNS settings are:"
echo "First DNS Address is: $firstDNS"
echo "Second DNS Address is: $secondDNS"
echo "Third DNS Address is: $thirdDNS"
echo "Is this information correct? [y/n]: "
while true; do
read yn
case $yn in
[Yy]* ) writeThreeResolveFile $file;;
[Nn]* ) ;;
* ) echo "Please enter y or n!";;
esac
done
#
再次,非常感谢任何帮助。
卡姆兰
答案 0 :(得分:0)
仔细检查你的引号,你在第93行末尾错过了一个。
答案 1 :(得分:0)
问题是,在询问“oneDNS”之后,你进入一个无限循环,你要求“twoDNS”。从这个循环你永远逃脱。
亲自尝试一下:
对于调试模式,您可能希望将第一行更改为#!/bin/bash -x
。
echo ""
oneDNS
echo ""
echo "So DNS settings are:"
echo "DNS Address is: $firstDNS"
echo "Do you have another DNS? [y/n]: "
while true; do #<-- how are you planning to escape from this ??
read yn
case $yn in
[Yy]* ) twoDNS;; #<-- this will be asked over and over again
[Nn]* ) writeOneResolveFile $file;;
* ) echo "Please enter y or n!";;
esac
done
答案 2 :(得分:0)
你的循环有while true
,没有什么可以突破无限循环。您需要指定退出条件。事情的方式,它只是再次要求yn
提示(第二次和随后的时间没有任何提示)。
更好的设计可能类似于
servers=
another=true
while $another; do
read -p "DNS server address to add?" server
servers="$servers $server"
case $dns in
'') ;;
*) while true; do
read -p "Add another [yn]?" yn
case $yn in [Yy]*) break;; [Nn]*) another=false; break;; esac
done ;;
esac
done
for s in $servers; do
echo nameserver $s
done >/etc/resolv.conf
(未经测试,但希望至少可以提出一些改变。)