Chinaunix

标题: find和for怎么一起用 [打印本页]

作者: wolaiye05    时间: 2012-12-24 10:07
标题: find和for怎么一起用
find得到一些文件列表,但for遍历该列表时,发现得不到完整的路径,因为文件路径有些空格。想在遍历之前,将" "转换成"\ ",始终失败。这种情况该如何办?
作者: dn833    时间: 2012-12-24 10:33
for i in ....... ;do ....... "$i";done
作者: wolaiye05    时间: 2012-12-24 10:42
dn833 发表于 2012-12-24 10:33
for i in ....... ;do ....... "$i";done
  1. list=`find -type f -name "*.txt"`
  2. for i in "$list"
  3. do
  4. echo $i
  5. done
复制代码
如何遍历的文件路径含有空格 就被当成两个或多个文件路径处理 想知道怎么让for 读取每个txt文件的路径。
作者: blackold    时间: 2012-12-24 10:48
回复 3# wolaiye05


    设置IFS
作者: seeLnd    时间: 2012-12-24 10:58
这种情况最好改用 while read吧,以行为单位,就不用管空格了
  1. find ... | while read line; do
  2.   echo "$line"
  3.    ....
  4. done
复制代码

作者: waker    时间: 2012-12-24 11:06
为啥不试试xargs
作者: Shell_HAT    时间: 2012-12-24 13:28
回复 3# wolaiye05
  1. list=`find -type f -name "*.txt"`
  2. saveIFS=$IFS
  3. IFS="\\"
  4. for i in "$list"
  5. do
  6.     echo $i
  7. done
  8. IFS=$saveIFS
复制代码

作者: wolaiye05    时间: 2012-12-24 14:53
blackold 发表于 2012-12-24 10:48
回复 3# wolaiye05

在find跟for结合使用下,IFS设置成"\t\n",取不到正常文件路径。
作者: blackold    时间: 2012-12-24 14:55
回复 8# wolaiye05


    这样设,取不到是正常的。

作者: wolaiye05    时间: 2012-12-24 14:55
seeLnd 发表于 2012-12-24 10:58
这种情况最好改用 while read吧,以行为单位,就不用管空格了

find和while结合可以取到正常文件路径,先前也这样写过,不过在find命令写成`find ...`,导致娶不到正常文件路径。
作者: wolaiye05    时间: 2012-12-24 14:56
waker 发表于 2012-12-24 11:06
为啥不试试xargs

不清楚xargs在这里怎么使用?
作者: wolaiye05    时间: 2012-12-24 15:03
blackold 发表于 2012-12-24 14:55
回复 8# wolaiye05

“Shell 脚本中有个变量叫 IFS(Internal Field Seprator) ,内部域分隔符。完整定义是The shell uses the value stored in IFS, which is the space, tab, and newline characters by default, to delimit words for the read and set commands, when parsing output from command substitution, and when performing variable substitution.”
看IFS的网络介绍,默认就是space,tab和newline,如果说设置不对,那我这里应该设置成IFS='.txt'或者其他的?
作者: wolaiye05    时间: 2012-12-24 15:05
Shell_HAT 发表于 2012-12-24 13:28
回复 3# wolaiye05

尝试了 没有正常取道文件路径
作者: blackold    时间: 2012-12-24 15:10
回复 12# wolaiye05


   

try: (IFS=$'\n'; for i in $list;do echo "[$i]";done)
作者: Shell_HAT    时间: 2012-12-24 15:45
回复 13# wolaiye05


    测试结果贴出来看看
作者: wolaiye05    时间: 2012-12-24 15:58
回复 15# Shell_HAT
  1. list=`find "${HOME}/test" -type f -name "*.txt"`
  2. IFS_old=$IFS
  3. IFS='\\'
  4. for i in "${list}"
  5. do
  6.         echo " 111 " $i
  7. done
  8. IFS=$IFS_old
复制代码
结果是
。。。。。。。。
111  /Users/wxj120bw/test/test 11/1.txt
/Users/wxj120bw/test/test 22/2.txt
logout

应该获取结果如下
。。。。。。。。
111  /Users/wxj120bw/test/test 11/1.txt
111  /Users/wxj120bw/test/test 22/2.txt


   
作者: Shell_HAT    时间: 2012-12-24 16:16
本帖最后由 Shell_HAT 于 2012-12-24 16:51 编辑

回复 16# wolaiye05

list=`find "${HOME}/test" -type f -name "*.txt"`
IFS_old=$IFS
IFS=$'\n'
for i in ${list}
do
    echo " 111 " $i
done
IFS=$IFS_old

作者: wolaiye05    时间: 2012-12-24 16:28
Shell_HAT 发表于 2012-12-24 16:16
回复 16# wolaiye05
  1. IFS=\n'是不是要改成IFS=‘\n' 结果跟'\\'一样 谢谢你们的回复
复制代码

作者: Shell_HAT    时间: 2012-12-24 16:52
本帖最后由 Shell_HAT 于 2012-12-24 16:53 编辑

回复 18# wolaiye05


    是DZ论坛code标签的bug,它会吧$'给删掉,17楼的code标签已经删掉了。你再试试
作者: wolaiye05    时间: 2012-12-24 17:36
Shell_HAT 发表于 2012-12-24 16:52
回复 18# wolaiye05

IFS设置成$'\n' 成功获取文件路径
作者: waker    时间: 2012-12-24 19:25
本帖最后由 waker 于 2012-12-24 19:25 编辑
wolaiye05 发表于 2012-12-24 14:56
不清楚xargs在这里怎么使用?

find "${HOME}/test" -type f -name "*.txt" -print0|xargs -0  echo xxx
作者: wolaiye05    时间: 2012-12-25 12:50
回复 21# waker
的确可以获取正常文件路径


   




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