Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 1st, 2007, 1:46 PM   #1
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
Unhappy Bash Script Deletion help

I want to change some features on a particular code. This code must delete files that haven't been used in the directory.

Here's my code that I saw an example of:

#!/bin/sh

#< Delete an entry from the address book

_ADDRESS_BOOK=$HOME/a_book/a_book
_TEMP_FILE=$HOME/tmp/a_book.tmp.$$
_ENTIRE_FILE=$HOME/tmp/a_full.tmp.$$

# As a : appears in every line, this has the effect
# of numbering the lines in the fashion we want.
grep -in ":" ${_ADDRESS_BOOK} > ${_ENTIRE_FILE}

echo "Enter a searchterm for your deletion:"
read _SEARCHTERM

grep -in "${_SEARCHTERM}" ${_ADDRESS_BOOK} > ${_TEMP_FILE}

_count=$(wc -l < ${_TEMP_FILE})
echo "${_count} match(es) found!"

while read line
do
  _record_number=$(echo $line | awk -F: '{print $1}')
  _records="${_records} ${_record_number}"
done < ${_TEMP_FILE}

echo ""

for record in ${_records}
do
   _current_rec=$(grep "^${record}:" ${_TEMP_FILE})
   _firstname=$(echo ${_current_rec} | awk -F: '{print $2}')
   _surname=$(echo ${_current_rec} | awk -F: '{print $3}')
   _address1=$(echo ${_current_rec} | awk -F: '{print $4}')
   echo "===================================================="
   echo "${_firstname} ${_surname} of"
   echo "${_address1}"
   echo "DO YOU WANT TO DELETE THIS RECORD? [y/n]"
   read response
   case $response in
     [yY]|[yY][eE][sS])  
	grep -v "^$record:" ${_ENTIRE_FILE} > ${_ENTIRE_FILE}_v
    cp -f ${_ENTIRE_FILE}_v ${_ENTIRE_FILE}
	cut -d':' -f 2-11 ${_ENTIRE_FILE}_v > ${_ADDRESS_BOOK}
	echo "ENTRY DELETED!"
	;;
     *) ;;
   esac
done

rm -rf ${_TEMP_FILE}
rm -rf ${_ENTIRE_FILE}
rm -rf ${_ENTIRE_FILE}_v

Here's another example:

#!/bin/bash
#< Script to remove old quarantine and virus mails, plus cocoon logs from web/mail servers

ECHO="/bin/echo"
FIND="/usr/bin/find"
RM="/bin/rm"
SLEEP="/usr/bin/sleep"
TR="/usr/bin/tr"
XARGS="/usr/bin/xargs"

TARGET[0]="/www/webapps/cocoon/WEB-INF/logs"
TARGET[1]="/var/amavis/quarantine"
TARGET[2]="/var/amavis/virusmails"

${ECHO} "WARNING: This script will remove all files in the following directories"
${ECHO} "$( ${ECHO} ${TARGET[@]} | ${TR} ' ' '\n' )"
${ECHO} "that haven't been modified for more than 7 days"
${ECHO} "Sleeping for 5 seconds - CTRL-C if you need to!"
${SLEEP} 5

for DIRECTORY in ${TARGET[@]}; do
   ${ECHO} "--> Performing prune in ${DIRECTORY}"
   ${FIND} ${DIRECTORY} -type f -mtime +7 | ${XARGS} ${RM} 2>/dev/null
   ${ECHO} "--> Prune in ${DIRECTORY} complete"
done

exit 0
acwbrat is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash File Rename and Deletion acwbrat Bash / Shell Scripting 2 Oct 31st, 2007 3:27 PM
how do I call a bash script in C? smile_sunshine C 8 Apr 30th, 2006 9:36 PM
Window Re-sizing in bash script. Huck Bash / Shell Scripting 1 May 1st, 2005 5:31 PM
Bash Script Help pelon Bash / Shell Scripting 2 Feb 28th, 2005 3:58 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC