LinuxQuestions.org
Review your favorite Linux distribution.
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 03-23-2009, 04:54 PM   #16
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Rep: Reputation: 57

Quote:
Originally Posted by circuit_girl View Post
I am very new to Unix but I am learning. I am working on script writing. I know a lot of perl but as far as bash and awk and all the other scripting languages that is what I am trying to learn.

I want to create a script that when it is ran it will execute the last command to display who has logged into my system. I want to capture all these users and display how many times they have logged in.

What is the best language to use and where do I begin. I need some help getting started. I am completely lost.

Thank you
a cool idea would be a hack of the inittab to log the users, and avoid using whatever kind of :
- sleep
- watch
- last,
that are not very very bright . lol

ftp/ssh/root jail kit could be installed to run a script at every login

or

a just hack of the getty and login commands would be enough
Code:
1:2345:respawn:/sbin/getty 38400 tty1
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3
...
and so on.


Enjoy!

Last edited by frenchn00b; 03-23-2009 at 04:56 PM.
 
Old 03-23-2009, 05:59 PM   #17
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
I'm not arguing against perl. It's an essential tool. But you want to learn the other tools in your tool box as well. There are many times as a sysadmin, when, rather than having to edit a piece of perl code and then run it, I can just type a one liner and get the results I want. Case in point above.

I have some rather lengthy shell scripts that fill a particular need and work well.

I have perl scripts that I use regularly, and one, in particular, that I edit and re-use every time I'm troubleshooting and reading log files.

So it's not an either/or. Learn both, and use appropriately.

When I was in the midst of weeks of intensive sysadmin training, there was an in class assignment that I sat staring at for a bit and then typed one line and got the answer. Others in the class were at it much longer, and the instructor's answer was two pages of code. I thought my use of three pipes and a regex was rather elegant, and a lot less headache than two pages of code.

I have also programmed in many languages, and, in the early 1980's wrote an in depth interactive graphics program that did symbolic projective geometry (all euclidean geometry can be expressed in matrix algebra) in Fortran. The high speed printer output of the code listing ultimately was 2 inches thick.
 
Old 03-23-2009, 06:12 PM   #18
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,109

Rep: Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328Reputation: 3328
Quote:
Originally Posted by choogendyk View Post
I'm not arguing against perl. It's an essential tool. But you want to learn the other tools in your tool box as well.
Indeed - and I wasn't proposing only perl. The problem can be solved easily by bash, perl, python ... pick a favoured.
It just seems to me that chains of stand-alone utilities is unlikely to teach anyone script writing skills.
 
Old 03-23-2009, 08:48 PM   #19
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by syg00 View Post
It just seems to me that chains of stand-alone utilities is unlikely to teach anyone script writing skills.
well, what constitute script writing then?
I just imagine that these stand alone utilities are equivalent to modules and functions. all these "tools and utilities" work together to perform a task, just like in any other languages,
 
Old 03-23-2009, 09:18 PM   #20
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
Quote:
Originally Posted by syg00 View Post
Indeed - and I wasn't proposing only perl. The problem can be solved easily by bash, perl, python ... pick a favoured.
It just seems to me that chains of stand-alone utilities is unlikely to teach anyone script writing skills.
& quoting the OP

Quote:
Originally Posted by circuit_girl View Post
I am very new to Unix but I am learning. I am working on script writing. I know a lot of perl but as far as bash and awk and all the other scripting languages that is what I am trying to learn.

I want to create a script that when it is ran it will execute the last command to display who has logged into my system. I want to capture all these users and display how many times they have logged in.
To me, scripting differs from programming. Scripting begins with tossing together stand alone lines with maybe some control structures to achieve a usable utility. Programming is more an activity of developing software using a language like C or C++. They blend in the middle. Several open source projects use perl or python to develop the structure of the software and then C or C++ for core elements that are performance critical. But it's difficult to be good at that without professional or academic training in programming.

Back to the OP. She originally said she knew a lot of perl and wanted to learn more, say, bash and awk, etc. I use ksh, but, in principle, it's the same. So, scripting. Start by learning individual commands. Understand the use of standard in, standard out, redirection, and pipes. Learn and understand regex in the different contexts (grep, sed, awk, shell expansion, etc). Learn control structures (if-then-else, case, while, etc). Learn different variable types. Look at examples in the OS environment and decode them.

The books I referenced in my first post in this thread provide the background, details and reference to dig into all of that in a fair bit of depth.

Since the OP said she knew a fair bit of perl, and wanted to learn more shell, awk, etc., it would seem that providing an answer that starts with the elements involved in a shell one liner is in the right direction. It's one of the fundamental concepts of Unix/Linux. Take simple tools that do one thing well and string them together with standard I/O and pipes. Put it in a file and add a #!/bin/bash at the top and you can make it a shell script.
 
Old 03-24-2009, 02:42 AM   #21
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by choogendyk View Post
I'm not arguing against perl. It's an essential tool. But you want to learn the other tools in your tool box as well. There are many times as a sysadmin, when, rather than having to edit a piece of perl code and then run it, I can just type a one liner and get the results I want. Case in point above.

I have some rather lengthy shell scripts that fill a particular need and work well.

I have perl scripts that I use regularly, and one, in particular, that I edit and re-use every time I'm troubleshooting and reading log files.

So it's not an either/or. Learn both, and use appropriately.

When I was in the midst of weeks of intensive sysadmin training, there was an in class assignment that I sat staring at for a bit and then typed one line and got the answer. Others in the class were at it much longer, and the instructor's answer was two pages of code. I thought my use of three pipes and a regex was rather elegant, and a lot less headache than two pages of code.

I have also programmed in many languages, and, in the early 1980's wrote an in depth interactive graphics program that did symbolic projective geometry (all euclidean geometry can be expressed in matrix algebra) in Fortran. The high speed printer output of the code listing ultimately was 2 inches thick.
Perl also is famous for one liners :-).

The point, though, is when one needs flow control constructs like 'if', 'for', 'while', etc., the Perl code is better than shell code.

I, of course, use a lot of things on command line with pipes - maybe sometimes up to 10 pipes on one command line. Most frequently used utilities are 'grep', 'cut', 'sort', 'uniq'.

I often use 'grep' with '-P' switch though :-).
 
Old 03-24-2009, 02:51 AM   #22
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
The point, though, is when one needs flow control constructs like 'if', 'for', 'while', etc., the Perl code is better than shell code.
well, better in terms of ?? care to elaborate? is it conciseness? readability?
 
Old 03-24-2009, 03:26 AM   #23
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
well, better in terms of ?? care to elaborate? is it conciseness? readability?
It is better in a sense that Perl (and Python for that matter) are more powerful languages than shell, i.e. they allow the programmer to achieve more with less effort and more efficiency.

Readability is a subjective matter, but I would say that Perl/Python code is more readable than shell code under other equal circumstances.

Conciseness ? Well, similar to shell one can write, say

Code:
system($cmd1) or system($cmd2) ... or system($cmdN);
in Perl - in shell it would be

Code:
$cmd1 && $cmd2 ... && $cmdN
, just the '&&' are counterintuitive here.
 
Old 03-24-2009, 07:15 AM   #24
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
Quote:
Originally Posted by Sergei Steshenko View Post
... just the '&&' are counterintuitive here.
And perl can't be counter intuitive? For an extreme example, please decode the following:

Code:
#:: ::-| ::-| .-. :||-:: 0-| .-| ::||-| .:|-. :||
open(Q,$0);while(<Q>){if(/^#(.*)$/){for(split('-',$1)){$q=0;for(split){s/\|
/:.:/xg;s/:/../g;$Q=$_?length:$_;$q+=$q?$Q:$Q*20;}print chr($q);}}}print"\n";
#.: ::||-| .||-| :|||-| ::||-| ||-:: :|||-| .:|
From http://mysite.verizon.net/les.peters/id2.html

It's in large part a question of both style and familiarity. I try to write clearly and comment my code to explain the intention in whatever language or environment I'm working in. The && in the shell script is totally familiar. It has the important property that the second command is only executed if the first succeeded. So,

cd $DIR && ...

will not proceed with the rest of the commands on the line if the cd fails (e.g. no such directory).
 
Old 03-24-2009, 07:29 AM   #25
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by choogendyk View Post
And perl can't be counter intuitive? For an extreme example, please decode the following:

Code:
#:: ::-| ::-| .-. :||-:: 0-| .-| ::||-| .:|-. :||
open(Q,$0);while(<Q>){if(/^#(.*)$/){for(split('-',$1)){$q=0;for(split){s/\|
/:.:/xg;s/:/../g;$Q=$_?length:$_;$q+=$q?$Q:$Q*20;}print chr($q);}}}print"\n";
#.: ::||-| .||-| :|||-| ::||-| ||-:: :|||-| .:|
From http://mysite.verizon.net/les.peters/id2.html

It's in large part a question of both style and familiarity. I try to write clearly and comment my code to explain the intention in whatever language or environment I'm working in. The && in the shell script is totally familiar. It has the important property that the second command is only executed if the first succeeded. So,

cd $DIR && ...

will not proceed with the rest of the commands on the line if the cd fails (e.g. no such directory).
Any language can be used in counterintuitive manner.

My point is that if I have something that looks like

$foo && $bar && $doo && $dah

, it looks like Boolean expression to me.

And since it looks like Boolean expression, for it _not_ to short circuit each member needs to yield TRUE, which is traditionally _not_ 0.

In shell it's the opposite - exit status should be 0 for execution to continue.

So, in this respect shell is counterintuitive by construction.
 
Old 03-24-2009, 12:00 PM   #26
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
Quote:
Originally Posted by Sergei Steshenko View Post
So, in this respect shell is counterintuitive by construction.
Only if your intuition is wedded to another tool.

From a logical perspective, if the first command fails, then you have your answer -- the overall construct has failed (&& cannot be true). If the first command succeeds, then you execute the next to see if it also succeeds. If they both do, then the overall construct succeeds.

It's sort of like -- what is the intuitive interface for bezier curves? Those who learned Illustrator first couldn't get comfortable with Freehand, and vice versa. Or page layout -- those who learned Quark first never liked PageMaker, and vice versa.

Since I learned Basic, Fortran, Algol, Pascal, APL, Lisp, JCL, C, PostScript, and so on long before I learned Unix or Perl, my intuition isn't wedded to a particular language. I just check the man page, or the book, or run a trial, and see how it works.

Wait until you start digging into Perl 6. Unicode throughout. The Yen symbol zippers two arrays together. Oh, and it's not backword compatible to Perl 5. You have to change your code.
 
Old 03-24-2009, 12:30 PM   #27
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by choogendyk View Post
Only if your intuition is wedded to another tool.

From a logical perspective, if the first command fails, then you have your answer -- the overall construct has failed (&& cannot be true). If the first command succeeds, then you execute the next to see if it also succeeds. If they both do, then the overall construct succeeds.

It's sort of like -- what is the intuitive interface for bezier curves? Those who learned Illustrator first couldn't get comfortable with Freehand, and vice versa. Or page layout -- those who learned Quark first never liked PageMaker, and vice versa.

Since I learned Basic, Fortran, Algol, Pascal, APL, Lisp, JCL, C, PostScript, and so on long before I learned Unix or Perl, my intuition isn't wedded to a particular language. I just check the man page, or the book, or run a trial, and see how it works.

Wait until you start digging into Perl 6. Unicode throughout. The Yen symbol zippers two arrays together. Oh, and it's not backword compatible to Perl 5. You have to change your code.
I am not getting scared - I'm looking into Perl 6 regularly.

And Algol, Fortran, Pascal, Modula-2, Verilog, some VHDL, Perl, octave/Matlab languages are on my list. As well as some Axiom and GAP, Maxima.
 
Old 03-24-2009, 09:12 PM   #28
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
Quote:
Originally Posted by Sergei Steshenko View Post
I am not getting scared
I wasn't aware we were talking about that sort of thing.
 
Old 03-26-2009, 07:11 PM   #29
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
Another Scripting Question

I want to see who owns more files me or root? I do not want to include directories in my search. I want it to output two lines...root (# of files) myusername (# of Files). What command tells who owns a file? and where is a good place to start?
 
Old 03-26-2009, 07:27 PM   #30
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
man find
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
i need to write a script that automatically logins venix Linux - Newbie 3 03-03-2009 01:34 PM
Strange remote logins after starting a gnome session Clemente Linux - Desktop 2 11-11-2008 02:14 PM
Looking for software to track logins and commands with auto-email DukeLeto Linux - Software 1 07-20-2007 05:14 PM
scripting the unix command script lmcthbe Linux - General 7 05-28-2003 02:49 PM
How to schedule unix script periodically from unix os level??? gopi_20us Programming 2 03-11-2002 06:45 AM

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

All times are GMT -5. The time now is 04:12 PM.

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