Pls set -vx options in main script, and post the entire log (eventually as an attachment). Without the context, errors are very difficult to track down.
And, pls remove the # in front of the last fi
Here's the complete version of your copyfiles function, so you don't need to fumble around with half baked solutions:
copyFiles() {
cd $4$user
old=_old
CNT=$(ls|wc -w)
if [[ "$CNT"-gt "0" ]]; then
echo "file for $4$user already exists!"
echo "Do you want to overwrite the file (y/n):"
read answer
if [ "${answer^?}" == "N" ]; then
mv $4$user $user$old
mkdir /$4/$user > /dev/null
fi
for i in $(cat $2)
do
cp $3$i $4$user
done
chmod 700 $4$user
fi
}
Here's a complete version of your copyfiles function as there are still more errors in the version you sent:
copyFiles() {
cd $4$user
old=_old
CNT=$(ls|wc -w)
if [[ "$CNT" -gt "0" ]]; then
echo "file for $4$user already exists!"
echo "Do you want to overwrite the file (y/n):"
read answer
if [ "${answer^?}" == "N" ]; then
mv $4$user $user$old
mkdir /$4/$user > /dev/null
fi
fi
for i in $(cat $2)
do
cp $3$i $4$user
done
chmod 700 $4$user
}
still one problem, if selected not to overwrite, it doesnt create user_old folder but overwrites existing files.
Do you want to overwrite the file (y/n):
+ read answer
n
+ '[' N == N ']'
+ mv /neha neha_old
mv: cannot move `/neha' to a subdirectory of itself, `neha_old'
+ mkdir ///neha
mkdir: cannot create directory `///neha': File exists
cat $2)
cat $2
++ cat /filelist
+ for i in '$(cat $2)'
+ cp /files/file1 /neha
+ for i in '$(cat $2)'
+ cp /files/file2 /neha
+ chmod 700 /neha
mv /neha neha_old
mv: cannot move `/neha' to a subdirectory of itself, `neha_old'
mkdir ///neha
mkdir: cannot create directory `///neha': File exists
You need to design a dir structure that won't stand in its own light. And - use the parameters correctly. $4 here seems to contain the root dir. Then, use it consistently everywhere.
BTW - you had $4$user$old in your post #1...
yes! yes! that was the last problem now code is working as it was designed for. Would you believe i have to submit this code tomorrow as my sem1 assignemnt! phew! what a close call!
For homework, the solution might be okay.
In the real world, you would backup individual files to homedir/file_old, rather than dealing with a homedir_old