There isn't an easy way (that I know of) to handle the equivalent of 2>&1
and capture stderr as well as stdout, particularly if you want the exit status too. Piping both to cat
will merge the output streams, though:
#!/usr/bin/csh
set my_tmp = ~/.msg.$$.tmp
set my_msg = `( /myscriptpath/myscript.any; echo $? >"$my_tmp" ) |& cat`
set my_ret = `cat "$my_tmp"`
rm -f "$my_tmp"
echo "ret=$my_ret, msg=$my_msg"
Or alternatively, if you are running in an environment such as a Linux-based system where /dev/stout
exists, assign my_msg
like this:
set my_msg = `/myscriptpath/myscript.any >& /dev/stdout`
A simpler variation on command |& cat
is to shell out to /bin/sh
and use its redirections capability. The resulting csh
block would then be this, with no need for $my_tmp
,
#!/usr/bin/csh
set my_msg = `sh -c '/myscriptpath/myscript.any 2>&1'`
set my_ret = $status
echo "ret=$my_ret, msg=$my_msg"