免费注册 查看新帖 |

Chinaunix

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

为什么会ambiguous redirect [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-18 11:07 |只看该作者 |倒序浏览
bash-2.03$ while read filename filesize
> do
> echo $filename $filesize
> done < `ls -l |awk '{print $9,$5}'`
bash: `ls -l |awk '{print $9,$5}'`: ambiguous redirect
bash-2.03$ ls -l |awk '{print $9,$5}'

100607 96
100611 96
100614 96
100615 96
100617 96
100618 96

为什么直接执行可以,但是加到while do的判断中不行,
难道一定要把ls -l |awk '{print $9,$5}' 指定到一个文件,然后再用while do done调用文件才可以吗.

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
2 [报告]
发表于 2010-06-18 11:11 |只看该作者
好像没见过这个用法。awk的输入来自重定向?

其实可以这样
while read filename filesize
do awk .....
done.
或是ls -l|xags |awk ;然后while read......

论坛徽章:
17
2015年辞旧岁徽章
日期:2015-03-03 16:54:152017金鸡报晓
日期:2017-02-08 10:39:422017金鸡报晓
日期:2017-01-10 15:19:56JAVA
日期:2016-11-01 13:25:46C
日期:2016-10-25 16:01:4715-16赛季CBA联赛之八一
日期:2016-06-21 23:38:0815-16赛季CBA联赛之山东
日期:2016-05-12 12:49:54IT运维版块每日发帖之星
日期:2016-04-20 06:20:00IT运维版块每日发帖之星
日期:2016-03-22 06:20:00黄金圣斗士
日期:2015-11-24 10:43:13IT运维版块每日发帖之星
日期:2015-08-25 06:20:002015亚冠之德黑兰石油
日期:2015-08-22 22:55:54
3 [报告]
发表于 2010-06-18 11:25 |只看该作者
把awk放前面,通过管道给while
  1. ls -l |awk '{print $9,$5}' | while read filename filesize; do echo $filename $filesize; done
复制代码

论坛徽章:
0
4 [报告]
发表于 2010-06-18 13:15 |只看该作者
谢谢,可以了
但是不清楚为什么while read 可以从文件读取,不能从命令替换中读取

论坛徽章:
0
5 [报告]
发表于 2010-06-18 13:59 |只看该作者
回复 4# 338378WN8


要这样写:
while read filename filesize;do echo $filename $filesize ;done <<< $(ls -l|awk '{print $9,$5}')

找本书搜索herestring的用法就直到了

论坛徽章:
0
6 [报告]
发表于 2010-06-18 15:03 |只看该作者
尝试了一下
while read filename filesize; do echo $filename $filesize; done << end
> `ls -l |awk '{print $9,$5}'`
> end
这下可以了

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
7 [报告]
发表于 2010-06-18 16:43 |只看该作者
<<< here string
<< here document

?

论坛徽章:
0
8 [报告]
发表于 2010-06-18 16:53 |只看该作者
我遇到过这个问题,我当时的处理是把shell换成了ksh就可以执行成功了,不知道为什么。呵呵

论坛徽章:
0
9 [报告]
发表于 2012-09-12 10:25 |只看该作者
回复 6# 338378WN8


    也可以这样:
while read line;do
           。。。。。。。。。。                     
   done < <(  )

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
10 [报告]
发表于 2012-09-12 16:25 |只看该作者
1.
The words that the parser has marked as variable assignments (those preceding the command name) and redirections are saved for later processing.

2.

The words that are not variable assignments or redirections are expanded. If any words remain after expansion, the first word is taken to be the name of the command and the remaining words are the arguments.

3.

Redirections are performed as described above under REDIRECTION.

4.

The text after the = in each variable assignment undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable.

If no command name results, the variable assignments affect the current shell environment. Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment. If any of the assignments attempts to assign a value to a readonly variable, an error occurs, and the command exits with a non-zero status.
If no command name results, redirections are performed, but do not affect the current shell environment. A redirection error causes the command to exit with a non-zero status.

If there is a command name left after expansion, execution proceeds as described below. Otherwise, the command exits. If one of the expansions contained a command substitution, the exit status of the command is the exit status of the last command substitution performed. If there were no command substitutions, the command exits with a status of zero.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP