ProfGhost
ProfGhost

Reputation: 359

KSH: Piping stderr and stdout into variable

I am trying to print stderr and stdout into one variable.
I want to check if rtest exists and capture the output, so i can exit the script if it isnt found.

checkdate=`rtest 2&>1`

The problem is that if i run my script stderr will still get printed on the terminal.

./script.ksh[26]: rtest:  not found

Upvotes: 0

Views: 754

Answers (1)

P.P
P.P

Reputation: 121387

The error message says that the script/executable rtest was not found in the PATH.

Give full path (or relative) to rtest and then run. Note that even if rtest is in current directory, you'll still get the same error (unless you have the current directory in your path).

Do:

checkdate=$(/path/to/rtest 2&>1)

or

checkdate=$(./rtest 2&>1) # if rtest is in current directory.

To check if rtest is in the PATH without executing it, you can use which command:

{ which rtest 2>&1 >/dev/null; } 2>&1 >/dev/null || { echo "rtest not found"; exit 1; }

Upvotes: 1

Related Questions