便携式方法来检查shell变量的emptynes​​s [英] Portable way to check emptyness of a shell variable

查看:176
本文介绍了便携式方法来检查shell变量的emptynes​​s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是测试如果变量为空/未定义在shell脚本便携和规范的方法?它应该在所有的SH-炮弹一样工作。我现在做的是一样的东西:

What is the portable and canonical way to test if variable is empty/undefined in a shell script? It should work in all sh-like shells. What I do now is something like:

if [ -z "$var" ] ; then
    ...

和反向,做当变量不是空/未定义的内容:

and for reverse, doing something when variable is not empty/undefined:

if [ -n "$var" ] ; then
    ...

虽然这些工作我现在写的脚本,我想知道的方式,将在任何合理兼容的 SH 的样壳,甚至工作一些比较模糊的环境比Linux的PC与GNU用户态和庆典破折号

我要找的人谁是在不同的外壳环境经历,知道做事的方式不同,并不像应该工作的意见。

I'm looking for an "expert answer" from someone who is experienced in different shell environments and knows the pitfalls of different ways of doing things, not an opinion like "that should work".

推荐答案

免责声明:我不认为自己在弹专家

Disclaimer: I don't consider myself an expert in shells.

据我所知,在测试命令和 -z -n 标志是POSIX标准的一部分,所以你应该通过跨多个平台使用它们被罚款。

AFAIK, the test command and the -z and -n flags are part of the POSIX standard, so you should be fine by using them across many platforms.

这篇关于便携式方法来检查shell变量的emptynes​​s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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