【发布时间】:2013-12-05 14:40:37
【问题描述】:
有人有一个“聪明”的想法,您应该能够在任何地方使用输出重定向。
这意味着这两个是等价的:
# echo expect > more friends
# cat more
expect friends
# rm more
# echo expect friends > more
# cat more
expect friends
# rm more
如果你能逃脱它就不会那么糟糕了,而且你确实可以:
# echo "expect > more friends"
expect > more friends
# echo 'expect > more friends'
expect > more friends
事实上,你甚至可以写这样的东西,它会按预期工作:
# echo '{ print $1 > 10 }'
{ print $1 > 10 }
现在让我们用 awk 试试:
# echo 5 | awk '{ print $1 > 10 }'
# cat 10
5
# rm 10
当然。现在,如果有人能告诉我这有什么意义......
但让我们尝试其他变体:
# echo 5 | awk "{ print $1 > 10 }"
# cat 10
5
# rm 10
# echo 5 | awk '{ print $1 \> 10 }'
awk: 1: unexpected character '\'
# echo 5 | awk '{ print $1 \\> 10 }'
awk: 1: unexpected character '\'
为什么要 bash?为什么?
无论如何,有一个解决方法,使用 完全可以做到:
# echo 5 | awk '{ print 10 < $1 }'
0
但又是为什么?
【问题讨论】:
标签: bash shell redirect awk escaping