General discussion

Locked

sys utilities written in Perl versus shell scripts, which is better?

By DanLM ·
I've been putting together some system scripts for where I'm working right now on an OS X 10 system. They were specific in the request, but I coded them in a way to allow them to be general purpose. In other words, I wrote them to use config files.
Examples of these scripts:
- Shell Script - Monitor script to watch 3 java process's to insure that they are always running, and if found not to be. Will launch them automatically. No pid numbers are used, just the name that appears in the ps -x. This was written for a specific application, but if you pull in the correct config file this script can monitor anything.
- Perl Script - Backup script that backups up directories to a zip file, and only retains so many of these backups. Deleting the oldest backup when the retention limit is reached. This really could be used for any directory, with any number of retention copies defined.
IE: cvsrespsrc-daily-20061024.zip keep 5 copies
cvsrespsrc-monthly-20061024.zip keep 10 copies.

I have everything running from corntab. In my eyes, the Perl script was cleaner. Lately, I have done everything in shell script because it was more universal. At least, I thought so. But, I'm sitting here thinking about this. Perl offers me so much more flexibility and is universal also.

Is there a reason to go with one and not the other? After my backup script, I'm almost tempted to use Perl for everything again. I can't think of any strength's that one really has over the other. Other then Perl is much more flexible. Is shell scripts less overhead because you are using os utilities for almost everything?

Dan

This conversation is currently closed to new comments.

10 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Shell scripts would be recognized by more people.

by stress junkie In reply to sys utilities written in ...

If you are thinking about writing scripts for a business then you should think about people that will come after you to support the systems. These people will almost certainly know shell commands but may not know Perl. So Perl is not so universal as you might think. It may be available on many systems but that doesn't mean that everyone knows how to program in it.

Collapse -

Perl is very C like and you can execute command line commands too

by jmgarvin In reply to Shell scripts would be re ...

So writing simple shell scripts should be quite readable...Now if you do funny like:
$foo =~ s/.*(\/)

Quite unreadable unless you know perl ;-)

Collapse -

Hah! C is the reason that I'm not a programmer.

by stress junkie In reply to Perl is very C like and y ...

I could never get that darned address book program right when I wrote it in C. You know. The one that you write just after you get "Hello world" going. Memory leaks, lost records, pointers going every which way. Ugh!

I started my career in a very small shop where I did everything. I wrote applications in Pascal, dBase 2, DEC Datatrieve, and DEC RDB. I also performed all of the system administrator's jobs. When it was time to start looking for my second job everybody wanted a C programmer. I just never got the hang of it.

I can do okay with the C shell in Unix. I've written some simple scripts. I couldn't realistically think of writing a complicated compiled application in C when most of my problems would be from C syntax. Try to find a logic problem when the code is a mystery. Forget it.

I sincerely hope that Kernigan and Ritchie suffer eternal torment in the hereafter.

The up side is that I love system administration.

---

Later that day ...

It occurred to me that you meant the C shell. Well, you can see how talking about C gets me all flustered.

Collapse -

I use regex as much as possible

by DanLM In reply to sys utilities written in ...

With perl, it isn't as errrrr. Hard to understand I guese, like it would be in perl. sh script: ps -x | grep processname | awk

But, your right. Most people will reconize more commands that are being done with shell scripts. They might have some issues with the variables and such, but I guese they could figure them out.

perl just lends itself so often to system applications though, even if they could be done in shell scripts.

eh, I think Ill just look at the complexity and decide from there.

Thanks for the comments, much appriceated.

Dan

Collapse -

In perl it would basically look the same

by jmgarvin In reply to I use regex as much as po ...

!#/usr/bin/perl
'ps -x | grep foo | awk'

That's it...

Collapse -

I wish I knew what the two of you were doing.

by apotheon In reply to In perl it would basicall ...

1. As far as I'm aware, ps doesn't have any -x option (though it does have an x option). Do you mean ps x or are you talking about something else that escapes my understanding just now?

2. Why are the results of the grepped ps output being piped through a bare awk command? I mean, what's the point?

By the way, you transposed two characters in the shebang line, jmgarvin -- it should be #!, not !#.

Collapse -

The ps -x is mostly BSD

by DanLM In reply to I wish I knew what the tw ...

I'm use to using it on BSD systems.
Actually apotheon, those were just bogus examples with regard to the ps -x | awk. I more often pipe grep's into awk to pull out specific columns, then perform mathematics. Also, awk gives you the ability to do simple array work if your splicing stuff out. Say you awk with an separator character of ~(config file field separator that I use a lot), then you want to split apart a value within that. Here is an actual value of me checking if a value was passed with a flag. Flag as passed, -b. But this flag can appear as -b or -b<any number>
[pre]#
# Check to see if a threshold value was passed with the switch.
#
TmpThreashold=`echo $Passed_values | awk \
' {if (length($1) > 2) {
Threashold=substr($1,3,length($1))
print Threashold
}
}'`[/pre]
That is from a firewall script that I did that parses through the auth.log. That is also a very good example of a script that I should have wrote in Perl using regular expressions.

That was my question, which is both more efficient and cleaner. shell scripts or Perl? I'm definitely leaning toward always using Perl anymore. Other then one liner shell scripts with piping, Perl just seems more versatile and cleaner.

Dan

Collapse -

HA! No wonder my scripts don't work ;-)

by jmgarvin In reply to I wish I knew what the tw ...

1) Mostly Unix systems use this. In Linux it would be ps x

2) I donno, just doing the same code in perl

As for the shebang, I have to say I should have run it first ;-)

Collapse -

choosing betwixt the twain

by apotheon In reply to sys utilities written in ...

Really, you should just use whichever one suits the task at hand and your own preferences at any given moment, unless you're in a work environment that specifically favors one over the other for some reason.

In the general sense, shell scripts are pretty universal: every 'nix system can run a shell script. On the other hand, different systems default to a different shell (for instance, almost every Linux system in the world defaults to bash, while almost every *BSD in the world defaults to csh or tcsh. Each proprietary UNIX vendor chooses its favorite shell as well. As your shell scripts start involving stuff other than simply saving calls to external utilities in a text file, they become less and less universal -- besides which, the shebang line specifies the shell you're using.

Perl, meanwhile, is basically the same on every system where Perl is installed, unless someone hasn't updated his Perl version since 1995 or something like that.

Some tasks are already accomplished easily through the use of a short pipeline containing a couple of system utilities, while others need to be constructed almost from scratch -- in the former case, a shell script is usually more appropriate, and in the latter, a Perl script is generally better.

I use both. It would probably behoove you to do much the same, really. Pick a language -- shell or Perl -- based on the task at hand, and make your job as easy as possible. Of course, Perl's more fun, so I tend to lean more toward using Perl when the options seem about equivalent otherwise.

Collapse -

The one that works

by bigbigboss In reply to sys utilities written in ...

The one that works in all situations is the best.

Back to Software Forum
10 total posts (Page 1 of 1)  

Related Discussions

Related Forums