提问人:JJJ 提问时间:11/8/2023 最后编辑:BarmarJJJ 更新时间:11/8/2023 访问量:24
Linux 服务器设置本地 DNS - “大小写”/方法的问题
Linux server setup local DNS - Problem with 'Case' / methods
问:
在这里,您可以看到代码,其中我有 2 个方法 + 下面的代码 1 个“案例”,带有 3 个选项
add_user()
=> 也是我想添加 1 个用户的地方 /etc/hostsgenerate_RRs()
=> 我想添加 N 个随机用户的地方/etc/hosts
在我的“案例”中,我有 3 个选项可供选择。
- 选项 1: => 当您使用
This 时,应从文本文件中添加 1 个新用户,并从此列表中删除此名称。(此选项不起作用)-a
bash script.sh -a SomeName
names.list
- 选项 2:=>
这应该将 5 个随机用户添加到并从中删除这 5 个名称(此选项有效,它会添加 5 个用户-g
bash script.sh -g 5
/etc/hosts
names.list
- 选项 3: => => 这将在 /etc/hosts 文件中搜索匹配的名称并输出他的名字。(此选项有效,如果它在 /etc/hosts 文件中,它会给出匹配的名称
"$1"
bash script.sh Name
我一直在寻找很长时间,试图弄清楚为什么我不能在下面的代码中使用选项“-a”。
对我来说奇怪的部分是,该选项使用该函数并使用该方法 5 次。-g
generate_RRs
add_user
现在,当我想使用该选项并且只想使用 1 个用户时(因此只使用一次功能add_user,然后它将无法正常工作并且屏幕保持“冻结”,我必须关闭它,
我已经尝试使用并且找不到故障。对不起,如果这不是一个好的解释,我会尽力解释我的问题和我的意图。-a
ctrl+Cshellcheck
#! /bin/bash
function add_user {
local Name=${1}
local Domain=${2}
#Add new dns host-record with next avalaible IP-address
for i in {1..254}; do
if ! grep 10.22.33.$i /etc/hosts ; then
sudo echo -e "10.22.33.$i ${Name}.${Domain}" >> /etc/hosts
sudo echo -e "host-record=${Name}.${Domain},10.22.33.$i" >> /var/dns/database
break
fi
done
}
function generate_RRs {
local Number=${1}
local Domain=${2}
local woord=$(sort -R names.list | uniq | head -${Number})
#Add N random users(depend on how many numbers that they input in argument $1)
for file in ${woord}; do
add_user ${file} "${Domain}"
sudo sed -i "/${file}/d" names.list
done
}
name=$(cat /etc/hosts | grep "${naam}" | cut -d " " -f2)
case "${1}" in
-a)
#add 1 user
add_user "$2" "my.domain"
;;
#Add N random users
-g)
generate_RRs "$2" "my.domain"
;;
#output user that exist in list
"$1")
echo -e "today is ok ${name}"
echo -e "Name: Address\n "
;;
esac
答: 暂无答案
下一个:返回值不匹配循环
评论
/etc/hosts
不包含用户,它包含主机名和地址。grep
/etc/hosts
10.22.33.10
10.22.33.1
-w
-F
.
echo -e
naam