On 29-Oct-98 Nagle, Adrian wrote:
> If there is a more appropriate place to ask about bash, please let me know.
> 
> I'm starting to learn how to use bash.  For a start, I want to write a
> script that copies some .html files from /floppy to a ~/public_html
> directory, change to permissions, user, and groups appropriately and delete
> the files in /floppy after this is done.

Quite a bit of errors here. I'll try to go through them one at a time.

> Part of my attempt:
> cd /floppy
> 
> if [-e /floppy/*.html]; then

You'll need to leave a space between the square brackets and it's content.

        if [ -e /floppy/*.html ]; then

...but this still may not work. Bash will expand "/floppy/*.html" such that if
you have the files "/floppy/file1.html" and "/floppy/file2.html", the result of
the expansion would be equivalent to:

        if [ -e /floppy/file1.html /floppy/file2.html ]; then

Since the -e option only accepts one argument, you will have an error. Try
using "ls" instead.

        if ls /floppy/*.html > /dev/null 2>&1; then

As the "ls" command will output all matching filenames to standard output, we
use the "> /dev/null" directive to direct all output to the null device.

However, when no files are found "ls" will output an error message to standard
error. To handle this, we use the directive "2>&1" to redirect standard error
to standard output (2 is the file descriptor of standard error and 1 is the
file descriptor of standard output). As a result, both standard error and
standard output will be dumped into the null device when standard output is
directed into it.

Note, the directive "2>&1" is sensitive to spacing as well as order. It will
give an error is spaces are placed within it, and will give a different result
if it is placed before "> /dev/null".

>   for filename in $*

The proper syntax should be:

        for filename in $*; do

This is the source of the "token" error.

Another problem. "$*" will be expanded to the parameters given when you run the
script. Example, if your script is named "myscript" and you run it as "myscript
file1.html file2.html", then "$*" will expand to "file1.html file2.html".

If you want to obtain the filename of all the files in the current directory,
you should use:

        for filename in *; do

>     fromdos < $filename > $HOME/public_html/$filename
>     chown anagle $HOME/public_html/$filename
>     chgrp users $HOME/public_html/$filename
>     chmod 644 $HOME/public_html/$filename
>     rm $filename

Shouldn't be any problems here, but you may find that quoting the variables may
help avoid problems during encounters with filenames that contains special
characters.

Example:
        fromdos < "$filename" > "$HOME/public_html/$filename"

>   done
> else
>   echo "No HTML files are located in $PWD./nNothing done."

You will need to give "echo" the "-e" option if you want it to interpret the
special characters. Also, the newline character is represented as "\n" and not
"/n".

        echo -e "No HTML files are located in $PWD.\nNothing done."

> fi

That should be it. The script still would not work perfectly under _all_
circumstances, but it should be sufficient. It's seldom practical or necessary
to deal with every possible source of errors when working with scripts that are
not meant for public distribution.

There are more than one way of achieving the intended result, and the one I've
presented may not be the best. Try experimenting a little.

If anyone spotted any errors, please feel free to post them.

Resources:
  Man pages
    bash
    echo

Cort
[EMAIL PROTECTED]

Reply via email to