Operating System - HP-UX
1835346 Members
2698 Online
110078 Solutions
New Discussion

how to list commands and their output in different pages.

 
SOLVED
Go to solution
Hanry Zhou
Super Advisor

how to list commands and their output in different pages.

I have quite a lot of commands, mostly they are cat, ls, as well as other commands.

Now, I want to write a script, then have outputs to list each one of commands itself and following it's corresponding output. Each command and it's output should start with different page.

Thanks in advance.
none
20 REPLIES 20
James R. Ferguson
Acclaimed Contributor

Re: how to list commands and their output in different pages.

Hi:

#!/usr/bin/sh
exec >> /tmp/mylog
CMD="cat /etc/hosts"
echo ${CMD}
eval ${CMD}
echo "\015"
# etc...

...prn [repeat as necessary]...

Notice that the redirection is done once for the duration of your script.

Regards!

...JRF...


Laurent Menase
Honored Contributor

Re: how to list commands and their output in different pages.

What do you mean by different page?
Do you mean print on a printer on different page?
Something like

while read a
do
banner "$a"
$a
echo ^L # type ctrl V ctrl L
done <ls
cat /tmp/toto
ps
echo yes
EOF

Sometimes generate in html it gives some nice outputs.
Hanry Zhou
Super Advisor

Re: how to list commands and their output in different pages.

#!/usr/bin/sh
exec >> /tmp/mylog
CMD="cat /etc/hosts"
echo ${CMD}
eval ${CMD}
echo "\015"
# etc...


Okay. Understand...
but, how would you put all different commands, for instance, cat /etc/hosts, cat /etc/passwd,....in a loop, and assign each command to CMD?

Thanks...
none
James R. Ferguson
Acclaimed Contributor
Solution

Re: how to list commands and their output in different pages.

Hi (again):

#!/usr/bin/sh
exec > mylog
while read CMD
do
echo "${CMD}"
eval "${CMD}"
echo "\015"
done < mycmds

...where 'mycmds' simply looks like:

# cat mycmds
cat /etc/hosts
cat /etc/services
vgdisplay
date

...notice the double quotes in the script.

Regards!

...JRF...
Hanry Zhou
Super Advisor

Re: how to list commands and their output in different pages.

Thanks JRF, It works, except one thing.

echo "\015" seems only separate each command and it's output by one empty line, how do I separate them by starting a new page?
none
Hein van den Heuvel
Honored Contributor

Re: how to list commands and their output in different pages.

try \014 for "Form Feed" instead of \015 for "Carriage Return"

Hein.
Hanry Zhou
Super Advisor

Re: how to list commands and their output in different pages.

No.. Hein, echo "\014" only give you ^L

in between each group of outputs...
none
James R. Ferguson
Acclaimed Contributor

Re: how to list commands and their output in different pages.

Hi (again):

> Hein: try \014 for "Form Feed" instead of \015 for "Carriage Return

Yes, indeed! Thanks my friend.

Regards!

...JRF...
Geoff Wild
Honored Contributor

Re: how to list commands and their output in different pages.

What about "\012"

Does that work?

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
James R. Ferguson
Acclaimed Contributor

Re: how to list commands and their output in different pages.

Hi Hanry:

Yes, a ^L when viewed with 'vi' (for example) is indeed a formfeed (\014). If you send the generated file to a printer, it will be honored as such. Using a pager like 'more' to view the file at your terminal will not interpret the character either. A 'cat' of the file would, but that would be a bit useless.

I think you would be better served by injecting a line of dashes to separate each command and its output. That way the output file could be viewed on a terminal as well as printed.

In fact, if you wanted to print the file, simply use 'sed' to change the dashes to true form feeds:

# sed s'/^------/\^L/' file | lp

Regards!

...JRF...

Hanry Zhou
Super Advisor

Re: how to list commands and their output in different pages.

No...
echo "\012" will give you 2 empty lines in between.

Just want to make sure, I am not going to print out the entire output, just into a unix file.
none
Geoff Wild
Honored Contributor

Re: how to list commands and their output in different pages.

How about awk?


echo "" | awk '{print "\f"}'

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Hanry Zhou
Super Advisor

Re: how to list commands and their output in different pages.

no.. this awk command seems does nothing.
none
James R. Ferguson
Acclaimed Contributor

Re: how to list commands and their output in different pages.

Hi:

Hanry, re-read my last offering.

Regards!

...JRF...
Hanry Zhou
Super Advisor

Re: how to list commands and their output in different pages.

Yes,JRF, I understand, instead of looking for starting from a different page, I can use dashes to separate them. That seems the best solution for now...
none
Steven Schweda
Honored Contributor

Re: how to list commands and their output in different pages.

> [...] Each command and it's output should
> start with different page.

That's "its". A "different page" on what?

Ctrl/L = 12 (dec) = 0xC (hex) = 014 (octal) =
Form Feed, which is ASCII for "go to the next
page".

> I am not going to print out the entire
> output, just into a unix file.

Define "different page" in "a unix file".
Hanry Zhou
Super Advisor

Re: how to list commands and their output in different pages.

Here is what I want:

page 1:
command1
command1 output




page 2:
command2
command2 output

....


page n:
commandn
commandn output
none
Laurent Menase
Honored Contributor

Re: how to list commands and their output in different pages.

try mine:

while read a
do
echo $a
$a
echo "\014" # or ^L
done <ls
ps
echo yes
EOF
Hanry Zhou
Super Advisor

Re: how to list commands and their output in different pages.

Laurent Menase , yours is working very well.

so, echo "\014" only works when you combine with "pr" command.
none
Hein van den Heuvel
Honored Contributor

Re: how to list commands and their output in different pages.

Hanry,

The problem is, as Steven to clearly points out, the defintion of 'a page'.

This sounds simple and intuitive, but it is not.


IF you are sending a file to a printer THEN a FormFeed character, aka FF, aka ^L, aka \014 will instruct most (all?) printers to skip to the next page.

Some online programs will honor this in an emulated fashion.
'cat' does not... it just sends it through so then it depends on your terminal (emulator) settings what happens.
Typically it skips to a clean page, making it pointless.

hpux 'more' donly displays a replacement "^L".

Linux 'more' which typically is 'less' does more.
It does it 'correctly' pausing after each 'page'.
Try to get a 'less' for hpux.

Your best bet IMHO is to inject a good few new-lines and a recognizable pagebreak indicator.

Untested: echo "\n\n\n\n--- $CMD ---\n\n"

hth,
Hein.