我想用以下代码测试别名是否存在:
if( (get-alias ls) ) {
echo "=> Alias ls already exits."
} else {
echo "=> Alias ls does not exit."
}
运行时,它会显示:
CommandType Name Version Source
----------- ---- ------- ------
Alias ls -> Get-ChildItem
=> Alias ls already exits.
我尝试将命令的输出重定向到 $null :
if( (get-alias ls >$null) ) {
echo "=> Alias ls already exits."
} else {
echo "=> Alias ls does not exit."
}
我期望运行时有这个输出:
=> Alias ls already exits.
但是代码片段的结果发生了某种变化:
=> Alias ls does not exit.
如果我在第一组括号后重定向,我也会得到:
=> Alias ls does not exit.
为什么重定向会干扰结果?
If 测试是否有输出,而不是命令的结果码。