Which is better? C, Perl or Shell

Which is better? C, Perl or Shell

Post by Chacrint Charintho » Fri, 13 Jun 2003 13:39:16



Dear All,

I am about to write a program to detect an existence of files and
remove the associated records from the database. Which programming
provide the best solutions between C, perl and shell.

-- Example from Shell --

prog"$HOME/bin/prog"
file="$HOME/log/file"
record=`echo $file | cut -b 31-34`

if [ -f $file ] ; then
  echo "delete from table where record=$record;" | $prog
else
  echo "record not found"  
fi

-- end --

Thanks in advance,

Chacrint

 
 
 

Which is better? C, Perl or Shell

Post by Marc Rochkin » Fri, 13 Jun 2003 14:27:48




Quote:> Dear All,

> I am about to write a program to detect an existence of files and
> remove the associated records from the database. Which programming
> provide the best solutions between C, perl and shell.

[snip]

he following are general comments, not necessarily tailored to your
specific example.

Use shell only when the program will be very short and when a substantial
part of the task can be handled by existing commands that you can
incorporate into your program. It's a horrible language otherwise.

Perl is a monstrosity that should normally be avoided. One of the worst
languages ever designed, if designed is the proper word for how it came
into being. Use it only when its ubiquity is the principal criterion. It's
way overused and now deserves to enter a long period of steady decline.

C requires lots of work to get running, because the language is tricky and
because its primitives and standard functions are weak (relative to Perl
and shell). But, the programs you write (if you write them reasonably well)
are by far the most efficient of any other high-level language. (OSes,
shells, and other languages are often written in C.)

Why not use Python? It's a language that's deserves to be much better known
and more widely used.

Of course, these are just my opinions, although they happen to be right;
other opinions that say something different happen to be wrong. ;-)

--Marc

 
 
 

Which is better? C, Perl or Shell

Post by Chris Lowt » Fri, 13 Jun 2003 20:36:47





>> Dear All,

>> I am about to write a program to detect an existence of files and
>> remove the associated records from the database. Which programming
>> provide the best solutions between C, perl and shell.

> [snip]

> he following are general comments, not necessarily tailored to your
> specific example.

> Use shell only when the program will be very short and when a substantial
> part of the task can be handled by existing commands that you can
> incorporate into your program. It's a horrible language otherwise.

> Perl is a monstrosity that should normally be avoided. One of the worst
> languages ever designed, if designed is the proper word for how it came
> into being. Use it only when its ubiquity is the principal criterion. It's
> way overused and now deserves to enter a long period of steady decline.

> C requires lots of work to get running, because the language is tricky and
> because its primitives and standard functions are weak (relative to Perl
> and shell). But, the programs you write (if you write them reasonably
> well) are by far the most efficient of any other high-level language.
> (OSes, shells, and other languages are often written in C.)

> Why not use Python? It's a language that's deserves to be much better
> known and more widely used.

> Of course, these are just my opinions, although they happen to be right;
> other opinions that say something different happen to be wrong. ;-)

> --Marc

This is an area where people hold the opinions very strongly!

Here's mine..

shell   - for a "quick fix" where speed and portability arent important an
d where existing tools do the bulk of the work already but need the "glue" t
o stick them together.

perl    - for quick development, particularly when text parsing is a part
of the game. Quicker than shell. Speed comparison with C is hard and depen
ds on the nature of the script. Perl can be quick than badly written
C. Consider perl for GUI applications that must be portable to differe
nt platforms.

C       - If speed is the prime concern, or if portability to platforms wi
thout perl is an issue. C development tends to be slower than perl - so pr
oject delivery times are a factor.

But over all - use what you know and are comfortable if there are deadlines
to meet or if there is an existing system to hook up with. Lean a new
language if not.

Personally - since discovering perl I have written very little "C". I love
perl, but know that others hate it.

Chris
--
Real address: chris at lowth dot sea oh em.
For GPL e-mail anti-virus, On-line IPTables wizzards and help,
web filtering for Smoothwall and IPCop, Security and Linux book
recommendations and more go to -- http://www.lowth.com

 
 
 

Which is better? C, Perl or Shell

Post by Stephane CHAZELA » Fri, 13 Jun 2003 23:13:25


[...]
Quote:> prog"$HOME/bin/prog"
> file="$HOME/log/file"
> record=`echo $file | cut -b 31-34`

> if [ -f $file ] ; then
>   echo "delete from table where record=$record;" | $prog
> else
>   echo "record not found"  
> fi

[...]

Just to help you understand why shells are often bad choices,
some comments on this code that may seem trivial otherwise:

Quote:> record=`echo $file | cut -b 31-34`

It's been so since the first release of Bourne shell around 1975
and maybe before then, but everybody still seems to ignore that
a non-quoted variable reference is a *list* subject to filename
generation.

So, with echo $file, you mean $file is a list of file patterns
that you want to pass separate to the "echo" command.

[ -f $file ] is incorrect as -f expects exactly one argument
(except you ensure $file is a pattern that expands to exactly
one file).
In your example, file in clearly not a list but a string
variable. So, it should be ? echo "$file" ? and [ -f "$file" ].
This may not make any difference in most cases, but may in very
embarrassing ones, and it makes the code harder to decipher and
debug. You should take the habit to always quote a variable
unless you explicitely want the "word splitting" and/or
"filename generation" to occur.

Note that the "echo" command doesn't output its arguments
reliably (it fails for args like "-n" or "-e" in some shells and
for arguments containing backslashes).

From a more generic point of view, echo "$file" | cut -b 31-34
doesn't return characters 31 to 34 of the "file" variable. It
does so in the particular case where the variable doesn't
contain newline characters (it's very very unlikely a $HOME
variable contains a newline character, but it's not strictly
impossible and may be forced to if one has an interest in
breaking your script. Using other languages, you don't have this
kind of problems).

-f doesn't only test for file existence, but also wether file is
of regular type. It silentely fails to report a file exists if
it exists but is not accessible.

... | $prog
as said above, $prog is considered as a list of files. If you
want to consider it as a list of strings (command and
arguments), you have first to disable filename generation
(through the not much portable set -f option).

As prog is $HOME/bin/prog, you may prefer quoting $prog
(| "$prog"). If prog had to take arguments, if would be very
difficult to do it in a reliable way.

prog="$HOME/bin/prog -option"
set -f
IFS=" "
... | $prog

is ok only in the particular case when $HOME doesn't contain
spaces.

So, to be consistant, you'd have to use: ... | eval "$prog"
and ensure $prog has a correct shell syntax:

prog=\'`sed "s/'/'\\\\\\\\''/g" << EOF
$HOME/bin/prog
EOF
`"' -option"

... | eval "$prog"

You could also use the non-POSIX array support extension of
recent shells:

prog=( "$HOME/bin/prog" -option )

Or portably, use a function instead of a variable:

prog() {

Quote:}

... | prog

HTH understanding how difficult it is to write clean shell code.
--
Stphane

 
 
 

Which is better? C, Perl or Shell

Post by Chuck Dillo » Fri, 13 Jun 2003 23:34:46



> Dear All,

> I am about to write a program to detect an existence of files and
> remove the associated records from the database. Which programming
> provide the best solutions between C, perl and shell.

> -- Example from Shell --

> prog"$HOME/bin/prog"
> file="$HOME/log/file"
> record=`echo $file | cut -b 31-34`

> if [ -f $file ] ; then
>   echo "delete from table where record=$record;" | $prog
> else
>   echo "record not found"  
> fi

> -- end --

> Thanks in advance,

> Chacrint

If this is all you need a single pipeline is all you need.  Look at the
man pages for 'find' and 'xargs' and you will be able to do it in one
pipeline.
        find $HOME/log -type f | xargs -n 1 basename | xargs printf "delete
from table where record=%s\n" | $HOME/bin/prog

Of course it would be easier if you $prog operated on commandline
arguments.

Or you could write a C function, put in in a shared library, use SWIG
to wrap it in a perl module, call it from perl and call perl from an sh
script. ;-)

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

 
 
 

Which is better? C, Perl or Shell

Post by Marc Rochkin » Sat, 14 Jun 2003 03:48:24




[snip]

Quote:

> Just to help you understand why shells are often bad choices,
> some comments on this code that may seem trivial otherwise:

[snip]

Quote:

> HTH understanding how difficult it is to write clean shell code.

Outstanding post... thanks!

--Marc

 
 
 

Which is better? C, Perl or Shell

Post by David Schwart » Sat, 14 Jun 2003 19:36:40



Quote:> Outstanding post... thanks!

    It should also be required reading for anyone who asks how to make a
shell script setuid root.

    DS

 
 
 

Which is better? C, Perl or Shell

Post by Kevin D. Cla » Sun, 15 Jun 2003 01:18:56



> Perl is a monstrosity that should normally be avoided. One of the
> worst languages ever designed, if designed is the proper word for how
> it came into being. Use it only when its ubiquity is the principal
> criterion. It's way overused and now deserves to enter a long period
> of steady decline.

I would encourage the original poster to ignore this advice.  Perl is
a beautiful language designed to let you get work done quickly,
reliably, and neatly.

Given that the problem that the original poster is trying to solve
involves file operations and database access, Perl would would
probably be the first tool that I'd reach for.

Regards,

--kevin
--
Kevin D. Clark / Cetacean Networks / Portsmouth, N.H. (USA)
cetaceannetworks.com!kclark (GnuPG ID: B280F24E)
alumni.unh.edu!kdc

 
 
 

Which is better? C, Perl or Shell

Post by one2001.. » Thu, 10 Jul 2003 16:18:32



> Dear All,

> I am about to write a program to detect an existence of files and
> remove the associated records from the database. Which programming
> provide the best solutions between C, perl and shell.

how about C interpreter with shell programming features?

http://www.softintegration.com/docs/ch/shell/

Quote:

> -- Example from Shell --

> prog"$HOME/bin/prog"
> file="$HOME/log/file"
> record=`echo $file | cut -b 31-34`

> if [ -f $file ] ; then
>   echo "delete from table where record=$record;" | $prog
> else
>   echo "record not found"
> fi

> -- end --

> Thanks in advance,

> Chacrint