sh与csh中的算符优先顺序

• 本文约166字,阅读大致需要1分钟Cheatsheets

第一个脚本(1.sh):

#!/bin/sh
echo test
exit 1

测试命令:./1.sh | grep test && echo 1

csh:

./1.sh | grep test && echo e
test

sh:

./1.sh | grep test && echo e
test
e

原因

测试程序存在两个严重问题,导致csh和sh产生不同的结果:

  1. Unix系统假定程序正常结束时返回码为0。
  2. 测试脚本语义不明确。

如果将测试脚本修改为:

./1.sh | ( grep test && echo e )

则语义明确,因此csh和sh会得到一样的结果。

搜索文章…(按ESC退出)