Creating a script that has next page unix bash

1k views Asked by At

I have 10 text files with me that I plan on displaying as an output simulating a manual page on how a user could use my script. However I'm looking for the most optimal way in going about displaying the 10 pages page per page.

In example: If a user press 1 on their keyboard they'll be taken to the first page, and if they press 2 on their keyboard they'll be taken to the second page. I originally planned to use a case structure here. If the user presses 3 it'll open a text file containing my contact info, and if they press 4 they'll go back to the main menu. However considering that I have 10 pages that are to be displayed, it would be very inefficient to use a case construct on this, I believe.

So to summarize, I would only like to use 1 for the user to go to the previous page, and 2 for the user to go to the next page.

I'm kinda bit of stuck in this part of my script for a while now. Thank you very much!

2

There are 2 answers

5
Charles Duffy On

Every UNIX system has a piece of software that will do this for you -- it's called a pager. Actually most systems have several pagers -- more, the traditional UNIX one, and less, the GNU project's modern, featureful take.

By convention, a system's user can configure their own pager by setting the environment variable PAGER, so they can even decide to have text displayed in some other piece of software -- a window in their pre-opened editor, a GUI window, a text-to-speech system, or whatever else they use and prefer.

The following is an example of how to select a pager in a robust way.

# 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
0
Gifter Villanueva On

I was able to find out a solution for this and it just so happened that I could stack the 10 manual pages into an array and scroll through them one by one. Here's my solution.

 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
}

Hopefully someone who might have the same problem as I do finds this helpful as well.