You are not logged in.

#1 2020-05-30 08:32:12

abarbarian
Member
Registered: 2012-03-11
Posts: 30

rsync + alias puzzle [SOLVED]

I made a command and made an alias of it,


rsync -vaHAXShix --delete --exclude=slots --exclude=tempfile --exclude=.local/share/Trash --exclude=projects --exclude=.local/share/gvfs-metadata --exclude=/home/*/.gvfs --exclude=.local/share/gvfs-metadata /home/bloodaxe/  /run/media/bloodaxe/HISTORY/bloodaxe$(date +\\%d-\\%m-\\%Y)"

alias homebackup="rsync -vaHAXShix --delete --exclude=slots --exclude=tempfile --exclude=.local/share/Trash --exclude=projects --exclude=.local/share/gvfs-metadata --exclude=/home/*/.gvfs --exclude=.local/share/gvfs-metadata /home/bloodaxe/  /run/media/bloodaxe/HISTORY/bloodaxe$(date +\\%d-\\%m-\\%Y)"

Both work ok but they output different folder names like so,

rsync command gives me a folder called

bloodaxe\25-\05-\2020

however when I make a alias with the same command and use the alias I get a folder called,

bloodaxe25-05-2020

Should not my alias output the same folder name ? I thought alias's simply duplicated the given command wheras my example seems to alter the original command.

I have searched the net but can find no explanation.

Thanks in advance for any answers. ;-)

Last edited by abarbarian (2020-05-30 10:59:37)

Offline

#2 2020-05-30 08:49:15

frostschutz
Member
Registered: 2013-11-15
Posts: 1,418

Re: rsync + alias puzzle [SOLVED]

$ date +\%d
30

basically that \ isn't even there. \% means nothing so date +\%d is same as +%d.

$ echo date +\%d
date +%d

With \ escapes in general... the shell might eat one. if you add stuff in double quoted strings, that eats one. the deeper down you go the more additional escapes you need to come out with the same result

$ date +\%d
30
$ date +\\%d
\30
$ eval "date +\\%d"
30
$ eval "date +\\\\%d"
\30
$ eval "eval \"date +\\\\%d\""
30
$ eval "eval \"date +\\\\\\%d\""
\30

At some point you should consider stop using aliases and write a shell script instead then run the shell script. it's a lot easier to understand if the entire command does not live inside "" extra layers of quotes

aliases are great for simple shortcuts, less great for complex series of options that need to be quoted themselves

if you don't want the \ in the folder name just remove it since you don't need it for date +%d either way. if you feel safer with it quoted you could use single quote string like date '+%d' then you don't need escape for "doublequote" inside doublequoted string.

Last edited by frostschutz (2020-05-30 08:52:43)

Online

#3 2020-05-30 09:16:04

Ropid
Member
Registered: 2015-03-09
Posts: 1,069

Re: rsync + alias puzzle [SOLVED]

You can use a function instead of an alias. Compared to an alias, with a function you won't have to add quotes around your command. This then makes it so you don't have to battle with how escaping is changing when inside quotes.

Your 'homebackup' would look like this as a function:

homebackup () { rsync -vaHAXShix --delete --exclude=slots --exclude=tempfile --exclude=.local/share/Trash --exclude=projects --exclude=.local/share/gvfs-metadata --exclude=/home/*/.gvfs --exclude=.local/share/gvfs-metadata /home/bloodaxe/  /run/media/bloodaxe/HISTORY/bloodaxe"$(date +%d-%m-%Y)" ; }

You can add a bunch of line-breaks to help with reading:

homebackup () {
    rsync -vaHAXShix --delete \
        --exclude=slots \
        --exclude=tempfile \
        --exclude=.local/share/Trash \
        --exclude=projects \
        --exclude=.local/share/gvfs-metadata \
        --exclude=/home/*/.gvfs \
        --exclude=.local/share/gvfs-metadata \
        /home/bloodaxe/ \
        /run/media/bloodaxe/HISTORY/bloodaxe"$(date +%d-%m-%Y)"
}

I removed those "\\" you had in your command and I added " quotes around the "$(date ...)".

Offline

#4 2020-05-30 09:31:56

a821
Member
Registered: 2012-10-31
Posts: 381

Re: rsync + alias puzzle [SOLVED]

you might as well use `--exclude-from=FILE`. See rsync(1).

Offline

#5 2020-05-30 10:59:08

abarbarian
Member
Registered: 2012-03-11
Posts: 30

Re: rsync + alias puzzle [SOLVED]

frostschutz

Thanks for the clear explanation. I found the date tip on line and simply copied their code. I was concentrating on the various rsync options which seemed more important at the time. I am back to tinkering after a break so am a tad rusty. I have made small scripts before and will probably make one to do my backup task now I have tamed rsync.

Ropid

Thanks for the function solution. I have been reading about functions but the information was strugling to stick to the grey cell.

a821

Thanks I have used the `--exclude-from=FILE`feature before and will do so in the future. One step at a time.Although that would not supply an answer to my query.

Thanks folks for the answers.

Offline

#6 2020-05-30 13:45:45

securitybreach
Member
From: In front of my computers
Registered: 2007-11-18
Posts: 416
Website

Re: rsync + alias puzzle [SOLVED]

Great information, thanks.


"Every normal man must be tempted at times to spit upon his hands, hoist the black flag, and begin slitting throats." -- H.L. Mencken
Website      Configs
Forum Admin: Bruno's All Things Linux   
securitybreach<a>archlinux.us

Offline

#7 2020-06-01 03:04:51

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: rsync + alias puzzle [SOLVED]

Ropid wrote:

You can use a function instead of an alias. Compared to an alias, with a function you won't have to add quotes around your command. This then makes it so you don't have to battle with how escaping is changing when inside quotes.

Addendum: a number of experienced people in the bash development community regard aliases as an ill-conceived notion which should be avoided at all costs, and argue that functions deprecate aliases "since you can do everything you need to do with functions, without the terrible buggy behavior of aliases".

There is certainly no denying that functions are the *only* way to properly implement e.g. interpolating values into a command. aliases are merely macros (evaluated as strings at the time the alias is sourced or otherwise defined), inserted into the command line -- they're really a pain in the neck in so many ways.

Last edited by eschwartz (2020-06-01 04:38:41)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#8 2020-06-01 04:29:55

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,534
Website

Re: rsync + alias puzzle [SOLVED]

Also note as an alias - at least one in double quotes rather than single - that command will not do what you likely intended it to do.  The name of the folder generated from `date` will not be the date on which you run the command: it will be the date on which the shellrc file was processed and the alias defined.  If you relogin daily, these might coincidentally be the same most of the time, but eventually you'll get a really confusing problem of new backups not using the "correct" date.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB