LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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, 07: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, 09:02 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,024
Blog Entries: 5

Rep: Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789
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, 10: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 07:40 PM
bash: socket redirection? Thinking Linux - Software 1 04-15-2006 01:20 PM
Bash redirection and subshells nx5000 Programming 2 11-03-2005 07:13 AM
bash redirection "$ cat << EOF > file" (how does this work) ninmonkeys Linux - General 1 11-09-2004 04:37 PM
BASH scripting: confused about redirection & file descriptors funkymunky Programming 1 06-07-2004 08:47 AM


All times are GMT -5. The time now is 02:33 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration