Operating System - Linux
1828350 Members
3093 Online
109976 Solutions
New Discussion

POSIX Shell programing issue

 
SOLVED
Go to solution
Andres_13
Respected Contributor

POSIX Shell programing issue

Hi all

I´m a newby in POSIX shell programing and i have this issue:

need to run a program which first ask for an option and again ask for another option and then it starts to refresh some data in the display.

My question is what i can do to get the n the data the program prints in the screen into a text file assuming that i don´t be able to modify such program.
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: POSIX Shell programing issue

Nothing more coplicated than redirecting stdout to a file.

Let's suppose that you want to capture the output of "ls /etc" to a file:

OUTFILE=/var/tmp/myfile
ls /etc > ${OUTFILE}
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: POSIX Shell programing issue

Hi:

# ./myscript > ./myscript.log

# ./myscript >> ./myscript.log

...the first truncates (clears) 'myscript.log' if it exists or creates an empty file if it doesn't and then writes the output (STDOUT) to it.

The second appends STDOUT to 'myscript.log' adding any new data to that already present.

I suggest this link for a quick overview of the POSIX shell:

http://www.docs.hp.com/en/B2355-90046/index.html

Don't forget to read and keep handy the 'sh-posix(1)' manpages, either:

http://www.docs.hp.com/en/B2355-60105/sh-posix.1.html

Regards!

...JRF...
Tim Nelson
Honored Contributor
Solution

Re: POSIX Shell programing issue

If you want to see what is happening on the screen and also send the output to a log you can use the "tee" command.

./myscript |tee -a my.log

Andres_13
Respected Contributor

Re: POSIX Shell programing issue

Thanks for the quick response guys, but that isn't what i need.

Let's say: run "/tmp/programX pf=/tmp/profile" when the program starts it prompts for an input (at this point i have to hit "m" and then have to hit "l"); the output that comes after all this is the one i need to log into a text file.

The question is how?, any clues?

Thanks and regards!
Sandman!
Honored Contributor

Re: POSIX Shell programing issue

You can always script(1) your session so that all your inputs and outputs are recorded in the file typescript. See the mapage of script(1) for details.
A. Clay Stephenson
Acclaimed Contributor

Re: POSIX Shell programing issue

That's bit more complicated and there is no simple way to redirect stdout in the middle of a process --- unless the process itself were coded to accept signals. In you particular case, you need to redirect stdin and stdout.

INFILE=/var/tmp/myinfile
OUTFILE=/var/tmp/myoutfile
# The following is a here docs that will
# put an m and l into a file.

cat << !EOF! > ${INFILE}
m
l
!EOF!

myprof pf=xxx < ${INFILE} > ${OUTFILE}
rm -f ${INFILE}

At this point, you would then need to pass ${OUTFILE} through a filter (sed, awk, Perl) and remove your first few extraneous lines.


If it ain't broke, I can fix that.
Andres_13
Respected Contributor

Re: POSIX Shell programing issue

Thanks for all!!!

Tim and Clay show me two different ways to do. Now i have to run some test to determine which one of them is the best one for me.

Regards!