Hello Friends
I am having trouble with my script below. I will describe the problems below the code box. I am hoping that some of the experts here can help me.
#!/bin/bash
#=========================================================================================================
# Rsync File Restore Script [Unfinished] ********************************************* #
# This script is in /raid0/data/backup/ * * #
# * Author: Johnny J. Davis * #
# * Date Created: * #
# [ Last Modified ] * Script name: filerestore_take2.sh * #
# Date: 12/18/12 * [incomplete] * #
# Time: 9:32 AM * * #
# ********************************************* #
##########################################################################################################
#=========================================================================================================
# ****[Begin Variables]****
#=========================================================================================================
LogDns1=/raid0/data/backup/logs/dns1_file_restore.log
LogServices=/raid0/data/backup/logs/services_file_restore.log
LogInet1=/raid0/data/backup/logs/inet1_file_restore.log
LogUser=/raid0/data/backup/logs/UserLog.log
NoLog="echo No log present for $secondhalf" # The variable $secondhalf will be assigned later
RSYNC=/usr/bin/rsync # on as a result of user input.
DATE=/bin/date
ECHO=/bin/echo
USER=root
#---------------------------------------------------------------------------------------------------------
# {End Variables}
#=========================================================================================================
# ****[Begin Functions]****
#=========================================================================================================
# Function to remove previous log, if present (DNS1)
removeLogDns1(){
if [ -a $LogDns1 ]
then
rm $LogDns1
else
echo "No log present for Dns1!"
fi
}
# Function to remove previous log, if present (SERVICES)
removeLogServices(){
if [ -a $LogServices ]
then
rm $LogServices
else
echo "No log present for Services!"
fi
}
# Function to remove previous log, if present (INET1)
removeLogInet1(){
if [ -a $LogInet1 ]
then
rm $LogInet1
else
echo "No log present for Inet1!"
fi
}
#-------------------------------------------------------------(Step 6)------------------------------------
# Function that confirms the intention to restore and restores the file
# placed in the variable fileName by the fileCheck function in step 5b.
restoreFile(){
echo -n "Restore $fileName? [Y/N]?> "
read -r answer
case $answer in
Y ) echo "Restoring the file $fileName!" # Will be Adding additional commands here
;;
N ) echo "Action aborted, nothing restored!"; makeSelection
;;
* ) echo "Must answer with a Y or N!"
;;
esac
#exit 0; sh /raid0/data/backup/filerestore_take2.sh
}
#-------------------------------------------------------------(Step 5a)---------------------------
# Function that changes the current working directory to the choice made in step 2, if in fact
# it was a directory.
cdNow(){
if [ -d $choice ]
then
cd $choice; echo "Changed Directory to $PWD"; makeSelection
else # If the choice from step 2 is not a directory and
echo "This is not a directory!" # manages to make it to this step, the script will
fi # terminate with echo.
}
#-------------------------------------------------------------(Step 5b)---------------------------
# Function checks to see if the choice from step 2, is in fact a file. If so, it stores the file
# name in a variable called fileName and proceeds to call the restoreFile function in step 6.
fileCheck(){
if [ -f $choice ]
then
fileName=$choice; restoreFile
else # If the choice from step 2 is not a file
echo "This is not a file!" # and manages to make it to this step, the
fi # script will terminate with echo.
}
#-----------------------------------------------------------(Step 4)--------------------------------------
# Function that determines if the selection from step 1 is a file
# or directory and moves to next function based on the result.
checkType(){
while [ -d $choice ] # Checks the variable to see if it is a directory.
do # If so, it calls the cdNow function, in step 5a.
cdNow
break;
done
while [ -f $choice ] # Checks the variable to see if it is a file. If so,
do # it calls the fileCheck function, in step 5b.
fileCheck
break;
done
}
#------------------------------------------------------------(Step 3)-------------------------------------
# Function checks for existance of files.
anyFiles(){
ls ./* > /dev/null 2>&1
if [ "$?" = "0" ]
then
checkType
else
echo "No files exist here!"
fi
}
#------------------------------------------------------------(Step 2)-------------------------------------
# Function that allows selecting either a file or directory.
makeSelection(){
PS3="Enter choice [ctrl-c quits]> "
select choice in `for i in $(ls -p ./); do echo ${i%%.*}; done`
do echo; break; done
anyFiles
}
#---------------------------------------------------------------------------------------------------------
# {End Functions}
#=========================================================================================================
# ****[Script Start]**** (Step 1)
#=========================================================================================================
#
echo
echo
echo
clear
echo "Current Working Directory: $PWD"
echo
echo "Please select file or directory."
echo
echo "[Selecton Menu]"
makeSelection
I haven't found a way around using 'ls' or 'find' in my select statement to produce the Selection Menu. I know that they return unsafe IFS results and that it results in some files having multiple menu entries. I am still looking for a way to combat that issue. However, I would like to have someone look at it for me.
Upon execution of the script, I get some prompts duplicated after performing the task. For instance, after traversing several directories, to finally reach the file that I would like to restore. I am prompted to restore the file. I say yes to restore. I get the echo stating that the file is being restored, then immediately after, I get the same prompt. I know it's something in the functions causing this, but I don't know how to resolve it. Also, while entering a selection at the selection menu, I deliberately entered an invalid choice to see what would happen. It drops to /root and echos "No files exist here!" and prompts to Restore ? [Y/N]?>. I don't know what's causing this or how to fix it. Do you have any ideas about any of this?
Please Note, that this script is not complete. I still have much work to do.
I should also point out that I am working on a Thecus N4100Pro Nas via ssh. The box has a very limited command set, as it is running a dumbed-down version of Slackware. Some options for the commands that do exist, are non-existent on this box. For instance, 'find' with the -maxdepth or -mindepth options. Find is a valid command for the box, but the options do not exist.
Thanks for your help.