- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- scripting - read vars from a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2002 09:37 AM
12-23-2002 09:37 AM
UNIX95= ps -e -o ruser,pid,pcpu,vsz,stime,time,args | grep oracleprod | grep -v grep | sort -rnk3 >> ./ps.out
Now I want to do a "for i in `cat ./ps.out`" , BUT, every field is on it's own line in the output file. If I cat the file, or even vi the ps.out file, it appears to be fine, as I would expect it to be.
So I either need to find a way to read 7 lines at a time and assign them to different variables, or find a way to fix the formatting of the ps command so it writes the lines correctly.
Any ideas?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2002 09:46 AM
12-23-2002 09:46 AM
Re: scripting - read vars from a file
To read one line at a time from your file, simply do:
while read LINE
do
your_thing
done < ps.out
...If you want to read the seven fields comprising each line, do:
while read RUSER PID PCPU VSZ STIME TIME ARGS
do
your_thing
done < ps.out
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2002 09:51 AM
12-23-2002 09:51 AM
Re: scripting - read vars from a file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2002 10:12 AM
12-23-2002 10:12 AM
SolutionPS_OUT=`ps -e -o ruser,pid,pcpu,vsz,stime,time,args | grep oracleprod | grep -v grep | sort -rnk3`
(watch the line wrap, all that goes on 1 line)
Next, change the IFS variable to cooperate:
OLD_IFS=$IFS
IFS="^J" (that's a control-J, a linefeed only)
Now this construction will give you an entire line at a time:
for LINE in `echo PS_OUT`; do
[do your stuff with the data here]
done
IFS=$OLD_IFS
The other option you have is leave the ps capture the way you have it and to read from the ps.out file like this:
exec 4<./ps.out
while read -u4 LINE; do
[do your stuff with the data here]
done
Depending on the actual format of the file, you may need to alter the IFS variable as above.
HTH
mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2002 11:11 AM
12-23-2002 11:11 AM
Re: scripting - read vars from a file
Don't set it to 1 in /etc/profile or root profile
Havint it set when you sh HP depots can cause crc errors. Not really, but you won't be able to use the patch.
Steve
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2002 02:07 PM
12-23-2002 02:07 PM
Re: scripting - read vars from a file
If you have predefined fields in your file do:
while read -r RUSER PID PCPU VSZ STIME TIME AGRS
do
.... use your $RUSER etc variables here as you want.
done < ./ps.out
Hope this helps,
0leg
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 01:10 PM
12-26-2002 01:10 PM
Re: scripting - read vars from a file
For example, the output from a 'ps' command might look like this:
gbc 18685 1 0 14:56:03 pts/ta 0:00 /usr/bin/newmail -i90
If you pipe that to awk, within awk each field on the line is assigned a parameter variable:
$0 = the entire line
$1 = gbc
$2 = 18685
$3 = 1
$4 = 0
$5 = 14:56:03
$6 = pts/ta
$7 = 0:00
$8 = /usr/bin/newmail
$9 = -i90
Anyway I can't give you an awk tutorial here but if you already know some shell scripting stuff, you can handle awk. The best book about the language is "The Awk Programming Language" by Aho, Kernighan and Weinberger.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 01:13 PM
12-26-2002 01:13 PM
Re: scripting - read vars from a file
Fred
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 03:37 PM
12-26-2002 03:37 PM
Re: scripting - read vars from a file
looks like your more after the part inside the loop?
How about using the POSIX features of "ps", and suppressing the headline:
UNIX95=. ps -e -C your,processes,go,here -o ruser=,pid=,pcpu=,vsz=,stime=,time=,args= |
while read ruser pid pcpu vsz stime time args
do case "$ruser" in
process#1) do-something-here ;;
process#2) do-something-else ;;
esac
done
FWIW,
Wodisch
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2002 09:15 AM
12-27-2002 09:15 AM
Re: scripting - read vars from a file
exec 4<$psout2
while read -u4 LINE
do
_usr=`echo $LINE | awk '{print $1}'`
_pid=`echo $LINE | awk '{print $2}'`
_cpu=`echo $LINE | awk '{print $3}'`
_vsz=`echo $LINE | awk '{print $4}'`
_sti=`echo $LINE | awk '{print $5}'`
_tim=`echo $LINE | awk '{print $6}'`
_arg=`echo $LINE | awk '{print $7}'`
done
Not exceptionally elegant, but it works! ;)