Chinaunix

标题: 一个想重命名文件的小脚本出现问题。 [打印本页]

作者: poke006    时间: 2009-06-22 16:37
标题: 一个想重命名文件的小脚本出现问题。
脚本名:rename2txt.sh
代码:
#!/bin/bash

ls $1 | more > /home/spyda/Documents/tmp.txt
cat /home/spyda/Documents/tmp.txt
while read old_filename;do
  mv $old_filename $old_filename.txt
  echo "$old_filename.txt"
done < /home/spyda/Documents/tmp.txt 2>&1
rm  /home/spyda/Documents/tmp.txt
exit 0


运行./rename2txt.sh /home/spyda/Documents/blogs时出现一下症状:

mv: cannot stat `5.30': No such file or directory
5.30.txt
mv: cannot stat `5.31': No such file or directory
5.31.txt
mv: cannot stat `6.10': No such file or directory
6.10.txt
mv: cannot stat `6.11': No such file or directory
6.11.txt
mv: cannot stat `6.15': No such file or directory
6.15.txt
mv: cannot stat `6.2': No such file or directory
6.2.txt
mv: cannot stat `6.21': No such file or directory
6.21.txt
mv: cannot stat `6.3': No such file or directory
6.3.txt
mv: cannot stat `6.4': No such file or directory
6.4.txt
mv: cannot stat `6.6': No such file or directory
6.6.txt
mv: cannot stat `6.7': No such file or directory
6.7.txt
mv: cannot stat `6.8': No such file or directory
6.8.txt
mv: cannot stat `6.9': No such file or directory
6.9.txt


小生很菜,望达人指教,谢谢!
作者: blackold    时间: 2009-06-22 16:47
加双引号试试mv "$old_filename" "$old_filename.txt"
作者: poke006    时间: 2009-06-22 17:02
标题: 回复 #2 blackold 的帖子
还是上面的错误。
作者: 南无小和尚    时间: 2009-06-22 17:09
是路径的问题不?
作者: blackold    时间: 2009-06-22 17:17
看一下/home/spyda/Documents/tmp.txt文件里面的内容。

[ 本帖最后由 blackold 于 2009-6-22 17:57 编辑 ]
作者: liaosnet    时间: 2009-06-22 17:24
路径~~

改一下~应该是你的命令不在 $1所在的目录里~~
作者: blackold    时间: 2009-06-22 17:32
标题: 回复 #6 liaosnet 的帖子
嗯,是这个问题。
作者: neunaruto    时间: 2009-06-22 17:55
代码没有错误,应该是你传递的参数没有指定正确重命名文件的路径,可以使用相对会绝对路径。
作者: poke006    时间: 2009-06-22 18:42
标题: 回复 #8 neunaruto 的帖子
对的,是没有写明路径。
作者: polokus    时间: 2009-06-23 00:25
ls  的结果是要转向到 文件中,那还用more做什么?
作者: chinaglwo    时间: 2009-06-23 02:32
for i in `ls`;do mv $i "`echo $i |sed 's/$/.txt/g'`"; done

用这个试试
作者: poke006    时间: 2009-06-24 10:13
标题: 回复 #10 polokus 的帖子
我是想将ls的输出结果做成一列的。不知道不用more是不是也可以。
作者: poke006    时间: 2009-06-24 10:15
标题: 回复 #11 chinaglwo 的帖子
是不是
         for i in `ls`;do mv $i "`echo $i |sed 's/$/.txt/g'`"; done
这里面也得转入目标目录里面啊?
作者: blackold    时间: 2009-06-24 10:18
标题: 回复 #12 poke006 的帖子
ls本身就是列输出的,不用再去摸它了。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2