Bash-eq的计算结果为true?
有人能帮我弄清楚为什么下面的代码会导致下面的输出吗?特别是当/etc/passed中没有匹配的字符串时 代码: 输出: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
+ 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
...
通常,您几乎不需要检查$?明确地说