创建菜单 shell 脚本

w3nuxt5m  于 2023-01-05  发布在  Shell
关注(0)|答案(3)|浏览(128)

我刚开始学习shell脚本,我正在尝试找出一种方法来添加一个选项,使用户返回菜单。例如:

5) echo "Return to the menu"
   echo "Return back to the menu? ";;

听到的是手头的剧本:

echo "1. I'm number one"
echo "2. I'm number two"
echo "3. I'm number three"

echo "4. Exit from menu "

echo -n "Enter one the following numbers:"

while :
do

read choice

case $choice in

  1)  echo "You have selected the number one"
      echo "Selected number is one";;

  2)  echo "You have selected the number two"
      echo "Selected number is two";;

  3)  echo "You have selected the number three"
      echo "Selected number is three";;    

  4)  echo "Exiting after the information have been received by both devices" # Referring to the TCP/IP protocol. The information has to be established by both client and device to act like a server before data is sent. Ok I'm showing of here :)
      exit;

  
esac
  echo -n "Enter one of the four options"
done # Sorry if there are errors in the this code, I wrote it on the fly :)
yr9zkbsy

yr9zkbsy1#

也可以使用select创建菜单

#! /bin/bash

opts=( "I'm number one" "I'm number two" "I'm number three" "Exit from menu" )
PS3="Enter one the following numbers:"
select o in "${opts[@]}"
do
    case "$REPLY" in
        1)  echo "You have selected the number one: $o"
            echo "Selected number is one";;

        2)  echo "You have selected the number two: $o"
            echo "Selected number is two";;

        3)  echo "You have selected the number three: $o"
            echo "Selected number is three";;    

        4)  echo "Exiting after the information have been received by both devices" # Referring to the TCP/IP protocol. The information has to be established by both client and device to act like a server before data is sent. Ok I'm showing of here :)
            exit;;
    esac
done
bjg7j2ky

bjg7j2ky2#

要添加返回菜单功能,请尝试以下操作:添加一个变量accessMenu,将其设置为true,并将while循环条件更新为so,然后在循环中首先将accessMenu设置为false ......

accessMenu=true
while [[ $accessMenu -eq true ]]; do
    accessMenu=false
    ... (code)
done

最后,如果您希望能够返回菜单,请将变量设置为true,它将在while循环中传递条件以再次运行。

de90aj5v

de90aj5v3#

我最近做了一个函数来实现这个功能,您所需要做的就是break返回到上一个函数
https://stackoverflow.com/a/74793424/793088

selectVideoAction() {
    # Needed so listVideoActions can do a check to see if a 5 was the
    # last $REPLY before a 6
    [ "$REPLY" != 6 ] && lReply="$REPLY"

    [ $allSelected -eq 1 ] && PS3="Select an action($lAction): "\
                           || PS3="Select an action: "
    printf "\n"

    [ "$REPLY" = 2 ] && displayVideoInformation "$1" && lAction=$REPLY
    [ "$REPLY" = 3 ] && searchNameOpensubtitles "$1" && lAction=$REPLY
    [ "$REPLY" = 4 ] && searchHashOpensubtitles "$1" && lAction=$REPLY
}

listVideoActions() {
    local actions=("Go back to video selection")            # 1
    actions+=("Show video information")                     # 2 repeatable
    actions+=("Search name on opensubtitles")               # 3 repeatable
    actions+=("Search hash on opensubtitles")               # 4 repeatable
    [ $allSelected -eq 1 ] && actions+=("Next video")      `# 5` \
    && actions+=("Repeat last action on remaining files");  # 6
    actions+=("Quit")                                       # 5 or 7

    printf "${BBlue}$2${Color_Off}\n"
    [ $allSelected -eq 1 ] && PS3="Select an action($lAction): "\
                           || PS3="Select an action: "

    if [ $repeatLast -eq 0 ]; then
        select action in "${actions[@]}"; do
            selectVideoAction "$1"

            if   [ "$REPLY" = 1 ]; then allSelected=0; break;
            elif [ "$REPLY" = 5 ]  && [ $allSelected -eq 1 ]; then break
            elif [ "$REPLY" = 5 ]  && [ $allSelected -eq 0 ]; then exit 1
            elif [ "$REPLY" = 6 ]  && [ $allSelected -eq 1 ]; then
                # Check that a repeatable action has previously been selected
                if [ ! -z "$lAction" ]; then
                    # If the previous action $REPLY was to go to the next video
                    # then the repeatable action needs to be run before going
                    # to the next video
                    [ "$lReply" = 5 ] && REPLY=$lAction && selectVideoAction "$1"

                    repeatLast=1
                    break
                fi
                PS3="No last action. Select an action($lAction): "
            elif [ "$REPLY" = 7 ]  && [ $allSelected -eq 1 ]; then exit 1; fi
        done
    elif [ $repeatLast -eq 1 ]; then
        # Run the last repeatable action automatically for every remaining video
        sleep 2 | yes "$lAction" | select action in "${actions[@]}"; do
            selectVideoAction "$1"
            break
        done
    fi
}

listVideos() {
    local lReply=""       # The last action $REPLY
    local lAction=""      # The last repeatable action $REPLY 2, 3 or 4
    local repeatLast=0    # When set to 1 $lAction is automatically repeated
    local allSelected=0   # When set to 1 all videos have been selected
    local options=("${videos[@]}")  
          options+=("Select All")
    local numOptions=$((${#options[@]}))
    PS3="Select a video: "

    select file in "${options[@]##*/}"; do
        # Strip all spaces so arithmetic(( )) comparison doesn't fail
        REPLY=$(echo "$REPLY" | tr -d ' ')
        printf "\n"

        if [ "$REPLY" = "$numOptions" ]; then
            allSelected=1
            local video

            for video in "${videos[@]}"; do
                listVideoActions "$video" "${video##*/}"
                # The action $REPLY 1 was selected to return to the video list 
                [ $allSelected -eq 0 ] && break
            done
            break
        elif (( "$REPLY" > 0 && "$REPLY" < "$numOptions" )); then
            listVideoActions "${videos[$REPLY-1]}" "${videos[$REPLY-1]##*/}"
            break
        fi
    done
    listVideos
}

相关问题