loops - Bash -eq evaluates to true? -


can me figure out why code below results in output below? when there no string in /etc/passed matches?

code:

for user in ${usernames[*]} egrep ${user} /etc/passwd >/dev/null echo -e "\nchecking if users exist..." if [ $? -eq 0 ]; echo -e "${user} exists!, skipping creation" 

output:

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

you examining exit code echo, success.

anyway, idiomatic way is

if grep -eq "$user" /etc/passwd;     ... 

generally, should never need examine $? explicitly.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -