LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 913|回复: 10

如何在bash里面引用一个文本的具体内容[基本解决]

[复制链接]
发表于 2004-6-5 19:07:14 | 显示全部楼层 |阅读模式
比如文本如下
1232rewrwefr sdfsdf
sdfsfrwr4r srfdefs
fsdfsdf dfsdjk sdfsdfs432 342342
dfsdf sdf
....
要用for循环,把里面每一个都取出来做操作,如何做呢?
这里的每一个指得是空格或者回车分隔的char序列
发表于 2004-6-5 19:18:34 | 显示全部楼层
for var in $(<file);do
对每个var进行操作
done
比如,查找,替换等等
 楼主| 发表于 2004-6-5 19:37:11 | 显示全部楼层
$(<file)
请问这个用法,能够具体说说么?
我用的是`cat $*`
发表于 2004-6-5 19:46:59 | 显示全部楼层
最初由 qhgary 发表
$(<file)
请问这个用法,能够具体说说么?
我用的是`cat $*`

用`cat file`也可以
 楼主| 发表于 2004-6-5 19:50:48 | 显示全部楼层
就是想听一下$(<file)的解释嘛 :)
发表于 2004-6-5 20:01:26 | 显示全部楼层
$(<file),这个<file,等价于cat file和cat <file.
`cat file`同样也等价于`<file`,但是,`...`,是相对比较古老的写法
$(...),是变量替换,意思是说吧cat file后的内容赋值给for结构的变量列表
然后在for循环里逐一用过var逐一引用并操作
呵呵,大概意思是这样的,我的解释有些勉强! :sorry,重定向和变量替换的例子论坛里很多~
 楼主| 发表于 2004-6-5 21:30:05 | 显示全部楼层
谢谢,刚才试过,的确如此。但是想想单独用<filename想看看是不是列出了文件内容,达到和cat filename相同效果,结果发现失败。看来这个<filename也只有在$()里面才有用武之地哦,呵呵
再次感谢督导
发表于 2004-6-5 21:48:11 | 显示全部楼层
又学了一招:thank
发表于 2004-6-5 22:19:13 | 显示全部楼层
最初由 qhgary 发表
谢谢,刚才试过,的确如此。但是想想单独用<filename想看看是不是列出了文件内容,达到和cat filename相同效果,结果发现失败。看来这个<filename也只有在$()里面才有用武之地哦,呵呵
再次感谢督导

严格来说,这是命名管道的应用。
知识点有很多,但我们要学到最关键的,方能学一当十,触类旁通。
发表于 2004-6-7 08:59:15 | 显示全部楼层
最初由 qhgary 发表
就是想听一下$(<file)的解释嘛 :)
摘自 bash 的 man 帮助:
  1.    Command Substitution
  2.        Command substitution allows the output of a command to replace the com-
  3.        mand name.  There are two forms:

  4.               $(command)
  5.        or
  6.               `command`

  7.        Bash performs the expansion by executing command and replacing the com-
  8.        mand  substitution  with  the  standard output of the command, with any
  9.        trailing newlines deleted.  Embedded newlines are not deleted, but they
  10.        may  be  removed during word splitting.  The command substitution [color=red]$(cat
  11.        file)[/color] can be replaced by the equivalent but faster [color=red]$(< file)[/color].
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表