You are not logged in.

#1 2019-01-25 10:25:04

S7.L
Member
Registered: 2018-09-16
Posts: 338

Cpmvbak file backup script

Ive figured out a script whereby first it asks you to input the file name then backs it up in the directory you are working in else it fails if incorrect filename, then it asks if you want to move it to $HOME/Backups via yes no function, i was wondering if this could look better or be a bit simpler and perhaps i have some improper scripting in there as im only a newbie. I would like to use say a tab completion on any file names in the directory when it asks for the file name, would that be possible?

i call it cpmvbak just for the functions it uses.

#!/bin/bash
function yes_or_no {
    while true; do
        read -p "$* [y/n]: " yn
        case $yn in
            [Yy]*) return 0  ;;  
            [Nn]*) echo "Aborted" ; return  1 ;;
        esac
    done
}

echo "enter a file name to backup"
read FILE

if [ -f $FILE ]; then
    cp $FILE $FILE.bak
    echo "file successfully backed up"

else

    echo "file does not exist"

fi

yes_or_no "move .bak to $HOME/Backups" &&
mv $FILE.bak $HOME/Backups

exit 1

EDIT: revision of script in post 4 https://forums.bunsenlabs.org/viewtopic … 355#p81355

Last edited by S7.L (2019-01-26 11:55:19)

Offline

#2 2019-01-26 07:49:47

ohnonot
...again
Registered: 2015-09-29
Posts: 3,773
Website

Re: Cpmvbak file backup script

i once wrote myself a function similar to your yes_or_no function, here it is:

confirm() 
{
# confirm will read a single character and decide whether it is yes or no.
# $1 is the prompt string, $2 is an optional default (if input is only return).
	local answer IFS=
	while read -rp "$1" -n1 answer
	do
	case $answer in 
		[Yy]) printf '\n'; return 0;;
		[Nn]) printf '\n'; return 1;;
		*) case "$2" in
			[Yy]) [[ "$answer" = "" ]] && return 0 || printf '\n';;
			[Nn]) [[ "$answer" = "" ]] && return 1 || printf '\n';;
			*) [[ "$answer" != "" ]] && printf '\n';;
			esac
		;;
	esac
	done
}

usage example:

if confirm "move .bak to $HOME/Backups" y; then mv "$FILE" "$HOME/Backups"; else exit 1; fi

I'd also change the read file portion; depends a little on what you want, but I'd probably do it like this:

<snip>

FILE=""
while ! [ -r "$FILE" ]; do
	read -p "enter a file name to backup" FILE
done

cp $FILE $FILE.bak
echo "file successfully backed up"

<snip>

... and why does your script exit with 1? that's an error.
you don't need that at all; it will exit with the last command's return value, which is more informative in this case.

Last edited by ohnonot (2019-01-26 07:54:53)

Offline

#3 2019-01-26 11:20:32

S7.L
Member
Registered: 2018-09-16
Posts: 338

Re: Cpmvbak file backup script

Thanks for chiming in ohnonot, your edits dont seem to work for me?

#!/bin/bash
confirm() 
{
# confirm will read a single character and decide whether it is yes or no.
# $1 is the prompt string, $2 is an optional default (if input is only return).
	local answer IFS=
	while read -rp "$1" -n1 answer
	do
	case $answer in 
		[Yy]) printf '\n'; return 0;;
		[Nn]) printf '\n'; return 1;;
		*) case "$2" in
			[Yy]) [[ "$answer" = "" ]] && return 0 || printf '\n';;
			[Nn]) [[ "$answer" = "" ]] && return 1 || printf '\n';;
			*) [[ "$answer" != "" ]] && printf '\n';;
			esac
		;;
	esac
	done
}

FILE=""
while ! [ -r "$FILE" ]; do
	read -p "enter a file name to backup" FILE
done

cp $FILE $FILE.bak
echo "file successfully backed up"

else

    echo "file does not exist"

fi

if confirm "move .bak to $HOME/Backups" y; then mv "$FILE" "$HOME/Backups"; else exit 1; fi

Im guessing that is how the layout should be going by your suggestions but it doesnt get any further than the "enter a file name to backup" i think its because its a loop due to that while?

Offline

#4 2019-01-26 11:24:48

S7.L
Member
Registered: 2018-09-16
Posts: 338

Re: Cpmvbak file backup script

So here is alpha2 keeping my edits, info in the script itself is what i would like to accomplish with it. It should be a good learning experience.

#!/bin/bash

## see "man test" for more info
## this script copies a file or folder and its contents and gives it a .bak extension then moves it to $HOME/Backups.
## todo: need to figure out how to get script to create $HOME/Backups if it is not there. Provide a --help flag

## yes/no function
function yes_or_no {
    while true; do
        read -p "$* [y/n]: " yn
        case $yn in
            [Yy]*) return 0  ;;  
            [Nn]*) echo "Aborted" ; return  1 ;;
        esac
    done
}
##manually type in exact file name to backup.
## todo: need to figure out some sort of return value if user doesnt want to backup a file and only a directory so the script just moves to creating a directory backup?
echo "enter file name to backup"
read FILE
if [ -f $FILE ]; then
    cp -r $FILE $FILE.bak
    echo "file successfully backed up"
else
    echo "file does not exist"
fi
## this moves the backup file to desired directory
yes_or_no "move file to $HOME/Backups ?" &&
mv $FILE.bak $HOME/Backups

## manually type in the directory name
## here is a second choice to backup directories - todo: need to figure out some sort of return value if user doesnt want to backup a directory so the script just exits or returns to file backup maybe?
echo "enter directory name to backup"
read FILE
if [ -d $FILE ]; then
    cp -r $FILE $FILE.bak
    echo "directory successfully backed up"
else
    echo "directory does not exist"
fi
## this moves the backup directory to desired directory
yes_or_no "move folder to $HOME/Backups ?" &&
mv $FILE.bak $HOME/Backups

Last edited by S7.L (2019-01-26 11:26:11)

Offline

#5 2019-01-26 11:32:59

ohnonot
...again
Registered: 2015-09-29
Posts: 3,773
Website

Re: Cpmvbak file backup script

re post #3:
you should've removed the "else ... fi" since there's no if anymore!

Offline

#6 2019-01-26 11:45:36

S7.L
Member
Registered: 2018-09-16
Posts: 338

Re: Cpmvbak file backup script

Ok so it should look like this after the function?

FILE=""
while ! [ -r "$FILE" ]; do
	read -p "enter a file name to backup" FILE
done

cp $FILE $FILE.bak
echo "file successfully backed up"

if confirm "move .bak to $HOME/Backups" y; then mv "$FILE" "$HOME/Backups"; else exit 1; fi

if so that leaves the .bak in the work directory and does not move it to $HOME/Backups rather it does the opposite.

Offline

#7 2019-01-27 07:15:50

ohnonot
...again
Registered: 2015-09-29
Posts: 3,773
Website

Re: Cpmvbak file backup script

S7.L wrote:

if so that leaves the .bak in the work directory and does not move it to $HOME/Backups rather it does the opposite.

i see.
i must've gotten the filenames wrong.
but i just wanted to show you the logic, not a working script.
i'm sure you'll figure it out.

btw this

if confirm "move .bak to $HOME/Backups" y; then mv "$FILE" "$HOME/Backups"; else exit 1; fi

is exactly the same as

if confirm "move .bak to $HOME/Backups" y
then
    mv "$FILE" "$HOME/Backups"
else
    exit 1
fi

Offline

#8 2019-01-27 13:13:23

S7.L
Member
Registered: 2018-09-16
Posts: 338

Re: Cpmvbak file backup script

^ ok thanks. Ill re visit this over the weekend.

Offline

Board footer

Powered by FluxBB