0

I know that I can redirect the stdout to an environment variable, e.g.

retcode=$(/usr/bin/osascript -e 'do shell script ... with administrator privileges')

but how do redirect the stderr output to an environment variable, too (in this example to detect a user cancelling the admin credentials dialog)?

1 Answer 1

1

This is the most simple way to redirect stdout and stderr into one variable:

output=$(/usr/bin/osascript -e 'do shell script ... with administrator privileges' 2>&1)
retcode=$?

It's getting more complicated, if you want to catch stdout and stderr separately. I think the only straight-forward way is to redirect stdout/stderr to separate files:

/usr/bin/osascript -e 'do shell script ... with administrator privileges' >/tmp/$$.stdout 2>/tmp/$$.stderr

retcode=$?
output=$(cat /tmp/$$.stdout)
stderr=$(cat /tmp/$$.stderr)

Make sure you clean up the files later.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .