5

在我运行我输入的脚本之前

# export CPIC_MAX_CONV=500

下面是test1.script文件

#!/bin/bash

function cpic () {
  var="`export | grep -i "CPIC_MAX_CONV" | awk '/CPIC_MAX_CONV/ { print $NF } '`"
  [[ $var=="" ]] && (echo "Empty String <<")
  [[ $var!="" ]] && (echo "$CPIC_MAX_CONV")
  echo "$var" ;
}

cpic

输出是:

# test1.script  ---- Me running the file

Empty String <<
500
CPIC_MAX_CONV="500"

无论我使用什么 "" 或 '' 或 [ 或 [[ 结果都是一样的。该CPIC_MAX_CONV变量由上述脚本找到。

我在 Linux/CentOS 6.3 上运行它。

这个想法很简单:查找是否CPIC_MAX_CONV在环境中定义并返回它的值。如果有一个空白空间,那么该变量当然不存在于系统中。

4

4 回答 4

4

为什么你总是说实话?让我们先在你的终端上玩一点:

$ [[ hello ]] && echo "True"

你认为输出是什么?(试试看!)还有以下内容?

$ [[ "" ]] && echo "True"

(试试看!)。

好吧,看来非空字符串等价于真表达式,而空字符串(或未设置的变量)等价于假表达式。

您所做的是以下内容:

[[ $var=="" ]]

[[ $var!="" ]]

所以你给了一个非空字符串,这是真的!

为了执行测试,您实际上需要标记之间的空格:

[[ $var == "" ]]

反而。现在,您的测试最好写成:

if [[ -z "$var" ]]; then
    echo "Empty String <<"
else
    echo "$CPIC_MAX_CONV"
fi

(没有子外壳,只有一个测试)。

关于你的脚本风格还有很多话要说。没有冒犯,我会说这真的很糟糕:

  • 不要使用反引号!请改用$(...)构造。因此:

    var="$(export | grep -i "CPIC_MAX_CONV" | awk '/CPIC_MAX_CONV/ { print $NF } ')"
    
  • 不要function blah用来定义函数。你的函数应该被定义为:

    cpic () {
        local var="$(export | grep -i "CPIC_MAX_CONV" | awk '/CPIC_MAX_CONV/ { print $NF } ')"
        if [[ -z "$var" ]]; then
            echo "Empty String <<"
        else
            echo "$CPIC_MAX_CONV"
        fi
    }
    

哦,我使用了local关键字,因为我猜你不会var在函数之外使用变量cpic

现在,函数的目的是什么cpic,特别是你定义变量的东西var?这很难描述(因为有很多你没有想到的案例)。(顺便说一句,你grep在这里似乎真的没用)。以下是您忽略的几个案例:

  • 导出的变量被命名somethingfunnyCPIC_MAX_CONVsomethingevenfunnier
  • 导出的变量在CPIC_MAX_CONV某处包含字符串,例如,

    export a_cool_variable="I want to screw up Randhawa's script and just for that, let's write CPIC_MAX_CONV somewhere here"
    

好的,我不想描述你的行到底在做什么,但我猜你的目的是知道变量是否CPIC_MAX_CONV已设置并标记为导出,对吗?在这种情况下,您最好这样做:

cpic () {
    if declare -x | grep -q '^declare -x CPIC_MAX_CONV='; then
        echo "Empty String <<"
    else
        echo "$CPIC_MAX_CONV"
    fi
}

它将更有效,更健壮。

哦,我现在正在阅读您的帖子的结尾。如果您只想判断变量CPIC_MAX_CONV是否已设置(设置为某个非空值 - 似乎您不在乎它是否标记为导出,如果我错了请纠正我),它甚至更简单(它会是效率更高):

cpic () {
    if [[ "$CPIC_MAX_CONV" ]]; then
        echo "Empty String <<"
    else
        echo "$CPIC_MAX_CONV"
    fi
}

也会这样做!

于 2012-12-24T20:58:51.830 回答
1

Do you really care whether CPIC_MAX_CONV is an environment variable versus just 'it is a variable that might be an environment variable'? Most likely, you won't, not least because if it is a variable but not an environment variable, any script you run won't see the value (but if you insist on using aliases and functions, then it might matter, but still probably won't).

It appears, then, that you are trying to test whether CPIC_MAX_CONV is set to a non-empty value. There are multiple easy ways to do that — and then there's the way you've tried.

: ${CPIC_MAX_CONV:=500}

This ensures that CPIC_MAX_CONV is set to a non-empty value; it uses 500 if there previously wasn't a value set. The : (colon) command evaluates its arguments and reports success. You can arrange to export the variable after it is created if you want to with export CPIC_MAX_CONV.

If you must have the variable set (there is no suitable default), then you use:

: ${CPIC_MAX_CONV:?}

or

: ${CPIC_MAX_CONV:?'The CPIC_MAX_CONV variable is not set but must be set'}

The difference is that you can use the default message ('CPIC_MAX_CONV: parameter null or not set') or specify your own.

If you're only going to use the value once, you can do an 'on the fly' substitution in a command with:

cpic_command -c ${CPIC_MAX_CONV:-500} ...

This does not create the variable if it does not exist, unlike the := notation which does.

In all these notations, I've been using a colon as part of the operation. That enforces 'null or not set'; you can omit the colon, but that allows an empty string as a valid value, which is probably not what you want. Note that a string consisting of just a blank is 'not empty'; if you need to validate that you've got a non-empty string, you have to work a little harder.


I'm not dissecting your misuse of the [[ command; gniourf_gniourf has provided an excellent deconstruction of that, but overlooked the simpler notations available to do what seems to be the job.

于 2012-12-24T21:48:17.680 回答
0

尝试这个:

#!/bin/bash
function cpic () {    
  var="`export | grep -i "CPIC_MAX_CONV"`"
  [ "$var" = "" ] && (echo "Empty String <<")
  [ "$var" != "" ] && echo "$CPIC_MAX_CONV"
}  
cpic
于 2012-12-25T19:03:26.927 回答
-1

你需要在你的条件空间。

#!/bin/bash

function cpic () {
    var="`export | grep -i "CPIC_MAX_CONV" | awk '/CPIC_MAX_CONV/ { print $NF } '`"
    [[ $var == "" ]] && (echo "Empty String <<")
    [[ $var != "" ]] && (echo "$CPIC_MAX_CONV")
    echo "$var" ;
}

cpic
于 2012-12-24T22:02:02.250 回答