收到错误“script.sh:第 150 行:私有:找不到命令”

收到错误“script.sh:第 150 行:私有:找不到命令”

我有这个脚本:

#!/bin/bash


BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null 2>&1 && pwd )"
USER_DEF=$(whoami)

function private {
    read -p "Enter private chat name: " name
    if [[ $name == '' ]] ; then
        :
    else
        if [ -d "$BASE_DIR/chats/private/$name/" ] ; then
            pass=$(cat "$BASE_DIR/chats/private/$name/pass")
            read -s -p "Enter private chat password: " password
            if [[ $password == $pass ]] ; then
                chat "private" "$name"
                count=$(find $BASE_DIR/chats/private/$name/ -type p)
                if [[ "$count" == '' ]] ; then
                    rm -rf "$BASE_DIR/chats/private/$name"
                fi
                echo You exited private chat: $name
            else
                echo Wrong password
            fi
        fi
    fi
    unset $options
    if [[ -e ./chats/public ]] ; then
        options=($(find $BASE_DIR/chats/public -mindepth 1 -type d -printf '%f\n'))
    fi
    options+=("Enter private room")
    options+=("Create public room")
    options+=("Create private room")
    options+=("Quit")
}

clear
read -r -p "Enter your name [$USER_DEF]: " UD
if [[ $UD = "" ]] ; then
    USERNAME=$USER_DEF
else
    USERNAME=$UD
fi
clear
echo Welcome back $USERNAME
echo We have this chat in public:

PS3='Please enter your choice: '
if [[ -e ./chats/public ]] ; then
    options=($(find $BASE_DIR/chats/public -mindepth 1 -type d -printf '%f\n'))
fi
options+=("Enter private room")
options+=("Create public room")
options+=("Create private room")
options+=("Quit")
while true 
do
    int_count=1
    for el in "${options[@]}"; do
        echo "$int_count) $el"
        int_count=$(expr $int_count + 1)
    done
    read -p "$PS3" optional
    opt=${options[$(expr $optional - 1)]}
    case $opt in
        "Enter private room")
            private # this is line 150
            ;;
        "Create public room")
            create_public
            ;;
        "Create private room")
            create_private
            ;;
        "Quit")
            echo "Bye, $USERNAME"
            exit 0
            ;;
        [a-zA-Z][a-zA-Z0-9]*) 
            public $opt
            ;;
    esac
done

问题是:如果在菜单中按1,我Enter再次1收到此错误:
script.sh: line 150: private: command not found
这意味着什么?我不能使用它超过 1 次?

答案1

问题是线

unset $options

$options被求值时,除其他外,它包含单词,private因此 shell 取消定义您的函数。

正确的语法是

unset options

相关内容