简体   繁体   中英

Hide error language from bash shell script

I have a simple shell script that tests whether a password is correct (or not) against an Apple Open Directory (LDAP) server.

What I'd like is simply to lose the gobblygook error message "Authentication for node /LDAPv3/1..." and instead insert my own language such as "password does not match".

Here's what happens now:

bash-3.2# test-password 

Enter username you'd like to test password for:
jdoe

Enter Password to test for jdoe
asdasdasd
Authentication for node /LDAPv3/127.0.0.1 failed. (-14090, eDSAuthFailed)
<dscl_cmd> DS Error: -14090 (eDSAuthFailed)

What I'd prefer is:

bash-3.2# test-password 

Enter username you'd like to test password for:
jdoe

Enter Password to test for jdoe
asdasdasd
 Password matches!

So I just need to know a way so that the std out error message is muffled...

Here's the script:

#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH
echo
echo Enter username you\'d like to test password for:
read USERNAME

echo
echo Enter Password to test for "$USERNAME"
read PASSWORD
/usr/bin/dscl /LDAPv3/127.0.0.1 auth $USERNAME $PASSWORD

if [ "$?" = "0" ]; then
       echo "Password is correct"
       exit 0

fi

It depends on where the error message is being written. Well-behaved UNIX programs write their errors to stderr.

/usr/bin/dscl /LDAPv3/127.0.0.1 auth $USERNAME $PASSWORD 2> /dev/null  # stderr
/usr/bin/dscl /LDAPv3/127.0.0.1 auth $USERNAME $PASSWORD  > /dev/null  # stdout
/usr/bin/dscl /LDAPv3/127.0.0.1 auth $USERNAME $PASSWORD &> /dev/null  # both

For what it's worth, you can combine this with the if statement. Also, no need for the explicit /usr/bin path in front.

if dscl /LDAPv3/127.0.0.1 auth $USERNAME $PASSWORD 2> /dev/null; then
    : # success
else
    : # failure
fi

Just throw away standard output and error:

/usr/bin/dscl /LDAPv3/127.0.0.1 auth $USERNAME $PASSWORD >/dev/null 2>&1

Unless the dscl program does something devious like open up your /dev/tty device for writing, that should take care of everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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