Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi
I'm new to shell scripts, so I'm hoping someone can help me with the following problem.
I'm trying to write a shell script (bash) using Red Hat 7.1. I'm trying to get the script to look at each file in one directory (Dir1) and, if the file does not exist in in the second directory (Dir2) or the version in Dir1 is different/older than in Dir1, copies the file from Dir1 to Dir2.
I use the following command- (name of script is shellscript)
bash shellscript Dir1 Dir2
This is the program I have done so far, but I'm getting a few errors.
#list name of script
echo $0
echo " "#check if the directories exists
if [ -d $2 ]
thenif [ -d $1 ]
then
#change prompt to first directory listed
cd $1#list name of first directory
echo "$1"
echo " "#iterate through all files in the first directory
for filename in `ls`do
#print each file in the directory $1
echo $filename
donedo
#check if each file in first directory is in the second directory
if [ -e $2/$filename ]
then#check to see if the file in first directory is newer than the one in the second directory
if [ $1/$filename -nt $2/$filename ]
then
#if file newer, copy it to second directory
cp $1/$filename $2/$filename#name files that were backed up
echo "$1/$filename was backed up"
fielse
cp $1/$filename $2/$filename
echo "$1/$filename was backed up"
fi
doneecho " "
cd $2
echo "$2"
echo " "for filename in `ls`
do
if [ -e $1/$filename ]
then
if [ $1/$filename -nt $2/$filename ]
then
cp $2/$filename $1/$filename
echo "$2/$filename was backed up"
fi
else
cp $2/$filename $1/$filename
echo "$1/$filename was backed up"
fi
doneecho " "
elseecho "Path 1 is not a directory"
fi
elseecho "Path 2 is not a directory"
fi
Any help would be much appreciated. Thanks!!

Mark - Well, I'll pass along a trick you can use to get a lot more information about what is happening in your scripts.
1) As the 1st command in your script put:
set -x
2) As the last command in your script put:
set +x
3) Execute your script like this example:
./myscript 2>&1 | tee myscript.log
When it is all done, edit myscript.log for details about what the script is doing.HTH, Guy

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |