Sample FOR loop question

This sample script should obtain a list of files in my home directory:

#!/bin/bash
# Manipulate files and copy to my home directory
#
for FILE in $HOME/.bash*
do
cp $FILE ${HOME}/public_html
chmod a+r ${HOME}/public_html/${FILE}
done

Unfortunately it only produces an error:

-bash-3.2$ ./fileloop.sh
chmod: can't access /students/kporte01/public_html//students/kporte01/.bash_history
chmod: can't access /students/kporte01/public_html//students/kporte01/.bash_profile
chmod: can't access /students/kporte01/public_html//students/kporte01/.bashrc

Running the script in debug mode, the result is:

-bash-3.2$ ./fileloop.sh
+ for FILE in '$HOME/.bash*'
+ cp /students/kporte01/.bash_history /students/kporte01/public_html
+ chmod a+r /students/kporte01/public_html//students/kporte01/.bash_history
chmod: can't access /students/kporte01/public_html//students/kporte01/.bash_history
+ for FILE in '$HOME/.bash*'
+ cp /students/kporte01/.bash_profile /students/kporte01/public_html
+ chmod a+r /students/kporte01/public_html//students/kporte01/.bash_profile
chmod: can't access /students/kporte01/public_html//students/kporte01/.bash_profile
+ for FILE in '$HOME/.bash*'
+ cp /students/kporte01/.bashrc /students/kporte01/public_html
+ chmod a+r /students/kporte01/public_html//students/kporte01/.bashrc
chmod: can't access /students/kporte01/public_html//students/kporte01/.bashrc

Hmmm... what did I miss?

try changing this:

chmod a+r ${HOME}/public_html/${FILE}

to this:

chmod a+r ${HOME}/public_html${FILE}

Tried it; still no luck...

You are doing this:

for FILE in $HOME/.bash*

And this:

cp $FILE ${HOME}/public_html

Then shouldn't you be doing this?

chmod a+r ${HOME}/public_html/$(basename $FILE)

For the files to copy, public_html should be a directory. Hence create a directory with the name public_html before executing this script.

#!/bin/bash
for FILE in $HOME/.bash*
do
cp $FILE ${HOME}/public_html
done
chmod 444 ${HOME}/public_html/.bash*

This will copy all the file that start with .bash to the directory public_html.
After executing this script, cd to the public_html directory and execute ls -al. Hopefully this should resolve your issue.