【发布时间】:2014-03-26 21:06:23
【问题描述】:
我目前正在编写一个脚本,允许我通过用户输入添加组。我在脚本的一部分中,用户在其中键入组名,并将其与 /etc/group 进行比较,并让用户知道是否需要添加它。我已经针对一个我知道的事实不在我的系统上的组进行了测试,它只读取我循环中的第一条语句。谁能告诉我哪里出错了?
#!/bin/bash
echo "This script will allow you to enter Groups and Users needed for new builds"
echo
echo
echo
echo
# Setting Variables for Group Section
Group=`cat /etc/group |grep "$group"`
echo -n "Please enter the group name that you would like to search for..press [ENTER] when done: " # Request User input to obtain group name
read group
echo "Searching /etc/group to see if the group "$group" exists." # Checking to see if the group exists
if [ "$group" != "$Group" ]; then
echo "The group already exist. Nothing more to do buddy."
else
echo "We gotta add this one fella..carry on."
【问题讨论】:
-
除了不使用
getent吗? -
我想验证当前不存在的组是/etc/group。
-
现在,您在读取组名之前尝试使用 grep 获取组名。 当然那会失败。
-
感谢大家的帮助:)
标签: linux string bash if-statement scripting