如何从shell指令检查返回值 [英] How to check return value from the shell directive

查看:495
本文介绍了如何从shell指令检查返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Makefile中,我需要测试当前目录是否为SVN存储库,如果不是,我想使用Makefile中的$(error)指令指示错误.

In my Makefile, I need to test if the current directory is an SVN repo or not and if it is not I want to indicate an error using the $(error) directive in Makefile.

所以我打算使用$(shell svn info)的返回值,但是我不确定如何从Makefile中获取此值.

So I plan to use the return value of $(shell svn info .) but I'm not sure how to get this value from within the Makefile.

注意:我不是想在配方中获取返回值,而是在Makefile的中间.

Note: I'm not trying to get the return value in a recipe, but rather in the middle of the Makefile.

现在我正在做这样的事情,它的工作仅仅是因为当出错时stdout为空白:

Right now I'm doing something like this, which works just because stdout is blank when it is an error:

SVN_INFO := $(shell svn info . 2> /dev/null)
ifeq ($(SVN_INFO),)
    $(error "Not an SVN repo...")
endif

我仍然想找出是否有可能在Makefile中获取返回值.

I'd still like to find out if it is possible to get the return value instead within the Makefile.

推荐答案

这对我来说很好-基于@eriktous'答案,对重定向的stdout进行了较小的修改,并且跳过了有效svn repo上svn信息的输出

This worked fine for me - based on @eriktous' answer with a minor modification of redirecting stdout as well to skip the output from svn info on a valid svn repo.

SVN_INFO := $(shell svn info . 1>&2 2> /dev/null; echo $$?)
ifneq ($(SVN_INFO),0)
    $(error "Not an SVN repo...")
endif

这篇关于如何从shell指令检查返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆