创建具有下一页unix bash的脚本

时间:2017-09-11 15:30:46

标签: bash output text-manipulation

我有10个文本文件,我计划将其显示为模拟用户如何使用我的脚本的手册页的输出。但是,我正在寻找每页显示10页页面的最佳方式。

例如:如果用户在键盘上按1,他们将被带到第一页,如果他们在键盘上按2,则他们将被带到第二页。我原计划在这里使用[ { "$addFields": { "prop2": { "$arrayToObject": { "$map": { "input": { "$objectToArray": "$prop2" }, "as": "prop_2", "in": { "k": "$$prop_2.k", "v": { "$cond": [ { "$eq": [ "$$prop_2.k", "prop21" ] }, { "$arrayToObject": { "$map": { "input": { "$objectToArray": "$$prop_2.v" }, "as": "prop_21", "in": { "k": { "$arrayElemAt": [ { "$split": [ "$$prop_21.k", "_" ] }, 0 ] }, "v": "$$prop_21.v" } } } }, "$$prop_2.v" ] } } } } } } } ] 结构。如果用户按下3,则打开包含我的联系信息的文本文件,如果他们按4,他们将返回主菜单。然而,考虑到我有10个要显示的页面,我相信使用case构造是非常低效的。

总而言之,我只想用1来让用户转到上一页,而2则让用户转到下一页。

我现在有点困在我脚本的这一部分了一段时间。非常感谢你!

2 个答案:

答案 0 :(得分:0)

每个UNIX系统都有一个软件可以为你做这件事 - 它被称为寻呼机。实际上,大多数系统都有几个寻呼机 - more,传统的UNIX寻呼机,以及less,GNU项目的现代化,功能强大的功能。

按照惯例,系统的用户可以通过设置环境变量PAGER来配置自己的寻呼机,这样他们甚至可以决定在其他软件中显示文本 - 预打开的编辑器中的窗口,GUI窗口,文本转语音系统或他们使用和喜欢的任何其他内容。

以下是如何以健壮的方式选择寻呼机的示例。

# Function that picks the best available pager
pager() {
  # if stdout is not to a TTY, copy directly w/o paging
  [ -t 1 ] || { cat; return; }

  if [ -n "$PAGER" ]; then  ## honor the user's choice, if they have a pager configured
    "$PAGER"
  elif command -v less >/dev/null 2>&1; then
    less
  elif command -v more >/dev/null 2>&1; then
    more
  else
    echo "WARNING: No pager found; falling back to cat" >&2
    cat
  fi
}

pager <<'EOF'
here is our documentation
several pages of content here
etc, etc, etc
EOF

答案 1 :(得分:0)

我能够找到一个解决方案,但事实上我可以将10个手册页堆叠成一个数组并逐个滚动它们。这是我的解决方案。

 declare -a manual=("$pg0" "$pg1" "$pg2" "$pg3" "$pg4" "$pg5" "$pg6" "$pg7" "$pg8" "$pg9" "$pg10")

promptchoice()
{
echo -e "\nPress the following keys to browse through this section!
1) Previous Page
2) Next Page
3) Contact Details
4) Back to main Menu"
echo -n "Your selection: "
}

pagearray()
{
x=0
clear
#promptchoice
#read a
while [ $x -lt "11" ]

do
promptchoice
read a    
    if [ $a -eq 1 -a $x -gt 0 ];
    then echo -e "${manual[$"x-1"]}\n"
    x=$(($x-1))


    elif [ $a -eq 2 -a $x -lt 10 ];
    then echo -e "${manual[$"x+1"]}\n" 
    x=$(($x+1))


    elif [ $a -eq 3 ];
    then echo -e "$contact\n"

    elif [ $a -eq 4 ];
    then bash mainmenu.sh

    else [ $a -eq * ];
    echo "Error in your input! Did you press previous despite not being on any page first?"
    echo "Or perhaps you went over the last page and still pressed next?"
    echo "Please try again!"
    sleep 3
    pagearray

if ($x = "11");
    then
    echo "You have seen the last customer care page! You will now be taken back to the prev menu."
    sleep 2
    pagearray
    fi

    fi

done
}

希望可能与我有同样问题的人发现这也很有帮助。