Computing.Net > Forums > Linux > Shell script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Shell script

Reply to Message Icon

Name: Mark
Date: January 10, 2002 at 14:52:19 Pacific
Comment:

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 ]
then

if [ -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
done

do

#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"
fi

else
cp $1/$filename $2/$filename
echo "$1/$filename was backed up"
fi
done

echo " "


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
done

echo " "
else

echo "Path 1 is not a directory"
fi
else

echo "Path 2 is not a directory"

fi


Any help would be much appreciated. Thanks!!



Sponsored Link
Ads by Google

Response Number 1
Name: Guy
Date: January 10, 2002 at 19:12:01 Pacific
Reply:

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


0

Response Number 2
Name: Mark
Date: January 11, 2002 at 13:47:44 Pacific
Reply:

Thanks. I'll give it a go.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: Shell script

Shell Script Question www.computing.net/answers/linux/shell-script-question/15096.html

Using Mailto in shell scripts www.computing.net/answers/linux/using-mailto-in-shell-scripts/27632.html

Bourne Shell Script Problem www.computing.net/answers/linux/bourne-shell-script-problem/19777.html