Linux 如何在bash中保存输入?

Linux 如何在bash中保存输入?,linux,bash,ubuntu,scripting,Linux,Bash,Ubuntu,Scripting,我正在写一个脚本,我想能够添加一个用户,并分配一个命令给它 所以基本上我想在脚本运行时编辑它。 在bash中可能吗 我已经写了基础知识,这是代码 #!/bin/bash #title: menu.sh #============================================================================== #Menu options #options[seq]="$varname" options[0]

我正在写一个脚本,我想能够添加一个用户,并分配一个命令给它

所以基本上我想在脚本运行时编辑它。 在bash中可能吗

我已经写了基础知识,这是代码

#!/bin/bash
#title:         menu.sh
#==============================================================================


#Menu options
#options[seq]="$varname"
options[0]="user1"
options[1]="user2"
options[2]="Guser3"
options[3]="user4"
options[4]="add user"


#Actions to take based on selection

function ACTIONS {
    if [[ ${choices[0]} ]]; then
        #Option 1 selected
        echo "user1 selected"
    fi
    if [[ ${choices[1]} ]]; then
        #Option 2 selected
        echo "user2 selected"
    fi
    if [[ ${choices[2]} ]]; then
        #Option 3 selected
        echo "user3 selected"
    fi
    if [[ ${choices[3]} ]]; then
        #Option 4 selected
        echo "user4 selected"
    fi
    if [[ ${choices[4]} ]]; then
        #Option 5 selected
        echo "new user added"

        read -p "Enter username name:  " sname
        read -p "Enter comand to save for this user:  " cname
        echo Adding  $sname to list with $cname comand
        
    fi
}

#Variables
ERROR=" "

#Clear screen for menu
clear

#Menu function
function MENU {
    echo "Menu Options"
    for NUM in ${!options[@]}; do
        echo "[""${choices[NUM]:- }""]" $(( NUM+1 ))") ${options[NUM]}"
    done
    echo "$ERROR"
}

#Menu loop
while MENU && read -e -p "Select the desired options using their number (again to uncheck, ENTER when done): " -n1 SELECTION && [[ -n "$SELECTION" ]]; do
    clear

    if [[ "$SELECTION" == *[[:digit:]]* && $SELECTION -ge 1 && $SELECTION -le ${#options[@]} ]]; then
        (( SELECTION-- ))
        if [[ "${choices[SELECTION]}" == "+" ]]; then
            choices[SELECTION]=""
        else
            choices[SELECTION]="+"
        fi
            ERROR=" "
    else
        ERROR="Invalid option: $SELECTION"
    fi
    set -e

done

ACTIONS
这就是代码

根据选择采取的行动
根据选择采取的措施如上所述,两个文件。一个包含
用户列表
,另一个包含脚本
菜单.sh

userlist:

user1
user2
Guser3
user4
从用户列表中获取输入,并初始化计数变量
i
。我将其包装在一个函数中:
get\u users

get_users() {
i=0
while read -r user
do
 options[$i]="$user"
 ((i++))
done < userlist

options[$i]="add user"
}
最后,确保将任何新用户附加到
用户列表
文件:

echo "$sname" >> userlist
请注意:

您需要添加一种更系统的方式,以响应您在
操作中的选择。由于您已经明确指定了选项应该是什么,例如,选项4将始终返回新用户

您可以通过以下方法来实现:

function ACTIONS {
    for (( c=0; c<=$(expr ${#options[@]} - 1); c++ ))
    do
    if [[ ${choices[$c]} ]]; then
        #Option 1 selected
        echo "user$(expr $c + 1) selected"
    fi
    done
    if [[ ${choices[$i]} ]]; then # $i is from the new get_users function - convenient way to get the last number from the array
        #Option 5 selected
        echo "new user added"

        read -p "Enter username name:  " sname
        read -p "Enter comand to save for this user:  " cname
        echo Adding  $sname to list with $cname comand
        echo "$sname" >> userlist
    fi
}
函数操作{
对于((c=0;c>userlist)
fi
}

如果我理解正确,您有一个数组
选项
,您想动态向该数组添加用户?这样下次执行
操作
函数时,您会得到一个列为选项的附加条目?是的,没错。我想动态添加用户。是的,这是可能的,但不需要编辑脚本。而是添加一个输入文件,然后从那里读取用户。例如,
i=0;而read-r user;do options[$i]=“$user”;((i++)done
如果这种方法适合您,我可以添加一个更完整的答案是的,如果可以,请添加更完整的答案。很抱歉重播时间太长,但我正在度假,我将非常感激添加更完整的答案
function ACTIONS {
    for (( c=0; c<=$(expr ${#options[@]} - 1); c++ ))
    do
    if [[ ${choices[$c]} ]]; then
        #Option 1 selected
        echo "user$(expr $c + 1) selected"
    fi
    done
    if [[ ${choices[$i]} ]]; then # $i is from the new get_users function - convenient way to get the last number from the array
        #Option 5 selected
        echo "new user added"

        read -p "Enter username name:  " sname
        read -p "Enter comand to save for this user:  " cname
        echo Adding  $sname to list with $cname comand
        echo "$sname" >> userlist
    fi
}