Operating System - HP-UX
1837764 Members
3633 Online
110119 Solutions
New Discussion

Re: Redirecting a result ...

 
SOLVED
Go to solution
Manuales
Super Advisor

Redirecting a result ...

Hi ..
how can i redirect an output ..
for example,
i have script run a script , this script uses a command named fdx .. this will output a result
i want this result to keep in a file ..

script send_information.sh contains:
/home/usr1/send.sh >> /tmp/log.txt

and file send.sh contains:
fdx -u inf_bank.txt $URL

but when i run send_information.sh , the output is not kept in the file indicated: log.txt

why ..
do i must put 2 before >> this do not work !!!

Thanks Manuales.

17 REPLIES 17
Sandman!
Honored Contributor
Solution

Re: Redirecting a result ...

Hi Manuales,

Make sure your script is working and not erring out. What you have is good since ">>" is the symbol for re-directing & appending stdout to a file. Try running it in debug mode as...

# sh -x /home/usr1/send.sh

cheers!
Jeff_Traigle
Honored Contributor

Re: Redirecting a result ...

Offhand, I would guess fdx is writing to stderr, which you are not redirecting. If you want stderr redirected to that file also, then you need to add that like this:

/home/usr1/send.sh >> /tmp/log.txt 2>>&1
--
Jeff Traigle
Manuales
Super Advisor

Re: Redirecting a result ...

:0(

it does not work !!!!!!!

when i run it with sh -x ...
in the output appears:

+ /home/usr1/send.sh
+ 1>>/tmp/log.txt
.....
.....
.....


and the output i can not keep it into file log.txt !!!!

how do i must use & ?

Thanks, Manuales.
Manuales
Super Advisor

Re: Redirecting a result ...


friend ..
with /home/usr1/send.sh >> /tmp/log.txt 2>>&1
what means 2>>&1 ?

i do not understand &1 ..

thanks, Manuales.
Sandman!
Honored Contributor

Re: Redirecting a result ...

Hi Manulaes,

Looks like the script is erring out and hence you see stderr on your teminal and not in the file. Redirect stderr also to the file and post the output here:

/home/usr1/send.sh >> /tmp/log.txt 2>>&1

cheers!
Sandman!
Honored Contributor

Re: Redirecting a result ...

Manuales,

&1 means re-directing stderr to the same place as stdout. The file descriptor 1 stands for standard output and 2 stands for standard error and the notation 2>>&1 means re-direct stderr (2) to the same place as stdout (1) which in your case is "/tmp/log.txt"

cheers!
Manuales
Super Advisor

Re: Redirecting a result ...

nop ...
this was resulted:

"Ambiguous output redirect."


help me !!!
it is not posible an easy thing be dificult for me !!! :'(

HELP ....
SOS ....

Thanks ....
James R. Ferguson
Acclaimed Contributor

Re: Redirecting a result ...

Hi Manuales:

I believe, too, that Jeff is correct. That is, your "send.sh" is probably writting the information you want to STDERR and not STDOUT.

STDERR is file descriptor-2 whereas STDOUT is file descriptor-1.

The shell opens these descriptors for you.

In a shell, a simple 'echo' or 'print' writes to STDOUT. A 'print -u2' statement writes to STDERR.

If you want to capture *both* STDOUT and STDERR into the same file, you do:

... 1>>file 2>&1

...which means take the output of STDOUT (1) and redirect it to 'file'. At the same time, assign STDERR (2) to file descriptor #1 (which is now 'file'). Thus, *both* streams are redirected to your 'file'.

The ">>" notation means "append", whereas a single ">" means create or truncate to zero-length and then begin writting.

See the man pages for 'sh-posix'.

Regards!

...JRF...
John Dvorchak
Honored Contributor

Re: Redirecting a result ...

In UNIX the standard error is 2 and the standard output is 1. So if you use 2>&1 >> /tmp/file you are telling the system to redirect standard error to standard out and >> redirect standard out (plus the standard error) to the /tmp/file and >> means append it to the bottom of the /tmp/file.

I hope this clears up the 2>&1 for you.
If it has wheels or a skirt, you can't afford it.
Manuales
Super Advisor

Re: Redirecting a result ...

nop ...
does not work ...
o.k., i have to be patient ..

script send_information.sh contains:
/home/usr1/send.sh >> /tmp/log.txt

and file send.sh contains:
fdx -u inf_bank.txt URL

If i run as follows
send.sh

the output a can see it on line ...

why??
SOS !!!

James R. Ferguson
Acclaimed Contributor

Re: Redirecting a result ...

Hi Manuales:

Try this at your command line:

# /home/user1/send.sh 2> /dev/null

Do you still see all of the output you did before?

Regards!

...JRF...
Jeff_Traigle
Honored Contributor

Re: Redirecting a result ...

Is your script actually written with POSIX shell, as the .sh would typically indicate? One reference I see to "Ambiguous output redirect" with a Yahoo! search is trying to use POSIX style redirects as we've shown above in csh or tcsh, which does not understand such syntax.

Another mentions not being able to nest redirections like "(eval '(echo matter >fullness)') >void". Maybe you're redirecting stdout of send_information.sh also? That would cause problems.

BTW, I added an extra < in my previous post. The proper syntax to redirect stderr to stdout, appending to file, is:

script >> file 2>&1
--
Jeff Traigle
Manuales
Super Advisor

Re: Redirecting a result ...

Yeap ... :'(
I continuew seeing the output ..

with follows:

send.sh 1>/dev/null
send.sh 2>/dev/null
send.sh >/dev/null

i see result on line ...

fdx is a command like ftp ...

fdx indicates "Trying 111.222.333.444
Connected...."
this output i require keeping ...

Help me!!
Manuales.


Manuales
Super Advisor

Re: Redirecting a result ...

My user uses csh
at the beggining of the script i'm using
#!/usr/bin/csh

env command:
SHELL=/usr/bin/csh

and script has at the end: ".sh"
do i must put ".csh"?

Manuales.
James R. Ferguson
Acclaimed Contributor

Re: Redirecting a result ...

Hi Manuales:

Yikes! Using the 'csh' shell leads to brain-damage! Please read this:

http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Redirecting a result ...

Hi Manuales:

You are using the 'csh' (C-shell) because your interpreter line (The "sheebang" that begins with #!) is "#!/usr/bin/csh".

That, and that alone governs the shell that will be run.

What you name the script is immaterial. You can call the script "script.sh" or merely "script". This is not evaluated when a determination of what to run is made by the kernel. What is, is the interpreter name following the sheebang.

Even though your user uses a 'csh' *your* script can still be written as a Poxix shell and call any other script or binary to execute. Then, the methods for redirecting output shown above can easily be made to work in *your* script.

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: Redirecting a result ...

Hi,

if you live in a (t)csh environment:

/home/usr1/send.sh >>& /tmp/log.txt

will redirect the stdout AND errout to the logfile.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"