如何 select 菜单中的多个文件并使用 ksh/bash 将其传递给子菜单

How to select a number of files in a menu and pass that to a submenu using ksh/bash

列出菜单中的文件数。

主要目标是 select 菜单中文件列表中的一个选项,然后在子菜单中使用该 selected 选项。对我有用的脚本是主菜单选项不变的地方。当 options 变量是 *.dbf 时它不起作用。相反,我们得到无效选项。

以下代码对我不起作用。

# function dbasubmenu 
dbasubmenu () {
  options=("DB reorg" "DB index" "DB TableCreation" "DB Submenu quit")
  prompt="Pick a Submenu option: "
  local PS3="$prompt"

  select opt in "${options[@]}"
  do
      case $opt in
          "DB reorg")
              echo "You picked $opt in DB reorg menu for $dbfile"
              ;;
          "DB index")
              echo "you picked $opt in DB index menu for $dbfile"
              ;;
           "DB TableCreation")
             echo "you picked $opt in DB TableCreation menu for $dbfile"
             ;;
          "DB Submenu quit")
              echo "exiting now the DB Submenu"
              return
              ;;
          *) echo "Invalid option $REPLY. Try a valid option ";continue;;
      esac
  done
}


#Main menu
  prompt="Pick a DB Main menu option: "
  PS3="$prompt"

  dbfs=(*.dbf)
   select dbfile in "${dbfs[@]%.dbf}"
    do 
       case $dbfile in
            "DB Mainmenu")
             echo "you picked $dbfile in DB Main menu "
             ;;
             "DB Submenu")
              dbasubmenu $dbfile
             ;;
              "DB Mainmenu quit")
                echo 
                echo "We're all done with the processing !!!!"
                exit
                ;;
            *) echo "Invalid option $REPLY. Try a valid option ";continue;;
          esac

      done

我们在不同的数据库目录中有几个 dbf 文件。例如,a.dbf
b.dbf c.dbf d.dbf。我们需要在 main
中显示所有 4 个文件 菜单,select 上述任何一个 dbf 文件并将该 dbf 文件用于
在子菜单中使用。作业完成后,我们需要退出
退出子菜单 return 返回主菜单 select 另一个
dbf 文件进行处理。

我们现在收到的错误消息是无效选项

基线:

  • 没有主菜单应该是什么样子的示例,并且...
  • 考虑到前 2 个主菜单选项引用了特定的 $dbfile,并且...
  • 维护 OP 的当前输出设计并...
  • 不对 dbasubmenu() 函数进行任何更改并且...
  • 问题同时标记了 bashksh,所以我要选择 bash 和 ...
  • 将文件 a.dbf b.dbf c.dbf 用于演示目的...

我假设主菜单应该如下所示:

1) DB Mainmenu - a
2) DB Mainmenu - b
3) DB Mainmenu - c
4) DB Submenu - a
5) DB Submenu - b
6) DB Submenu - c
7) DB Mainmenu quit
Pick a DB Main menu option:

主菜单代码块现在如下所示:

prompt="Pick a DB Main menu option: "
PS3="$prompt"

# save current COLUMNS sexttting; redefined as relatively narrow to force
# 'select' to display menu list as single column

oldCOLUMNS=${COLUMNS}
COLUMNS=8

dbfs=(*.dbf)                    # load *.dbf file list into array
dbfsmenu=( ${dbfs[@]%.dbf} )    # strip '.dbf' from file names and put in new array;
                                # used solely for menu/display purposes

# add menu prefixes to our dbfsmenu array items, add the 'quit' option on the end

select opt in "${dbfsmenu[@]/#/DB Mainmenu - }" "${dbfsmenu[@]/#/DB Submenu - }" "DB Mainmenu quit"
do
    # process our selected option; for the options where we want $dbfile
    # we'll need to strip off the menu prefix and add the '.dbf' suffix

    dbfile="${opt##* }.dbf"

    case $opt in
        # need to list 'quit' item first to keep from matching
        # wildcard pattern for 'DB Mainmenu*'

        "DB Mainmenu quit")  echo
                             echo "We're all done with the processing !!!!"
                             exit                                           ;;

        "DB Mainmenu"*)      echo "You picked ${dbfile} in DB Main Menue"   ;;

        "DB Submenu"*)       dbasubmenu ${dbfile}                           ;;

        *)                   echo "Invalid option. Try a valid option "
                             continue                                       ;;
    esac
done

# reset COLUMNS to what it was

COLUMNS=${oldCOLUMNS}

我们来测试一下运行:

1) DB Mainmenu - a
2) DB Mainmenu - b
3) DB Mainmenu - c
4) DB Submenu - a
5) DB Submenu - b
6) DB Submenu - c
7) DB Mainmenu quit
Pick a DB Main menu option: 1
You picked a.dbf in DB Main Menue
Pick a DB Main menu option: 2
You picked b.dbf in DB Main Menue
Pick a DB Main menu option: 3
You picked c.dbf in DB Main Menue
Pick a DB Main menu option: 4
1) DB reorg
2) DB index
3) DB TableCreation
4) DB Submenu quit
Pick a Submenu option: 1
You picked DB reorg in DB reorg menu for a.dbf
Pick a Submenu option: 2
you picked DB index in DB index menu for a.dbf
Pick a Submenu option: 3
you picked DB TableCreation in DB TableCreation menu for a.dbf
Pick a Submenu option: 4
exiting now the DB Submenu
Pick a DB Main menu option: 7

We're all done with the processing !!!!

可以稍微清理一下格式以使其更易于阅读,并且可能需要重新显示 follow-on 选择的菜单...我将把它留给 OP ...