如果then语句失败,则使用String Bash脚本

时间:2021-02-24 22:24:31

I am currently writing a script that will allow me to add groups via user input. I am on the portion of my script where the user types the group name in and it compares it against /etc/group and lets the user know if it needs to be added or not. I have tested this against a group that I know for a fact is not on my system and it only reads the first statement in my loop. Could someone tell me where I am going wrong?

我目前正在编写一个脚本,允许我通过用户输入添加组。我在我的脚本中用户键入组名称的部分,并将其与/ 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."

2 个解决方案

#1


1  

If you're on Linux, and thus have getent available:

如果您使用的是Linux,那么可以使用getent:

printf "Group to search for: "
read -r group
if getent group "$group" >/dev/null 2>&1; then
  echo "$group exists"
else
  echo "$group does not exist"
fi

Using getent uses the standard C library for directory lookups. Thus, it's good for not only /etc/passwd, /etc/group, etc., but also directory services such as Active Directory, LDAP, NIS, YP and the like.

使用getent使用标准C库进行目录查找。因此,它不仅适用于/ etc / passwd,/ etc / group等,还适用于Active Directory,LDAP,NIS,YP等目录服务。

#2


0  

Here's what you do:

这是你做的:

  1. Search for a group name
  2. 搜索组名称
  3. Input the group name to search for
  4. 输入要搜索的组名称

Sadly, you can't search for the group name before you input it, as this would violate causality and the laws of spacetime as we know them. Try searching after you know what you search for instead:

可悲的是,在输入之前你无法搜索群组名称,因为这会违反我们所知道的因果关系和时空规律。相反,在您知道搜索内容后尝试搜索:

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

if cat /etc/group | grep -q "^$group:"
then 
    echo "The group already exist. Nothing more to do buddy."
fi

#1


1  

If you're on Linux, and thus have getent available:

如果您使用的是Linux,那么可以使用getent:

printf "Group to search for: "
read -r group
if getent group "$group" >/dev/null 2>&1; then
  echo "$group exists"
else
  echo "$group does not exist"
fi

Using getent uses the standard C library for directory lookups. Thus, it's good for not only /etc/passwd, /etc/group, etc., but also directory services such as Active Directory, LDAP, NIS, YP and the like.

使用getent使用标准C库进行目录查找。因此,它不仅适用于/ etc / passwd,/ etc / group等,还适用于Active Directory,LDAP,NIS,YP等目录服务。

#2


0  

Here's what you do:

这是你做的:

  1. Search for a group name
  2. 搜索组名称
  3. Input the group name to search for
  4. 输入要搜索的组名称

Sadly, you can't search for the group name before you input it, as this would violate causality and the laws of spacetime as we know them. Try searching after you know what you search for instead:

可悲的是,在输入之前你无法搜索群组名称,因为这会违反我们所知道的因果关系和时空规律。相反,在您知道搜索内容后尝试搜索:

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

if cat /etc/group | grep -q "^$group:"
then 
    echo "The group already exist. Nothing more to do buddy."
fi