zsh-users
 help / color / mirror / code / Atom feed
* copying files with shell built in functions?
@ 2003-03-28 16:59 Dominik Vogt
  2003-03-28 17:30 ` Borzenkov Andrey
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Dominik Vogt @ 2003-03-28 16:59 UTC (permalink / raw)
  To: Zsh Users

In a fit of mental derangement, my colleagues tried to exchange
the libc on a machine at run time and used 'mv' to rename the
file.  As you might imagine, they regretted that immediately :-)

Can anyone think of a way to copy or rename a file with only shell
built in functions?  (either zsh or bash)  I have tried things
like

  $ while read <options> X; do echo <options> "$X"; done < ifile > ofile

and

  $ echo $(< ifile) > ofile

(does not work because echo destroys XR and LF)

or

  $ cat <<EOF > ofile
  $(< ifile)
  EOF

(does not work because it still needs a cat and the here document
is mangled.

Bye

Dominik ^_^  ^_^


^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: copying files with shell built in functions?
  2003-03-28 16:59 copying files with shell built in functions? Dominik Vogt
@ 2003-03-28 17:30 ` Borzenkov Andrey
  2003-03-28 17:46 ` Stephane CHAZELAS
  2003-03-28 18:00 ` Zefram
  2 siblings, 0 replies; 7+ messages in thread
From: Borzenkov Andrey @ 2003-03-28 17:30 UTC (permalink / raw)
  To: dominik.vogt, 'Zsh Users'


> In a fit of mental derangement, my colleagues tried to exchange
> the libc on a machine at run time and used 'mv' to rename the
> file.  As you might imagine, they regretted that immediately :-)
> 
> Can anyone think of a way to copy or rename a file with only shell
> built in functions?  (either zsh or bash)  I have tried things
> like
> 
>   $ while read <options> X; do echo <options> "$X"; done < ifile > ofile
> 
> and
> 
>   $ echo $(< ifile) > ofile
> 
> (does not work because echo destroys XR and LF)
> 
> or
> 
>   $ cat <<EOF > ofile
>   $(< ifile)
>   EOF
> 
> (does not work because it still needs a cat and the here document
> is mangled.
> 

bor@itsrm2% zmodload zsh/files
bor@itsrm2% which mv
mv: shell built-in command
bor@itsrm2% which rm
rm: shell built-in command
bor@itsrm2% man zshmodules
bor@itsrm2% echo $ZSH_VERSION
4.0.4

-andrey


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: copying files with shell built in functions?
  2003-03-28 16:59 copying files with shell built in functions? Dominik Vogt
  2003-03-28 17:30 ` Borzenkov Andrey
@ 2003-03-28 17:46 ` Stephane CHAZELAS
  2003-03-28 18:00 ` Zefram
  2 siblings, 0 replies; 7+ messages in thread
From: Stephane CHAZELAS @ 2003-03-28 17:46 UTC (permalink / raw)
  To: Zsh Users

On Fri, Mar 28, 2003 at 05:59:29PM +0100, Dominik Vogt wrote:
[...]
>   $ while read <options> X; do echo <options> "$X"; done < ifile > ofile
> 
> and
> 
>   $ echo $(< ifile) > ofile

bash can't handle '\0', so forget it.

If you have zsh 4, you probably have a mapfile module.

zmodload zsh/mapfile
print -rn -- $mapfile[ifile] > ofile

Also note the zsh/files modules which provides builtin
ln/mv/chown...

If you don't have modules (or can't load them anymore because
your libc is gone), you should still be able to do :

while IFS= read -r line; do
  print -r -- $line
done < ifile > ofile

You'll probably end-up with a trailing \n, but that shouldn't
harm.

But I'm surprises you don't have at least one statically linked
ln or mv somewhere.

Here, I have a statically linked /sbin/zsh with mapfile and
files built in. That can reveal useful.

Also note the "vared mapfile[somefile]" for a text editor.

-- 
Stéphane


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: copying files with shell built in functions?
  2003-03-28 16:59 copying files with shell built in functions? Dominik Vogt
  2003-03-28 17:30 ` Borzenkov Andrey
  2003-03-28 17:46 ` Stephane CHAZELAS
@ 2003-03-28 18:00 ` Zefram
  2003-03-28 19:03   ` Bart Schaefer
  2 siblings, 1 reply; 7+ messages in thread
From: Zefram @ 2003-03-28 18:00 UTC (permalink / raw)
  To: Zsh Users

Dominik Vogt wrote:
>In a fit of mental derangement, my colleagues tried to exchange
>the libc on a machine at run time and used 'mv' to rename the
>file.  As you might imagine, they regretted that immediately :-)

This is precisely what I created the zsh/files module for.  I have the
module loaded all the time, in normal user shells as well as root shells.
I've never had this kind of situation, where the external programs
were unusable, but I've frequently been glad that the argument length
limitations of exec(2) don't apply to builtins ("rm *" in a directory
with *lots* of files).

-zefram


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: copying files with shell built in functions?
  2003-03-28 18:00 ` Zefram
@ 2003-03-28 19:03   ` Bart Schaefer
  2003-03-28 19:11     ` Stephane CHAZELAS
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2003-03-28 19:03 UTC (permalink / raw)
  To: Zsh Users

On Mar 28,  6:00pm, Zefram wrote:
}
} Dominik Vogt wrote:
} >In a fit of mental derangement, my colleagues tried to exchange
} >the libc on a machine at run time and used 'mv' to rename the
} >file.  As you might imagine, they regretted that immediately :-)
} 
} This is precisely what I created the zsh/files module for.  I have the
} module loaded all the time, in normal user shells as well as root shells.

There's no "cp" in zsh/files, though.

Probably because

zsh% >newfile <oldfile

works just fine.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: copying files with shell built in functions?
  2003-03-28 19:03   ` Bart Schaefer
@ 2003-03-28 19:11     ` Stephane CHAZELAS
  2003-03-29  3:46       ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Stephane CHAZELAS @ 2003-03-28 19:11 UTC (permalink / raw)
  To: Zsh Users

On Fri, Mar 28, 2003 at 07:03:47PM +0000, Bart Schaefer wrote:
[...]
> There's no "cp" in zsh/files, though.
> 
> Probably because
> 
> zsh% >newfile <oldfile
> 
> works just fine.

But uses more or cat that generally don't work when there's no
libc.

-- 
Stéphane


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: copying files with shell built in functions?
  2003-03-28 19:11     ` Stephane CHAZELAS
@ 2003-03-29  3:46       ` Bart Schaefer
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2003-03-29  3:46 UTC (permalink / raw)
  To: Zsh Users

On Mar 28,  8:11pm, Stephane CHAZELAS wrote:
} Subject: Re: copying files with shell built in functions?
}
} On Fri, Mar 28, 2003 at 07:03:47PM +0000, Bart Schaefer wrote:
} [...]
} > zsh% >newfile <oldfile
} > 
} > works just fine.
} 
} But uses more or cat that generally don't work when there's no
} libc.

True, I forgot about that.   (It is sort of silly when the multio code
already has a `while (read()) write()' loop.)  There's always:

zsh% print -nr -- "$(<oldfile)" >newfile

or (more fun, but not necessarily less memory intensive)

zsh% zmodload zsh/mapfile
zsh% mapfile[newfile]=$mapfile[oldfile]



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-03-29  3:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-28 16:59 copying files with shell built in functions? Dominik Vogt
2003-03-28 17:30 ` Borzenkov Andrey
2003-03-28 17:46 ` Stephane CHAZELAS
2003-03-28 18:00 ` Zefram
2003-03-28 19:03   ` Bart Schaefer
2003-03-28 19:11     ` Stephane CHAZELAS
2003-03-29  3:46       ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).