1827855 Members
2705 Online
109969 Solutions
New Discussion

functions

 
kamal_15
Regular Advisor

functions

hi all
i want to ask about
how can i use functions like (stat,lstat,..)
in my script?

i writes scripts using sh and korn?
when i type any of this commands in prompt
it return error
sh:lstat : not found

please explain

thankx
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor

Re: functions

Strictly speaking stat() and lstat() are system calls and you cannot use them from the shell. Learn C/C++ then you can use them. Now you create a c program that invokes stat() and outputs to stdout and then you could use that command in a shell script.

A much better solution is to use Perl. If has both the stat() and lstat() functions built-in.

You could even do something like this:
#!/usr/bin/sh
STAT=$(perl -e stat("/tmp"))
echo "STAT = ${STAT}"

but you would really need to fprmat the stat output to be meaningful too you.

The best answer is to learn Perl and do your scripting in it.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: functions

I really should give you a little better example in Perl:
perl -e '@x = stat("/tmp"); foreach $x (@x) {print $x," ";} print "\n";'

This is untested but it should at least give you the fields separated by a space. You should note that the times are epoch seconds so you still have some work to do. Mode would be more meaningful is printed in octal but I 'll leave all that to you.
If it ain't broke, I can fix that.
Rajeev  Shukla
Honored Contributor

Re: functions

Here is how you can use the stat() in C
When you use this function it actually returns you a pointer to a structure "stat" which defines all the parameters of a file, below is a simple example which when compiled and run bu passing a file return the inode and the size of the file


#include
#include
#include

main (argc, argv)
int argc;
char *argv[];
{
struct stat buf;
stat(argv[1], &buf);

printf("The file size are: %ld\n", buf.st_size);
printf("The file inode are: %ld\n", buf.st_ino);

}

Cheers
Rajeev