Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash-eq的计算结果为true?_Bash_Loops - Fatal编程技术网

Bash-eq的计算结果为true?

Bash-eq的计算结果为true?,bash,loops,Bash,Loops,有人能帮我弄清楚为什么下面的代码会导致下面的输出吗?特别是当/etc/passed中没有匹配的字符串时 代码: 输出: + for user in '${usernames[*]}' + egrep addaf /etc/passwd + echo -e '\nChecking if users already exist...' Checking if users already exist... + '[' 0 -eq 0 ']' + echo -e 'addaf exists!, skip

有人能帮我弄清楚为什么下面的代码会导致下面的输出吗?特别是当/etc/passed中没有匹配的字符串时

代码:

输出:

+ for user in '${usernames[*]}'
+ egrep addaf /etc/passwd
+ echo -e '\nChecking if users already exist...'
Checking if users already exist...
+ '[' 0 -eq 0 ']'
+ echo -e 'addaf exists!, skipping creation'
addaf exists!, skipping creation

您正在检查echo的退出代码,这总是成功的

无论如何,惯用的方法是

if grep -Eq "$user" /etc/passwd; then
    ...

通常,您几乎不需要检查$?明确地。

您正在检查echo的退出代码,这总是成功的

无论如何,惯用的方法是

if grep -Eq "$user" /etc/passwd; then
    ...
通常,您几乎不需要检查$?明确地说