簡體   English   中英

如何在bash中保存輸入?

[英]How to save input in bash?

我正在編寫一個腳本,我希望能夠添加一個用戶並為其分配一個命令。

所以基本上我想在它運行時編輯腳本。 在 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

那是代碼,

根據選擇采取的行動

根據選擇采取的行動

如前所述,兩個文件。 一個包含一個userlist ,一個包含你的腳本menu.sh

userlist:

user1
user2
Guser3
user4

從用戶列表中獲取輸入,並初始化您的計數變量i 我把它包裝在一個函數中: get_users

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

options[$i]="add user"
}

get_users添加到您的 MENU 函數。 這將在每次調用菜單時讀取用戶列表。

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

最后,確保你追加任何新用戶到你的userlist文件:

echo "$sname" >> userlist

請注意:

您將需要添加一種更系統的方法來響應您在ACTIONS函數中的選擇。 由於您已明確指定應該選擇什么,例如,選項 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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM