0

What I would want:

#!/bin/csh
# call /myscriptpath/myscript.any and redirect 2>&1 and get retval and output
set my_msg = `/myscriptpath/myscript.any` 
set my_ret = `echo $?`
  • How to capture the exit code from myscript.any?
  • How to capture the message from myscript.any, if possible?

/myscriptpath/myscript.any This works as expected called from bash command line.

0

1 Answer 1

1

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"
1
  • On systems that have those, you can do set out_and_err = `cmd >& /dev/stdout` Commented Sep 21, 2023 at 19:53

You must log in to answer this question.

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