help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: command for swapping of two files or two dirs?


From: Alex fxmbsw7 Ratchev
Subject: Re: command for swapping of two files or two dirs?
Date: Fri, 26 Mar 2021 22:29:03 +0100

the gawk solution takes two args
gawk -f script.gawk file1 file2
and does so, if no error, test, in one go

On Fri, Mar 26, 2021 at 10:21 PM Peng Yu <pengyu.ut@gmail.com> wrote:
>
> The problem is that the swap based on multiple commands is not atomic.
>
> With multiple commands, wouldn't it cause complications for error
> recovery? Also, is it safe? If there is any malicious program delete
> some files during the process?
>
> On 3/26/21, Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com> wrote:
> > i agree to all excepts the srandom thingy
> > a [[ -e loop passthru maybe.. like always
> >
> > On Fri, Mar 26, 2021 at 9:58 PM Greg Wooledge <greg@wooledge.org> wrote:
> >>
> >> On Fri, Mar 26, 2021 at 09:18:10PM +0100, Alex fxmbsw7 Ratchev wrote:
> >> > swap() { declare t= tt= ; [[ $# != 2 ]] && return 1
> >> >  mv "$1" "${t:=/tmp/$SRANDOM}"
> >> >  mv "$2" "${tt:=/tmp/$SRANDOM}"
> >> >  mv "$t" "$2"
> >> >  mv "$tt" "$1"
> >> > }
> >>
> >> 1) Use mktemp(1) or tempfile(1) or whatever.  Your $SRANDOM hack has
> >>    security problems.
> >>
> >> 2) Don't move the file to /tmp.  That might be on a different file
> >> system.
> >>    You want to keep the file within its current directory, as that's the
> >>    only place you can be sure isn't on another file system.
> >>
> >> 3) There should only need to be three moves.
> >>
> >> 4) You probably want && between the moves, to abort if something goes
> >>    wrong.
> >>
> >
> >
>
>
> --
> Regards,
> Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]