Operating System - HP-UX
1833827 Members
2285 Online
110063 Solutions
New Discussion

Re: How to redirection Standard error to an env variable instead of a file

 
SOLVED
Go to solution
yyghp
Super Advisor

How to redirection Standard error to an env variable instead of a file

I am writing a script used for quite a bunch of users. I use the command "ldappasswd" in the script for the user to change their password. While the errors are not friendly to the users, such as "ldap_bind: Invalid credentials" if they type the wrong old password.
I would like to redirect such error to an environment variable, and compare the error strings, then echo some friendly words to the standard output.
I know I can redirect the error to a file, like:
ldappasswd 2>errorfile
But is it possible to redirect the standard error to a variable ?
Thanks!
5 REPLIES 5
Michael Schulte zur Sur
Honored Contributor

Re: How to redirection Standard error to an env variable instead of a file

Hi,

try ldappasswd 2>&1 >/dev/null

greetings,

Michael
Senthil Kumar .A_1
Honored Contributor

Re: How to redirection Standard error to an env variable instead of a file

Hi yyghp,

if the script you are using doesnot throw up any output message and only error output..
u could write a script like this...

------------------------------------
#!/usr/bin/sh
ERR=`/path/ldappasswd 2>&1`
if [ ERR .....form here on your logic...

------------------------------------

The character around /path/ldappasswd 2>&1 is a back quote ,the one u find before "key 1" in KB.

NOTE:
But mind you, if the error message is multilined..the "\n" enter character will be stripped off and made into single line and put in the variable.
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
Muthukumar_5
Honored Contributor

Re: How to redirection Standard error to an env variable instead of a file

Yes. But preferred way is as like,

ldappasswd | grep -q "ldap_bind: Invalid credentials"
if [[ $? -eq 0 ]]
then
echo "action got error"
else
echo "ok"
fi

grep -q will be used to search string in silent mode.

hth.
Easy to suggest when don't know about the problem!
Michael Schulte zur Sur
Honored Contributor
Solution

Re: How to redirection Standard error to an env variable instead of a file

Oups,

I copyied only half of what I wanted.
That should have been
ERR=`ldappasswd 2>&1 >/dev/null`
which is roughly the same as that from senthil.
If you need the stdout too then you can redirect stderr to file and read it into a variable.
ldappasswd args 2>errorlog
ERR=`cat errorlog`

Michael
Sandman!
Honored Contributor

Re: How to redirection Standard error to an env variable instead of a file

command substitution will let you get at what you want...

# ERR=$(ldappasswd 2>&1)
OR
# ERR=`ldappasswd 2>&1`

both these forms should work.