I'm writing a Bash Script and in it I execute a piped command within a Function I wrote and I can't seem to redirect the
stderr from the 1st pipe to stdout..?
I'm setting the output to an Array "COMMAND_OUTPUT" and splitting on newlines using this --> "( $(...) )". By putting
the extra ( ) around the $( ), it splits the output using the IFS and sets the array elements.
Then the command after the semi-colon echos BOTH Return Codes from each command using the PIPESTATUS Array
Variable to STDOUT. which works perfectly getting the Return Codes.
Here's the Command:
I've tried adding "2>&1" to each point in the Command within "$(..)", but none seem to send the 'cat' command's STDERR
to STDOUT in order to capture the error inside the Array "COMMAND_OUTPUT".
If I set $SEND_FILE to a file that doesn't Exist, I can see that everything gets stored in the COMMAND_OUTPUT Array
except for "cat: : No such file or directory". Which get printed immediately when that line is executed.
Any ideas on how to redirect the stderr from the 1st pipe command to stdout? Any thoughts would be much appreciated!
Subshell is great for low effort logging "(...) >>log_file 2>&1" as it allows you to concatenate stdout and or stderr on multiple commands and split stdin if you use whole-line or all-one-shell commands, like line, below. The 'tail -1' would not work, for the stdin FILE* buffer would swallow the second and some following line parts that would be lost when tail exited, but 'line' does a lot of read(0,*buf,1) until it finds a linefeed, so the second line is still left within/behind the fd, which is inherited, not the FILE*.
If I move the command to a new Function and simply execute the command only inside the function and then set the function call to a variable
and redirect to stderr from the function call, it will then redirect stderr to stdout.
Like this:
Now this way sends STDERR to STDOUT and the Array now holds all the data (including stderr)...
Is this a bad way to do this, or is that ok to do?
Thanks Again,
Matt
---------- Post updated at 05:11 PM ---------- Previous update was at 05:09 PM ----------
Hey DGPickett, thanks for the reply!
Cool, thanks for the example... Much appreciated!
I was about to head out for the day, I'll probably pick back up on this tomorrow or Monday... Need to clear my head a bit!
Thanks guys for ALL your replies!
I'll be sure to give BGPickett's example a try.
I had to remove the 2nd "2>&1" that was right after the 'cat' Command in order to get its STDERR to be captured by the
array "COMMAND_OUTPUT"... But other then that it worked PERFECTLY..!
Final code to capture both Command's STDERR & STDOUT into an Array:
Thanks again everybody for all your suggestions!
Hello,
I unfortunately have a process that does two things, it returns an answer to me and then does a bunch of work that I would like to wait on. Here is a simple example:
#!/bin/bash
function p_w {
echo "poopy"
sleep 10
echo "scoop"
}
foo=$(p_w &)
sleep 1
echo "1... (7 Replies)
Hello,
We have an oracle database running on a Linux host (RHEL5)...I'm trying to run Oracle dbv (database verify utility) and capture its output to a file using the following syntax but the standart output does NOT get redirected to the file...
dbv blocksize=32768 ... (2 Replies)
I need to capture the vmstat output of a server every 5 minutes, in a text filename with the name in the format vmoutput. yesterday's date.txt. I need to get the vmstat o/p for the whole day with 5 minutes interval and send it (preferably ftp) to my local desktop folder.
eg: vmstat 300... (1 Reply)
Hi all, I'd like to capture the output from the 'top' command to monitor my CPU and Mem utilisation.Currently my command isecho date
`top -b -n1 | grep -e Cpu -e Mem` I get the output in 3 separate lines.Tue Feb 24 15:00:03
Cpu(s): 3.4% us, 8.5% sy .. ..
Mem: 1011480k total, 226928k used, ....... (4 Replies)
Hi,
I'm new to ksh - unix platform. I'm writing a small script which will search my current directory and will search for file names which it takes input from the users.
Here is the code I'm having.
1 #!/bin/ksh
2 echo "enter a file name to be searched in the current dir : "
3 read... (1 Reply)
Hi
I have a C++ program that generates a lot of log information on the console, I need this output (printed using printf function) to go to a file since I will use crontab to schedule the job.
I know I can do this:
myprog > myfile
but I don't know how to enter this in crontab.
I use... (3 Replies)
I have the following line in my script:
$sftpcmd $rmthost <<COMMANDS>> $sftplog 2>&1
For some reason this is not capturing the errors from sftp, they go to the file attached to the cron entry
ie
mm hh dd MM * /myscript > cron.out
any idea why?
digital unix 4.0d (6 Replies)
A few years ago I took a C programming class and used both Linux and FreeBSD as my operating systems and gcc as the compiler. I ran a Command-line utility to to capture what happened as I ran the code, the output was saved to a file that I specified when I ran the utility command (that I can't... (1 Reply)
Hi,
I have an expression using grep and nawk that captures the ID number of a given Unix process. It gets printed to screen but I don't know how to declare a variable to this returned value!
For example,
ps -ef|grep $project | grep -v grep | nawk '{print $2}'
This returns my number. How... (2 Replies)