Results 1 to 2 of 2
Hey guys! Just getting started with bash scripts yesterday, so bare with me. :P I'm trying to make a script that will backup all the non-directory files in the working ...
- 03-23-2011 #1Just Joined!
- Join Date
- Mar 2011
- Location
- US
- Posts
- 2
Bash Script Backup Files Help - Newbie-
Hey guys! Just getting started with bash scripts yesterday, so bare with me. :P I'm trying to make a script that will backup all the non-directory files in the working directory into a backup folder. I'm having a few problems:
1) I wanted the user to be able to put in both capital and lowercase letters for the user input, but my if [[ "$text" == "[yY]" ]] statement didn't work. What's a good way to get conditional statements to ignore letter case?
2) The code as it is successfully writes all non-directory files to the backup folder, but my prompt displays "/usr/bin/backup: 46: [[: not found" whenever it sees a file that isn't a directory. I have no idea what this is. Line 46 in my code is the "done" for the "for" loop.
3) What's a good way for me to make sure the keyboard input is either Y/y, N/n, or all? I don't want to use any goto statements or the like. I just want to display a "Not a valid input" message and have the user input again.
4) And why does "if [ $test=y ]" test true if test=n? (general question)
Any help appreciated!
Thanks!
Code:# This will backup files in a backup current directory # FILES stores the contents of the current directory # OVERWRITE_TEMP is used for user control of present file overwrite # OVERWRITE_BOOL is used for user control of full file overwrite FILES=`ls` OVERWRITE_BOOL=1 # Step through each of the directory listings for f in $FILES do # Test if the file is a director if [ -d $f ] then echo "Skipping $f ..." elif [ -f $f ] then if [ $OVERWRITE_BOOL -eq 1 ] then temp=`ls /Backups | grep $f` if [[ "$temp" == "$f" ]] then echo "The file $fif already exists ..." echo -n "Do you wish to overwrite?" echo -n '[y],[n],[all]' read text if [[ "$text" == "y" ]] then cp $f /Backups elif [[ "$text" == "n" ]] then echo "Skipping $f ..." elif [[ "$text" == "all" ]] then OVERWRITE_BOOL=0 fi else cp $f /Backups fi else cp $f /Backups fi fi done echo "Process Completed ... "
- 03-23-2011 #2
Not only is ls unnecessary, but it will break your script if any filenames contain whitespace. Use parameter expansion (see below)Code:OVERWRITE_BOOL=1 # Step through each of the directory listings for f in $FILES
[repeat advice about whitespace
]
Code:for f in *
Code:do # Test if the file is a director if [ -d $f ]
That will break your sacript if $f contains whitespace; always quote variable references (unless there is a reason not to, which is rare):
(I will not repeat this where the problem exists again)Code:if [ -d "$f" ]
Code:then echo "Skipping $f ..." elif [ -f $f ] then if [ $OVERWRITE_BOOL -eq 1 ] then temp=`ls /Backups | grep $f` if [[ "$temp" == "$f" ]] then echo "The file $fif already exists ..." echo -n "Do you wish to overwrite?" echo -n '[y],[n],[all]' read text if [[ "$text" == "y" ]] then cp $f /Backups elif [[ "$text" == "n" ]] then echo "Skipping $f ..." elif [[ "$text" == "all" ]] then OVERWRITE_BOOL=0 fi
Use a case statement:
Code:case $text in [yY]) cp "$f" /Backups ;; [nN]) echo "Skipping $f ..." ;; all) OVERWRITE_BOOL=0 ;;


Reply With Quote
