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.
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.
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.
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.
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.
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.
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,
you can prolly slim it down to (getpwnam("$_"))[0,2,7];
but generally:
You have a couple of ways of importing the data. (provided perl dosent have a built in function to read the file as in this example) One of the most popular is loading the data into a control structure (array/hash/combo) or working right off the file handle.
so let say you have x file and you need to work with it. you can do something like this (from the perl cookbook)
This is from the perl cookbook. ties a file to an array.
use Tie::File;
use Fcntl;
tie(@lines, Tie::File, $FILE, mode => O_RDWR) or die "Cannot tie file $FILE: $!\n";
OR you can do something like this. not from the cook book.
open(FILEHANDLE,"filename") or die;
while (<FILEHANDLE>) {do something};
RTM did that help at all? if not toss me some stuff and i will try to explane it better. i am rather fortunet and have a variety of perl books on hand along w/ the latest cd bookself so i should be able to answer a fair amount for ya.
Optimus P - Cool. Thanks for the info. I think I have used something like it once, but didn't recall it. I'll try to remember for next time (actually doing any scripts has been so few and far between for the last 2 years it's hard to remember how to do much of anything any more).
Perderabo - fyi - the person was looking for a uid not account name ( see post )
but in the context of your search patter the user would not be entering in the first 3 fields of the password filed they woudl only be entering in a username i believe.
no matter tho we are getting way off the topic of efficiency