Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi! I had a question of how to pipe output from a script into a pager such as less so we can scroll up and down to view contents. Currently I have the following script called pmxcheck.sh that checks various aspects our mail gateway. The script puts out a lot of info, I had to create another script called pmxchk.sh that basically contains the following:
#!/bin/bash
pmxcheck.sh | less
This just basically runs the script and pipes it into the less pager to prevent the output from scrolling off screen. My question is how to integrate the less pager into my original script so we don't need two files to do one thing.
Hmmmm.....Off the top of my head, you might have the best luck by redirecting all output to the pager at the beginning of the output commands ......This can be accomplished a couple of different ways: one, by redirecting everything to a different file descriptor, or just simply redirecting the output straight to the pager, depending on what else the script is doing.........The canonical way is to use an extra file descriptor, like so....
Code:
#! /bin/bash
<script setup commands, such as variable assignments, etc>
exec 3>&1 # Setting FD1 (stdin) to FD3
exec 1 | less # piping FD1 to less
<commands for output to pager go here>
exec 1>&3 # resetting stdin back to FD1
exec 3>&- # closing off FD3
<rest of script>
exit 0
HTH
---thegeekster
Last edited by thegeekster; 06-05-2007 at 09:30 AM.
Thank you thegeekster, and druuna your tips have solved my problem. I simply wrapped the entire script in the function as recommended and it worked perfectly.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.