Scroll through shell script output without dropping it

I have a bash script.sh. I can easily scroll through the output as follows:

$ ./script.sh | less 

But how to automatically scroll output without having to scroll through less ? In other words, how to place this functionality directly in the script itself? I just want to execute the script as follows:

 $ ./script.sh 

I know that I could write another script to execute the first, and automatically output the output, but I do not want to write another script to get the first, to do what I want it to be done. Do you know what I mean?

+4
source share
3 answers

You can write your script as follows:

 #!/bin/bash ( Your script here ) | less exit $PIPESTATUS 

This will lead to the output of the script via less if the output is terminal (so you can ./script.sh > file without paging) and it saves the script exit code.

+5
source

Normally, just add the following to the script

 #!/bin/bash ( # add this to the start #your old script here date cat /etc/passwd df ls -l #end of your script ) | less #and add this to the end 

or you can put the whole script in a bash function like

 #!/bin/bash the_runner() { #your old script here date cat /etc/passwd df ls -l #end of your script } the_runner " $@ " | less 
+3
source

Instead of changing the script itself, I decided to add a special binding to Bash.

Effectively, you can write ./script.sh instead of ( ./script.sh ) | more ( ./script.sh ) | more .

Here is what you need to add to your .bashrc :

 # Switch to vi mode (default is emacs mode). set -o vi dont_scroll_down() { # Add the command to your history. history -s "$READLINE_LINE" # Redirect all output to less. bash -c "$READLINE_LINE" 2>&1 | less -eFXR # Remove the command from the prompt. READLINE_LINE='' # Optionally, you can call 'set -o vi' again here to enter # insert mode instead of normal mode after returning from 'less'. # set -o vi } bind -m vi -x '"J": "dont_scroll_down"' 

As a result, you can do the following:

  • Enter the command you want to run.

     $ ./script.sh 
  • Press Escape to exit insert mode and enter normal mode.

  • Now press Shift-j to execute the line.

Now you can scroll through the output from the very beginning.

0
source

Source: https://habr.com/ru/post/1480779/


All Articles