免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3862 | 回复: 10
打印 上一主题 下一主题

文件名更名操作 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-10-23 11:38 |只看该作者 |倒序浏览
本帖最后由 tonyliuy 于 2015-10-23 11:39 编辑

想给文件夹里的文件更名,原文件名和新文件名在一个list里
  1.    while (<IN>) {
  2.        chomp;
  3.        @chr = split;
  4.       if ($i % 2 == 0) {
  5.           system("mv $chr[5].1old $chr[0].1new") or print OUT "$!\n";
  6.           system("mv $chr[5].2old $chr[0].2new") or print OUT "$!\n";
  7.       }
  8.       $i ++;
  9.   }
  10.   print OUT "$i\n";
复制代码
其中第六列是原文件名,第一列是新文件名,运行之后1new文件没问题,2new文件清了,全变成了每行都是“1”的文件
而且文件报错,$!信息是Inappropriate ioctl for device
求大神指教,错在哪里了?改怎么改脚本?

论坛徽章:
0
2 [报告]
发表于 2015-10-23 16:04 |只看该作者
求助各路大神帮忙解答~~~

论坛徽章:
0
3 [报告]
发表于 2015-10-30 15:46 |只看该作者
求大神解答,帖子不要沉~~

论坛徽章:
78
双子座
日期:2013-10-15 08:50:09天秤座
日期:2013-10-16 18:02:08白羊座
日期:2013-10-18 13:35:33天蝎座
日期:2013-10-18 13:37:06狮子座
日期:2013-10-18 13:40:31双子座
日期:2013-10-22 13:58:42戌狗
日期:2013-10-22 18:50:04CU十二周年纪念徽章
日期:2013-10-24 15:41:34巨蟹座
日期:2013-10-24 17:14:56处女座
日期:2013-10-24 17:15:30双子座
日期:2013-10-25 13:49:39午马
日期:2013-10-28 15:02:15
4 [报告]
发表于 2015-10-30 18:12 |只看该作者
http://stackoverflow.com/questio ... te-ioctl-for-device

$!只有失败操作时才有意义

另外你那个or用的不对,$!也不对

论坛徽章:
78
双子座
日期:2013-10-15 08:50:09天秤座
日期:2013-10-16 18:02:08白羊座
日期:2013-10-18 13:35:33天蝎座
日期:2013-10-18 13:37:06狮子座
日期:2013-10-18 13:40:31双子座
日期:2013-10-22 13:58:42戌狗
日期:2013-10-22 18:50:04CU十二周年纪念徽章
日期:2013-10-24 15:41:34巨蟹座
日期:2013-10-24 17:14:56处女座
日期:2013-10-24 17:15:30双子座
日期:2013-10-25 13:49:39午马
日期:2013-10-28 15:02:15
5 [报告]
发表于 2015-10-30 18:12 |只看该作者
http://stackoverflow.com/questio ... te-ioctl-for-device

$!只有失败操作时才有意义

另外你那个or用的不对,$!也不对

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
6 [报告]
发表于 2015-11-01 08:36 |只看该作者
回复 1# tonyliuy

# perldoc -f rename
    rename OLDNAME,NEWNAME
            Changes the name of a file; an existing file NEWNAME will be
            clobbered. Returns true for success, false otherwise.

            Behavior of this function varies wildly depending on your system
            implementation. For example, it will usually not work across file
            system boundaries, even though the system mv command sometimes
            compensates for this. Other restrictions include whether it works
            on directories, open files, or pre-existing files. Check perlport
            and either the rename(2) manpage or equivalent system
            documentation for details.

            For a platform independent "move" function look at the File::Copy
            module.

   

论坛徽章:
0
7 [报告]
发表于 2015-11-02 09:26 |只看该作者
$!只有失败时才有意义这个我知道,你说的用的不对是指什么?我想的是system函数运行成功,or后面的就不运行了;system运行出错,就报出错误原因,看看是文件名的问题还是内存什么的其他问题回复 4# yybmsrs


   

论坛徽章:
78
双子座
日期:2013-10-15 08:50:09天秤座
日期:2013-10-16 18:02:08白羊座
日期:2013-10-18 13:35:33天蝎座
日期:2013-10-18 13:37:06狮子座
日期:2013-10-18 13:40:31双子座
日期:2013-10-22 13:58:42戌狗
日期:2013-10-22 18:50:04CU十二周年纪念徽章
日期:2013-10-24 15:41:34巨蟹座
日期:2013-10-24 17:14:56处女座
日期:2013-10-24 17:15:30双子座
日期:2013-10-25 13:49:39午马
日期:2013-10-28 15:02:15
8 [报告]
发表于 2015-11-02 11:29 |只看该作者
回复 7# tonyliuy


    实际跟你想的是反的,system成功返回的是0,失败返回的是非0
  1.                Since "SIGINT" and "SIGQUIT" are ignored during the execution
  2.                of "system", if you expect your program to terminate on receipt
  3.                of these signals you will need to arrange to do so yourself
  4.                based on the return value.

  5.                    @args = ("command", "arg1", "arg2");
  6.                    system(@args) == 0
  7.                         or die "system @args failed: $?"

  8.                If you’d like to manually inspect "system"’s failure, you can
  9.                check all possible failure modes by inspecting $? like this:

  10.                    if ($? == -1) {
  11.                        print "failed to execute: $!\n";
  12.                    }
  13.                    elsif ($? & 127) {
  14.                        printf "child died with signal %d, %s coredump\n",
  15.                            ($? & 127),  ($? & 128) ? 'with' : 'without';
  16.                    }
  17.                    else {
  18.                        printf "child exited with value %d\n", $? >> 8;
  19.                    }
复制代码

论坛徽章:
0
9 [报告]
发表于 2015-11-04 09:19 |只看该作者
原来是这样... ...谢了~回复 8# yybmsrs


   

论坛徽章:
7
巳蛇
日期:2013-11-28 09:22:59天秤座
日期:2014-10-25 15:40:452015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之德黑兰石油
日期:2015-07-15 08:46:452015亚冠之平阳省
日期:2015-11-08 16:27:53白银圣斗士
日期:2015-11-14 09:58:12
10 [报告]
发表于 2015-11-04 10:08 |只看该作者
直接用perl内置的rename是不是更好?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP