LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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
 
LinkBack Search this Thread
Old 06-05-2007, 09: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 09:36 AM.
 
Old 06-05-2007, 10:11 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,002
Blog Entries: 2

Rep: Reputation: Disabled
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, 10:25 AM   #3
thegeekster
Member
 
Registered: Dec 2003
Location: USA (Pacific coast)
Distribution: Vector 5.8-SOHO, FreeBSD 6.2
Posts: 513

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

Rep: Reputation: 32
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, 11: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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


All times are GMT -5. The time now is 06:28 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration