LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 06-05-2007, 08:34 AM   #1
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Rep: Reputation: 30
Pipe a bash script into less in the script?


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.


Original Script

echo "-----------"
uptime
echo
#check Disk Space
echo "Disk Utilization"
echo "----------------"
df -F ufs -h
echo
#Verify all services are running
echo "Puremessage-Services currently running"
echo "--------------------------------------"
pmx-service
echo
echo "Puremessage-Updates"
echo "-------------------"
pmx-verify | egrep 'AntiSpam-Data|Blocklist-Data|Sophos-Data'
echo
echo "Postfix Queue"
echo "-------------"
mailq | grep "Request."
echo
echo "Postfix-Qshape"
echo "--------------"
/opt/pmx/postfix/sbin/pmx-qshape
echo "Postfix-Active"
echo "--------------"
echo
/opt/pmx/postfix/sbin/pmx-qshape active
echo
echo "Postfix-Deferred"
echo "----------------"
/opt/pmx/postfix/sbin/pmx-qshape deferred
echo
echo "Postfix-Incoming"
echo "----------------"
/opt/pmx/postfix/sbin/pmx-qshape incoming
echo
echo "Milter Status"
echo "-------------"
pmx-milter status

#End


Thanks for the help.

Last edited by keysorsoze; 06-05-2007 at 08:36 AM.
 
Old 06-05-2007, 09:11 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Although I do not know the behavior of the pmx* commands, you could solve this as follows:
Code:
#!/bin/bash

function allCommands() {
  echo "-----------"
  uptime
  echo

  #check Disk Space
  echo "Disk Utilization"
  echo "----------------"
  df -F ufs -h
  echo

  #Verify all services are running
  echo "Puremessage-Services currently running"
  echo "--------------------------------------"
  pmx-service
  echo
  echo "Puremessage-Updates"
  echo "-------------------"
  pmx-verify | egrep 'AntiSpam-Data|Blocklist-Data|Sophos-Data'
  echo
  echo "Postfix Queue"
  echo "-------------"
  mailq | grep "Request."
  echo
  echo "Postfix-Qshape"
  echo "--------------"
  /opt/pmx/postfix/sbin/pmx-qshape
  echo "Postfix-Active"
  echo "--------------"
  echo
  /opt/pmx/postfix/sbin/pmx-qshape active
  echo
  echo "Postfix-Deferred"
  echo "----------------"
  /opt/pmx/postfix/sbin/pmx-qshape deferred
  echo
  echo "Postfix-Incoming"
  echo "----------------"
  /opt/pmx/postfix/sbin/pmx-qshape incoming
  echo
  echo "Milter Status"
  echo "-------------"
  pmx-milter status
  #End
}

allCommands | less
Hope this helps.
 
Old 06-05-2007, 09:25 AM   #3
thegeekster
Member
 
Registered: Dec 2003
Location: USA (Pacific coast)
Distribution: Vector 5.8-SOHO, FreeBSD 6.2
Posts: 513

Rep: Reputation: 34
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.
 
Old 06-05-2007, 09:28 AM   #4
thegeekster
Member
 
Registered: Dec 2003
Location: USA (Pacific coast)
Distribution: Vector 5.8-SOHO, FreeBSD 6.2
Posts: 513

Rep: Reputation: 34
LOL......druuna beat me to the punch.........

Now you have a couple of different ways to do it, either use a function or manipulate file descriptors.......
 
Old 06-05-2007, 10:41 AM   #5
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
separte pipe for stderr and stout in a bash script ask4info Programming 7 09-07-2012 01:39 AM
Shell script pipe input - bash mostly laikos Programming 4 11-09-2008 05:14 PM
Bash script hangs upon starting another script in the background masea2 Linux - Software 4 11-13-2006 05:18 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration