LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-09-2006, 10:38 AM   #1
panchosansa
LQ Newbie
 
Registered: Oct 2006
Posts: 18

Rep: Reputation: 0
Just finished my first script. Need a bit of help formatting it.


Hi everyone,

It's a pleasure for me to join your community.
I am still a "freshman" in Linux and in bash scripting, but I am glad to say that with a bit of help I managed to complete my first script. But much more important is the fact that I am getting increasingly enthusiastic and hooked to this great OS and the many unique possibilities that scripting presents...
And here is the final product of many hours of hard work
Code:
#!/bin/bash
echo
echo "This script will find all the open ports on a computer."
echo "It will also show the process that has opened each one and the number of files opened per process."
echo
echo

showProcesses()
{	
   lsof -i | uniq -f 7 > table.list
   cat table.list | awk 'NR>1{print "Process name\t"$1"\t\t\ton port\t\t"$8}'
}

showNumberOfFiles()
{
	cat table.list | awk 'NR>1{print $2}' > PID.list
	while read pid; do
	lsof -p $pid | wc -l | awk '{print "\t\t\tNumber of files opened\t"$1}'
	done < PID.list
}

showProcesses > file1.list

showNumberOfFiles > file2.list

paste file1.list file2.list

echo
It's a fairly simple one and what I want to do with it now is to format it like a table. I used the paste command and some tabs to try and allign the data so that it is legible but coudldn't do it. Does anyone have an idea how I can accomplish this?

Kindest regards,
Valentin
 
Old 10-10-2006, 05:53 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Hello and welcome to LQ. Hope you like it here.

Pipe the output through "column -t":
Code:
output() { lsof -w -n -i | uniq -f 7; }
output | awk 'NR>1{print "Process name\t"$1"\t\t\ton port\t\t"$8}' | column -t
output | awk 'NR>1{print $2}' | while read pid; do 
 lsof -w -n -p $pid | wc -l | awk '{print "\t\t\tNumber of files opened\t"$1}'
done | column -t
BTW, as you see you don't have to use temporary files. If you do it's best to use "mktemp" for that and have a portion of the name be the same as the script you're running so you can spot them if they don't get deleted. Also note the output function (saves typing), the switches for lsof (just my best practice to discard warnings and speedup by discarding resolving) and the number of open files (true in the sense everything is a file but those are not all real files).
 
Old 10-10-2006, 06:07 AM   #3
panchosansa
LQ Newbie
 
Registered: Oct 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks very much, unSpawn. I enjoy my time here and I am sure I would be a regular on this forum because I want to develop my scripting skills. I've found your remarks very useful and will bear them in mind for future reference.
 
Old 10-10-2006, 08:31 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
you may also want to look at printf() http://www.tldp.org/LDP/abs/html/internal.html#EX47
based on C fn of same name.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
32 bit or 64 bit install - is 32 bit easier for a newbie? dms05 Linux - Newbie 3 05-19-2006 03:05 PM
What do you do after all is finished mdkusr General 12 01-29-2006 11:44 PM
finished new installation DanoL Linux - Newbie 2 05-03-2004 08:39 PM
Un-finished Install bigalusn Linux - Newbie 4 09-25-2003 07:05 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration