efficiency..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting efficiency..
# 1  
Old 04-27-2004
efficiency..

how efficient is it, and how practical is it to call outside programs in a shell script (bash) for small tasks?

for example, say i have a script that might preform many tasks, one of those tasks may require root access; rather than implementing inside the script a method to use su or sudo to preform the action with root privelages, i write a separate script which ONLY takes the action to be preformed as an argument, and the root password as an argument.

now of course i realize that this might be unnecessary. but id like to know others' opinions.
# 2  
Old 04-27-2004
Invoking external programs is a slow process. How big the penalty is depends on how often it is done. Inside a loop, the price can really add up.

But shells can't do everything, sometimes you must use an external programs. So my advice is: use external programs when you must and only when you must.
# 3  
Old 04-27-2004
i got it Smilie . use OO concepts only with OO languages Smilie
# 4  
Old 04-27-2004
IMHO since you would be doing shell script i dont think the penelty would be super if you used os commands. i mean thats kinda what its purpose is if you ask me.

take perl for example (or other higher language programs) they will incorporate alot of the os functionality into the language so teh program dosent have to use external programs to do its job.

there was a snipit of code i think RTM posted last week or so in the perl thread where he defined a variable by calling the shell to do something perl is capable of doing internally.

but ask Per said it is best to keep it to a absolute minimum.

i wrote some perl scripts a while back on purpose i called to the shell to see the time differance when working with files and i saved nearly 10 sec by not calling to the shell and doing the same process useing perl functions and modules.
# 5  
Old 04-28-2004
Must have been this:
$userinfo=`/usr/bin/grep $id /etc/passwd`;

So how do you do it in Perl ? I haven't done that much in Perl and found it rather weird that it always seemed that you had to use UNIX commands to get info - I have Perl scripts that I could have done in ksh and only kept in Perl for a bit of understanding of it.
# 6  
Old 04-28-2004
Quote:
Originally posted by RTM
Must have been this:
$userinfo=`/usr/bin/grep $id /etc/passwd`;
There may be a problem there. Suppose that id is set to "rtm". If there are other users like xrtm or rtm2, you will return multiple lines. Of course, since I didn't see the original code, this could be what you want.

$userinfo=`/usr/bin/grep "^${id}:" /etc/passwd`;

might be a solution. I'm not sure if perl's backticks involve launching a shell or not.
# 7  
Old 04-28-2004
In perl it would involve many more lines of code, and you would start by using open(), traverse through the file and assign the pattern match $_=~/xxx/ to a variable.

Much easier from a coding perspective to just open a shell, but depending on the task, it may affect performance.

Code:
A string enclosed by backticks (grave accents) first undergoes variable interpolation
 just like a double-quoted string. The result of that is then interpreted as a command by the shell,

Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

About efficiency of parallel memory allocation

Hello, there. I'm a new beginner to Linux kernel and curious about its memory management. When multiple applications apply for memory space at the same time, how Linux kernel solve the resource contending problem for high performance? I have known that there is a buddy system for allocating and... (4 Replies)
Discussion started by: blackwall
4 Replies

2. Shell Programming and Scripting

File or Folder Efficiency?

I've got this program set up so that it creates files whose unique names specify the jobs their contents describe. In order to retrieve the information inside those files, I have to do a "grep" and awk or sed to extract it. I've just assumed that making a directory with that unique name that... (1 Reply)
Discussion started by: gmark99
1 Replies

3. Shell Programming and Scripting

The efficiency between GREP and SED???

Hello Everyone! I am a newbie. I'd like to get key lines from a big txt file by Reg Exp, The file is nearly 22MB. GREP or SED?which may be the best choice,more efficient way? or any other best practise? Thank you in advance. Ever:) (5 Replies)
Discussion started by: ever
5 Replies

4. Shell Programming and Scripting

Improve program efficiency (awk)

Hi !! I've finished an awk exercise. Here it is: #!/bin/bash function calcula { # Imprimimos el mayor tamaño de fichero ls -l $1 | awk ' BEGIN { max = $5; # Inicializamos la variable que nos guardará el máximo con el tamaño del primer archivo } { if ($5 > max){ #... (8 Replies)
Discussion started by: Phass
8 Replies

5. Shell Programming and Scripting

Perl: code efficiency for gmtime

I have the following Perl snippet: # get datetime @dt = gmtime(); $strdate = 1900 + $dt . addleadingzero(++$dt) . addleadingzero($dt) . addleadingzero($dt) . addleadingzero($dt) . addleadingzero($dt); # write to file $outfile = $strdate . ".txt"; getstore($url, $outfile) or die "Error:... (3 Replies)
Discussion started by: figaro
3 Replies
Login or Register to Ask a Question