[prog] Bash script blues

Jacinta Richardson jarich at perltraining.com.au
Thu Aug 25 19:13:13 EST 2005


Kaitlyn wrote:

> However I get errors:
> kaitlyn at rogue ~/bin/working $ for i in `ls -1 |grep .torrent` ; do mv
> "$i" `echo "$i" |sed s/\ /_/g` ; done
> mv: cannot stat `this': No such file or directory
> mv: cannot stat `is': No such file or directory
> mv: cannot stat `torrent.torrent': No such file or directory
> kaitlyn at rogue ~/bin/working $ ls
> this is torrent.torrent
> kaitlyn at rogue ~/bin/working $

If you want a solution to make the problem away I can help you with that.  If
you want to know why your above code isn't working, I can't help so much.  If
you try:

	for i in `ls -1 | grep .torrent`; do echo "$i"; done;

you'll find that what the for loop is giving you is

	this
	foo
	it.torrent
	this_that_torrent.torrent

which isn't a good thing at all.  I don't know how to make the for loop behave
itself.

I also can't make:

	echo "$i" | sed s/\ /_/g

work when I provide my own string, but I don't know how to fix that either.

Anyway, if you want a useable solution I can certainly help.  For a pure perl
solution try:

	perl -e 'use File::Copy; foreach(<*.torrent>) {$newname = $_; $newname =~ s/
/_/g; move($_, $newname); }';

If you want a slightly more bash-ish solution we could do:

	perl -e 'for(<*.torrent>) {$nn = $_; $nn =~ s/ /_/g; system("mv", $_, $nn"); }';


I'm sorry I can't actually help you with the bash issue though.

All the best,

	Jacinta

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |




More information about the Programming mailing list