简体   繁体   English

KornShell中的测试命令(ksh)

[英]test command in KornShell (ksh)

I have a question on the test command in the KornShell (ksh). 我对KornShell(ksh)中的测试命令有疑问。 I know -ne is for comparing integers and != is for comparing strings. 我知道-ne用于比较整数而且!=用于比较字符串。 How will the test command behave if one argument is a string and the other is an integer? 如果一个参数是一个字符串而另一个是整数,那么测试命令将如何表现? I have below conditions in my code and both are working properly. 我的代码中有以下条件,两者都正常工作。

Code: 码:

myCount=1
myCount=`expr $myCount+ 0`
temp=`ps -aef | grep damn | wc -l`
if [ $temp -ne $myCount]; then
        echo ERROR Number
fi

if [ $temp != $myCount ]; then
        echo ERROR Strings
fi

Output: 输出:

ERROR Number
ERROR Strings

But your code is flawed anyway. 但无论如何你的代码都有缺陷。

temp= ps -aef | grep damn | wc -l temp = ps -aef | grep damn | wc -l ps -aef | grep damn | wc -l ps -aef | grep damn | wc -l will always return at least 1, since it will find the grep command as well as being a string padded with leading spaces, which is why both of your tests are true. ps -aef | grep damn | wc -l将始终返回至少1,因为它将找到grep命令以及填充前导空格的字符串,这就是为什么两个测试都为真。

Piping to wc is also unnecessary since the -c switch of grep will count for you. 管道到wc也是不必要的,因为grep的-c开关对你来说很重要。

better code would be: 更好的代码是:

temp= ps -aef |grep damn |grep -cv grep which will return the number of running instances of processes containing the damn string and it will be a number. temp = ps -aef |grep damn |grep -cv grep将返回包含该死的字符串的进程的运行实例数,它将是一个数字。

The type is not relevant because it's a simple text substitution. 该类型不相关,因为它是一个简单的文本替换。 In other words, the value of the variable $temp will be substituted in place of $temp (for example). 换句话说,变量$temp的值将替换为$temp (例如)。

At least for the version of ksh I'm running, for the numeric comparison, if the value starts with a non-numeric, it will equate to 0. If it starts with a numeric but contains non-numerics, you will get an error. 至少对于我正在运行的ksh版本,对于数值比较,如果值以非数字开头,则它将等于0.如果它以数字开头但包含非数字,则会出现错误。

For example: 例如:

$ export s1=xyz
$ export s2=7xyz
$ export i1=0
$ if [ $i1 -eq $s1 ]
> then
>     echo equal
> fi
equal
$ if [ $i1 -eq $s2 ]
> then
>     echo equal
> fi
ksh: 7xyz: bad number `7xyz'

However, based on your comments, that may not be the case for all versions of ksh . 但是,根据您的评论,对于所有版本的ksh ,情况可能并非如此。

Based on that, I would try to ensure that you use string comparisons for strings and numeric comparisons for numbers. 基于此,我将尝试确保您使用字符串比较进行字符串和数字的数字比较。 Anything else may be non-portable. 其他任何东西都可能是不便携的。

Using ksh93 and GNU coreutils expr 7.4 your command: 使用ksh93和GNU coreutils expr 7.4你的命令:

myCount=`expr $myCount+ 0`

gives me a syntax error and sets myCount to null which causes both if statements to output "ksh: [: argument expected" errors. 给我一个语法错误并将myCount设置为null,这导致两个if语句输出“ksh:[:argument expected”错误。 Try putting a space before the plus sign. 尝试在加号前加一个空格。 Also, there needs to be a space before ] . 此外,之前需要有一个空间]

You shouldn't need to convert myCount or temp to integers. 您不需要将myCounttemp转换为整数。 The coercion of myCount using expr is completely unnecessary. 使用expr强制myCount是完全没有必要的。

I prefer this form for comparing integers since it allows you to use symbolic comparison operators such as != and > instead of -ne and -gt : 我更喜欢这种形式来比较整数,因为它允许你使用符号比较运算符,如!=>而不是-ne-gt

if (( $temp != $myCount ))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM