Date: Thu, 02 Mar 2000 23:51:28 -0800 From: Doug Barton <Doug@gorean.org> To: Joe Park <joepark@uclink4.berkeley.edu> Cc: freebsd-questions@FreeBSD.ORG Subject: Re: very basic scripting questions Message-ID: <38BF6F00.84E6DD15@gorean.org> References: <4.2.0.58.20000302214315.00c0e100@uclink4.berkeley.edu>
index | next in thread | previous in thread | raw e-mail
Joe Park wrote:
>
> Hello,
>
> I'm a newbie and I would like to ask you guys about scripting. I need to
> write a script that does chmod functions on files/directories listed at
> separate file. (it doesn't have to be a argument. the script already knows
> the name of the file.) For example, let say there is a file list_files.txt
>
> list_files.txt :
> 750 /usr/src/tmp
> 750 /usr/src/lib
>
> How do I feed each line to chmod? (maybe with cat?) How about if I need
> to do some filtering, using regular expression?
>
> I'm sorry to bother you guys with such a simple question. Where can I find
> more books about unix scripting? I just bought "mastering regular
> expression" from O'Reilley. Is this a right choice?
There is no such thing as "unix scripting." There are many different
scripting languages that run in a unix environment. In fact, you could
make a semantic argument that "scripting" is the wrong word altogether.
What you're really doing is programming.
Ok, now for some really useful information. What you want to learn
depends on what you need to do. If you need to do a lot of routine tasks
like this (especially tasks that involve file processing) you want to
learn perl. "Learning Perl" and after you've mastered that "Programming
Perl" from O'Reilly are your friends. If you will be doing a lot of
general system administration tasks or need to work across a number of
different platforms you should learn Bourne shell. The "Learning Bash"
book from O'Reilly is also really good.
Now for your actual problem. This can be done with a simple perl
script. Please refer to the above resources and "man perl" for more
details. I'm sure someone will have comments about my code...
Good luck,
Doug
--
"Welcome to the desert of the real."
- Laurence Fishburne as Morpheus, "The Matrix"
#!/usr/bin/perl -w
# Open the file to read the info
# Print an informative error message if this doesn't work
open(LIST, "list_files.txt") or die "Can't open list_files.txt for read:
$!";
# Loop through each line of the file
while (<LIST>)
{
# gnaw off the newline at the end of the line
chomp;
# Set the variables
my($mode, $filename) = (/(\S+)\s+(\S+)/);
# Set the modes. Perl chmod() is a builtin which calls the
# Unix internal.
# But the chmod commands needs a 0 prepended
# And its mode in octal format
$mode = "0$mode" if (length($mode) == 3);
# Always check your return codes
if ((chmod oct($mode), $filename) == 1)
{
print "Okey dokey, set $mode on $filename\n";
} else {
print "Whoah! Couldn't set the mode on $filename\n";
}
}
# We don't really need this since we're just going to exit next
# but might as well learn good habits...
close LIST;
exit 0;
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message
help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?38BF6F00.84E6DD15>
