如何使用ksh / bash

时间:2019-06-30 14:37:12

标签: bash ksh

列出菜单中的文件数。

主要目标是从菜单中的文件列表中选择一个选项,然后在子菜单中使用该选择的选项。对我有用的脚本是主菜单选项不变的地方。当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

我们在不同的db目录中有几个dbf文件。例如,a.dbf
    b.dbf c.dbf d.dbf。我们需要将所有4个文件显示在主窗口中
    菜单中,选择上述dbf文件中的任何一个,并使用该dbf文件来
    在子菜单中使用。工作完成后,我们需要退出
    从子菜单中退出并返回主菜单以选择另一个
    dbf文件进行处理。

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

1 个答案:

答案 0 :(得分:0)

基线:

  • 没有示例主菜单的外观,并且...
  • 考虑到前两个主菜单选项引用了特定的$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 !!!!

可以对格式进行一些整理以使其更易于阅读,并且可能需要重新显示菜单以供后续选择...我将其留给OP ...