Bash:如何检查变量是否包含另一个变量的字符串?

时间:2014-11-30 14:10:09

标签: bash shell if-statement

为了简化在公司的NIS服务器中创建新用户的过程,我编写了以下脚本:

#!/bin/bash
# This script will simplicate NIS user management.
# You will not be able to change password or delete users peeradmin and root through this script.
# Written by Itai Ganot 2014.

# Variables
USER=$1
GREP="/bin/grep"
PASSWDFILE="/etc/passwd"
YPPASSWD="/usr/bin/yppasswd"
USERDEL="/usr/sbin/userdel"
USERADD="/usr/sbin/useradd"
PASSWD="/usr/bin/passwd"
YPCAT="/usr/bin/ypcat passwd.byname"


# Functions
function usage {
echo -e "Usage: $0 <username to manage>"
}

function updatenis {
echo -e "\e[36m #===#  Uptdating NIS database... \e[0m"
cd /var/yp && make
}

# Script
if [ -z "$USER" ]; then
usage
exit 1
fi
if [ "$(id -u)" != "0" ]; then
echo -e "Run as root!"
exit 1
fi
"$GREP" -q "$USER" "$PASSWDFILE"
if [ "$?" = "0" ]; then
    echo -e "\e[36m #===#  User already exists \e[0m"
    echo -e "\e[36m #===#  How would you like to continue? \e[0m"
    USERID=$(id -u $USER)
    select CHOICE in 'Change user password' 'Remove user' 'View user' 'Exit'; do
        case $CHOICE in
        "Change user password")
        if [[ "$USER" = "peeradmin" || "$USER" = "root" ]]; then # Defense against changing root or peeradmin password
                        echo -e "\e[36m #===#  User $USER should never be edited! \e[0m"
                        exit 1
                        fi

        echo -e "\e[36m #===#  Provide root password for NIS server... \e[0m"
        "$YPPASSWD" "$USER"
        break
        ;;
        "Remove user")
            if [[ "$USER" = "peeradmin" || "$USER" = "root" ]]; then # Defense against deletion of user root or peeradmin.
            echo -e "\e[36m #===#  User $USER should never be edited! \e[0m"
            exit 1
            fi
        read -r -p "Remove home directory and mail? [y/n] " ANSWER1
        if [[ "$ANSWER1" = [Yy] ]]; then
        "$USERDEL" -r "$USER"
        updatenis
        echo -e "\e[36m #===#  User $USER has been deleted along with the user's home folder and mail \e[0m"
        break
        else
        "$USERDEL" "$USER"
        echo -e "\e[36m #===#  User $USER has been deleted \e[0m"
        updatenis
        break
        fi
        ;;
        "View user")
        echo -e "\e[36m #===# Displaying user $USER \e[0m"
        $YPCAT | $GREP "$USER"
        break       
        ;;
        "Exit")
        echo -e "\e[36m #===#  Exiting, No changes done.  \e[0m"
        exit 0
        ;;
        esac
    done
else
    read -r -p "User doesn't exist, would you like to add it? [y/n] " ANSWER2
    if [[ "$ANSWER2" = [Yy] ]]; then
        echo -e "\e[36m #===#  Collecting required information... \e[0m"
        sleep 2
        LASTUID=$(tail -n 1 $PASSWDFILE | awk -F: '{print $3}')
        NEXTUID=$(( LASTUID + 1 ))
        $USERADD -g users $USER -u $NEXTUID
        echo -e "\e[36m #===#  Set password for the new user \e[0m"
        $PASSWD $USER
        updatenis
        read -r -p "Would you like to test the creation of the user? [y/n] " ANSWER3
            if [[ "$ANSWER3" = [Yy] ]]; then
            $YPCAT | $GREP "$USER"
                if [ "$?" = "0" ]; then
                echo -e "\e[36m #===#  User $USER created successfully!  \e[0m"
                fi
            fi
    elif [[ "$ANSWER2" = [Nn] ]]; then
        echo -e "\e[36m #===#  Exiting, no changes done. \e[0m"
        exit 0
    fi
fi

我希望将脚本公开,以便我能够与某些社区分享,但是我很难完成某些特定的事情。 我想定义一个名为PROTECTEDUSERS的新变量,并为其分配一个或多个用户名。 例如:

PROTECTEDUSERS="root peeradmin"

然后,在相关的一行:

if [[ "$USER" = "peeradmin" || "$USER" = "root" ]];

我希望该行包含新创建的变量。 我试过这个:

if [[ "*$USER*" =~ "$PROTECTEDUSERS" ]]; then...

但它不起作用,甚至是可能的事情吗?我相信它只是不知道该怎么做,请协助。

提前致谢

2 个答案:

答案 0 :(得分:3)

你正在反向使用glob,在bash中使用==这个条件:

if [[ "$PROTECTEDUSERS" == *"$USER"* ]]; then...
只有RHS才支持

glob pattern

答案 1 :(得分:1)

if [[ "$PROTECTEDUSERS" =~ $USER ]]; then

更新以匹配“admin”之类的子网:

if [[ "$PROTECTEDUSERS" =~ (^| )$USER($| ) ]]; then