Solved

variable size

Posted on 2000-04-05
8
219 Views
Last Modified: 2010-04-21
I have a shell program that issues a command and redirects the resulting output to a file.  The output, in some cases, is 165 characters long. Only 126 characters are being written to the file via the redirect! How can I make it write the entire line to file instead of truncating at 126 chars?
COMMAND:
ps -ef | grep lmg > output.file
SHELL:
ksh
0
Comment
Question by:carydb
8 Comments
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2687564
Given the way that the shell sets up the redirection, it is highly unlikely that this is part of the problem.  Are you sure the output is different with and without the redirect.

I'm guessing that the 'full width' output is coming from the ps when going straight to the terminal without first passing through the pipe to grep.  Perhaps the version of ps you are using has different behavior when dealing with pipes vs terminals, or the version of grep is truncating the line.
0
 
LVL 2

Expert Comment

by:festive
ID: 2688250
you could also do something like:
LOGFILE="logfile.log"
exec 1>$LOGFILE 2>&1

this will redirect all script/error output to the logfile and may help locate the problem.

you can try different switches with ps also to give more/less details.
0
 
LVL 1

Expert Comment

by:Astroman
ID: 2690198
if you do

ps -ef | grep lmg | tee output.file

You will get the output both on the screen, and in the file. That might help see what's happening.
0
 
LVL 14

Accepted Solution

by:
chris_calabrese earned 20 total points
ID: 2690739
Again, I'm guessing that the problem is that either ps is changing its behavior going to a pipe vs the terminal or that grep itself is the culprit.  No ammount of changing how the shell or some other program ends up eventually executing
  filedes=open(filename, O_CREAT);
  close(1);
  dup2(filedes, 1);
is going to change this.

Step 1:
  Run just 'ps -ef' to the terminal and see what you get.

Step 2:
  Run 'ps -ef | more' t and see what you get.  If this is different than in step 1, the problem lies with ps changing its behavior when dealing with a pipe vs a terminal.  Some versions of ps support -w, -ww, and/or -www to specify full-width output in these situations.

Step 3:
  Run just the 'ps -ef | grep lmg | more'  without the redirect and see what you get.  If this is different than in step 2, the grep is to blame.  Try the GNU grep from www.gnu.org if this is the case.

Step 4:
  Run the whole 'ps -ef | grep lmg > output.file' and see what you get.  If this is different than in Step 3, you must be using a very very strange shell that does not implement redirection properly.  Trhy a different shell.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:carydb
ID: 2690964
Chris (chris_calabrese).  I thought that ps -ef was printing the entire line to screen, but it is not.  I guess the issue is that ps is truncating the output to screen.  I did not find a -w option in the ps man page.  Any suggestions on how I can get an untruncated output from ps?  Thanks
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2691254
Is the behavior different between a pipe and terminal on the ps output, or is it that ps is simply never showing you the full command line you want?

If it's the former, you might be able to fool ps into thinking the output is a terminal by the clever use of pseudo terminals - easiest if you use the expect tool.

If it is always truncating the line, there's probably not much you can do short of getting the source code to your version of ps (assuming open source or you have a source licence) and modifying it.  You might also be able to start with the source code to the 'top' program (try http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/top-3.5beta9/).
0
 

Author Comment

by:carydb
ID: 2691294
Adjusted points from 10 to 20
0
 

Author Comment

by:carydb
ID: 2691295
You have been a lot of help. Thanks. I will see about changing the ps code if I can. Thanks again!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now