Here is a handy little script I have been using for years. Not sure who
wrote it or I would give them due credit, not really complicated but had no
shell scripting experience when I first started using it. (why reinvent the
wheel?)

I call it 'jic' (just in case) and place it in /usr/local/bin. Then when I
want to modify a file, let's say /etc/passwd, simply do a 'jic /etc/passwd'
and it creates a backup with the time stamp. This version only does the
month, day, and year but easily changed to do more. You can also easliy
modify it to place the backup file in a subdirectory as you mentioned.

###START (do not put this line in the script)
#!/bin/sh

case "$#" in
1)
  if [ ! -f "$1" ]; then
     echo "error: No such file $1"
     exit
  else
     FILE="$1"
     FILE_BUP="$FILE.`date +%m%d%y`"

     if [ -f $FILE_BUP ]; then
        echo "error: $FILE_BUP exists, abort"
        exit
     fi
  fi
;;

*) 
  echo "\nerror: usage: $0 filename\n"
  exit
;;
esac

/bin/cp $FILE $FILE_BUP

exit
###END




-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Monday, January 29, 2001 7:39 PM
To: [EMAIL PROTECTED]
Subject: [OT] Checking for the existance of a file in Bash scripts


Howdy,

I just wrote a bash script for making manual backups easier (I do them
manually when I'm about to edit something major) it basically makes a new
directory in /backup/ who's name is equal to today's date, then recursively
copies certain dirs I want backed up.

The problem is, on some projects I like to do the manual backup each time I
get to a stable point before making the next few edits. What I'm wanting is
for the script to check and see if a directory with today's date already
exists. If it does I want it to append the hour as well as the date. Then if
there's already on with the same hour it should append the minute.

Another option would be for it to add something like _1 to the end and
increment _1 by one digit each time (_2, _3, _4....) but I like the day,
then hour, then minute better. I guess we could add seconds for amusement's
sake.

Anyway, I'm sure I could figure out how to do an if...else loop, I've seen
examples, but in bash I've never seen an example of checking for the
existence of a file or dir. Can anyone show me how to do that?

Here's what I have:

#!/bin/bash
#
#First find out what today is
#
#
#oldformat: today=$(date +%Y_%m_%d)
#
#new format:
today=$(date +%m-%d-%Y)
echo "Today is" $today
mkdir /backup/$today && echo "backup dir $today created"
cp -a /etc /backup/$today && echo "etc copied"
echo
echo "Done"


Thanks in advance...

----------------------------------------------------
Jonathan Wilson
System Administrator

Cedar Creek Software
http://www.cedarcreeksoftware.com

Central Texas IT
http://www.centraltexasit.com



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to