Click to See Complete Forum and Search --> : Conditional IF help needed
Chess007
11-14-2010, 07:57 PM
Hi,
Thanks for all of your help in the past. I've been using Linux for a while and am learning as I go. I am currently writing a script to backup the users $HOME folder to a folder called /mnt/backup. This script will run daily. I have most of the script written but I have a few questions. I want to use an IF THEN ELSE statement. Here's the pesudocode and some real code:
IF /mnt/backup does not exist
THEN
cd /mnt
mkdir backup
ELSE echo "As the folder /mnt/backup exists, your home directory will be copied to it" >> ~/read_me.txt
fi
rsync -av $HOME /mnt/backup/
*the purpose of that rsync command is to copy the users $HOME folder to /mnt/backup
Questions:
1. How do I test to see if a directory exists?
2. Can a bash script run commands (such as the cd and mkdir commands in my example)?
3. What will rsync do if a file is in use? Can files in use be copied?
I've attached a png image of the Bash code, in case it does not show up here correctly?
Pafnoutios
11-15-2010, 03:43 AM
1. How do I test to see if a directory exists?
[CODE]if [ -d /mnt/backup ][\code] Check "man test".
2. Can a bash script run commands (such as the cd and mkdir commands in my example)?
Yes, that's the point. I have a daily backup script calling tar. My /mnt/backup is on a separate drive, so I verify that it is mounted by looking for the file /mnt/backup/ismounted which I generated.
3. What will rsync do if a file is in use? Can files in use be copied?
I think it will still be copied. I've recently editted bash scripts while they were still running. The running instance didn't change, but it was already modified to rerun with "fixes" as soon as it stopped.
ph34r
11-15-2010, 09:13 AM
smells like homework...
Pafnoutios
11-15-2010, 02:30 PM
smells like homework...
Wow. I can usually spot those.
<edit>I see it now.</edit>
Chess007
11-15-2010, 09:51 PM
Thanks for your help. As we are encouraged to do research for our project that is what I have been doing. Of course I will cite all my sources; as I do with all my college research projects.
# ! /bin/bash
# This is a script which gives the user information about
# his/her use of their account and computing resources.
#Said information is put into a text file, "read_me.txt" in
#the users home directory. The home directory is then backed up.
# The following is the first line in the text file.
echo "The following are some important details about your account." >> ~/read_me.txt
echo "The process using the most computing resources is..." >> ~/read_me.txt
top >> ~/read_me.txt
echo "The free and used hard drive space in your home directory is..." >> ~/read_me.txt
ls -l /home/ >> ~/read_me.txt
echo "The free swap space you have is..." >> ~/read_me.txt
free >> ~/read_me.txt
echo "If you have any concerns about this information, please contact the systems administrator" >> ~/read_me.txt
echo "Now your home directory will be backed up to /mnt/backup" >> ~/read_me.txt
#To get a list of home directories
HOMES=`grep /home /etc/password | cut -f6 -d' : '`
# Backup the data in those directories to the /mnt/backups/ partition
--> this is where the conditional IF would come in
After that, one more echo'd status message and its done.
From research I have changed my script a lot. Originally, I was going to do a network backup, then I considered a simple cp, but moved on to rsync as it seems a more logical choice. I am not asking for anyone to do anything for me. This is my first script so it is likely to be simplistic. I am simply trying to learn. Thanks again.
Edit 1:
Here's the conditional if then else:
if [ -d /mnt/backup ]
then
echo "As /mnt/backup exists, your home folder will be backed up to it" >> ~/read_me.txt
else
mkdir /mnt/backup
fi
That tests to see if mnt/backup exists. If it does, then the logic moves to echo and that text is output to a file. However, if mnt/backup does not exist, then it is created. :) Hope this helps someone.