sec*_*ecr 83 shell cross-platform negate
我正在寻找一个简单但跨平台的否定过程来否定过程返回的价值.它应该将0映射到某个值!= 0和任何值!= 0到0,即以下命令应返回"yes,nonexistingpath不存在":
ls nonexistingpath | negate && echo "yes, nonexistingpath doesn't exist."
Run Code Online (Sandbox Code Playgroud)
的! - 运算符很棒但很遗憾不是shell独立的.
Jon*_*ler 86
以前,答案是作为最后一节介绍现在的第一部分.
!运算符为了解决其他问题,我最近(2015年9月)注意到POSIX shell支持!运算符.例如,它被列为保留字,并且可以出现在管道的开头- 其中一个简单的命令是"管道"的特殊情况.因此,它也可以在if语句和/ while或until循环中使用 - 在符合POSIX的shell中.因此,尽管我有所保留,它可能比我在2008年意识到的还要广泛.对POSIX 2004和SUS/POSIX 1997的快速检查表明,!这两个版本都有.
请注意,!运算符必须出现在管道的开头,并否定整个管道的状态代码(即最后一个命令).这里有些例子.
# Simple commands, pipes, and redirects work fine.
$ ! some-command succeed; echo $?
1
$ ! some-command fail | some-other-command fail; echo $?
0
$ ! some-command < succeed.txt; echo $?
1
# Environment variables also work, but must come after the !.
$ ! RESULT=fail some-command; echo $?
0
# A more complex example.
$ if ! some-command < input.txt | grep Success > /dev/null; then echo 'Failure!'; recover-command; mv input.txt input-failed.txt; fi
Failure!
$ ls *.txt
input-failed.txt
Run Code Online (Sandbox Code Playgroud)
在Bourne(Korn,POSIX,Bash)脚本中,我使用:
if ...command and arguments...
then : it succeeded
else : it failed
fi
Run Code Online (Sandbox Code Playgroud)
这是便携式的.'命令和参数'可以是管道或其他复合命令序列.
not命令'!' 运算符,无论是内置于shell还是由o/s提供,都不是普遍可用的.尽管如此,写起来并不是很难 - 下面的代码可以追溯到至少1991年(尽管我认为我之前写的版本更早).但是,我不倾向于在我的脚本中使用它,因为它不可靠.
/*
@(#)File: $RCSfile: not.c,v $
@(#)Version: $Revision: 4.2 $
@(#)Last changed: $Date: 2005/06/22 19:44:07 $
@(#)Purpose: Invert success/failure status of command
@(#)Author: J Leffler
@(#)Copyright: (C) JLSS 1991,1997,2005
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "stderr.h"
#ifndef lint
static const char sccs[] = "@(#)$Id: not.c,v 4.2 2005/06/22 19:44:07 jleffler Exp $";
#endif
int main(int argc, char **argv)
{
int pid;
int corpse;
int status;
err_setarg0(argv[0]);
if (argc <= 1)
{
/* Nothing to execute. Nothing executed successfully. */
/* Inverted exit condition is non-zero */
exit(1);
}
if ((pid = fork()) < 0)
err_syserr("failed to fork\n");
if (pid == 0)
{
/* Child: execute command using PATH etc. */
execvp(argv[1], &argv[1]);
err_syserr("failed to execute command %s\n", argv[1]);
/* NOTREACHED */
}
/* Parent */
while ((corpse = wait(&status)) > 0)
{
if (corpse == pid)
{
/* Status contains exit status of child. */
/* If exit status of child is zero, it succeeded, and we should
exit with a non-zero status */
/* If exit status of child is non-zero, if failed and we should
exit with zero status */
exit(status == 0);
/* NOTREACHED */
}
}
/* Failed to receive notification of child's death -- assume it failed */
return (0);
}
Run Code Online (Sandbox Code Playgroud)
当它无法执行命令时,它返回"成功",与失败相反.我们可以辩论"无所事事"选项是否正确; 也许它应该在没有被要求做任何事情时报告错误.' "stderr.h"'中的代码提供了简单的错误报告功能 - 我在任何地方都使用它.根据要求提供源代码 - 请参阅我的个人资料页面与我联系.
Jay*_*rod 39
在Bash中,使用!命令前的操作符.例如:
! ls nonexistingpath && echo "yes, nonexistingpath doesn't exist"
Run Code Online (Sandbox Code Playgroud)
Edu*_*rch 17
你可以尝试:
ls nonexistingpath || echo "yes, nonexistingpath doesn't exist."
Run Code Online (Sandbox Code Playgroud)
要不就:
! ls nonexistingpath
Run Code Online (Sandbox Code Playgroud)
如果某种情况发生你没有Bash作为你的shell(例如:git脚本或puppet exec测试)你可以运行:
echo '! ls notexisting' | bash
- > retcode:0
echo '! ls /' | bash
- > retcode:1
小智 7
没有 !、没有 subshell、没有 if 的解决方案,并且至少应该在 Bash 中工作:
ls nonexistingpath; test $? -eq 2 && echo "yes, nonexistingpath doesn't exist."
# Alternatively without an error message and handling of any error code > 0
ls nonexistingpath 2>/dev/null; test $? -gt 0 && echo "yes, nonexistingpath doesn't exist."
Run Code Online (Sandbox Code Playgroud)
! ls nonexistingpath && echo "yes, nonexistingpath doesn't exist."
Run Code Online (Sandbox Code Playgroud)
或者
ls nonexistingpath || echo "yes, nonexistingpath doesn't exist."
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32772 次 |
| 最近记录: |