LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2009, 06:00 AM   #1
jaziniho
LQ Newbie
 
Registered: May 2009
Posts: 2

Rep: Reputation: 0
trouble with file redirection in bash


Hi all, (First post, so, greetings!)

I'm having some trouble with redirection in bash.

I'm trying to redirect stderr to a file, then later redirect stderr to a different file. I thought this was just a case of (re)redirecting the filehandle 2 to the new file, but it seems I've misunderstood.

Here's a code sample showing what I'm trying to do:



###############################
####The main script (local)####
###############################

#!/bin/bash

CONNECT1="user@machine"
CONNECT2="user@machine2"

ssh $CONNECT1<<MYEND 2> ~/output-machine1
/tmp/remote-script
MYEND

ssh $CONNECT2<<MYEND 2> ~/output-machine2
/tmp/remote-script
MYEND




#######################
#### remote-script ####
#######################

#!/bin/bash

# get some output to stdout (/tmp/jaziniho does exist)
ls /tmp/jaziniho

# get some output to stderr
ls /does/not/exist



Output I get back:

to stdout:
first the list of files in /tmp/jaziniho on machine1
second the list of files in /tmp/jaziniho on machine2

for redirected stderr:
$cat output-machine1
ls: /does/not/exist: No such file or directory

However, file output-machine2 is never created, the output from stderr for the second machine is lost.

Can anyone explain what's wrong?
 
Old 05-08-2009, 08:02 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
I don't see any obvious issues so it leads me to believe you are not getting any errors on the CONNECT2. Have you done a side by side comparison of /tmp/remote-script on machine and machine2? If you accidentally left out the "ls /does/not/exist" on the latter's remote-script it might not be producing errors. (Or if you had create /does/not/exist on machine2 earlier it might be that the ls is working.)

That is to say you're assuming there is an error and perhaps there isn't one.
 
Old 05-08-2009, 09:22 AM   #3
jaziniho
LQ Newbie
 
Registered: May 2009
Posts: 2

Original Poster
Rep: Reputation: 0
*sigh*

I found a typo in my simplified test script, it actually worked in reality. (both were redirecting to output-machine1)

as for the real world version, I found a redirect to /dev/null buried deep in the heart of it.

sorry! thanks for taking the time and interest nonetheless.
 
  


Reply

Tags
bash, descriptors, file, pipes, redirection, stderr, stdout



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash:redirection to a variable, not file stabu Programming 2 08-11-2008 06:40 PM
bash: socket redirection? Thinking Linux - Software 1 04-15-2006 12:20 PM
Bash redirection and subshells nx5000 Programming 2 11-03-2005 06:13 AM
bash redirection "$ cat << EOF > file" (how does this work) ninmonkeys Linux - General 1 11-09-2004 03:37 PM
BASH scripting: confused about redirection & file descriptors funkymunky Programming 1 06-07-2004 07:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration