Sponsored Content
Top Forums Shell Programming and Scripting Bash - proper way to append variable to stderr Post 303003651 by bakunin on Monday 18th of September 2017 02:26:46 PM
Old 09-18-2017
Quote:
Originally Posted by alex2005
I was trying to figure out what will be the best way to append a variable to the stderr.
Actually the way you presented your problem you were writing into a a file - "error.log". You just happened to redirect stderr to point to that file.

You might picture UNIX processes (running commands) like garden hoses - you put something in on top (this is stdin) then inside something is done with it and finally something pours out at bottom (stdout). You can point both - stdin and stdout - to some file or device. In case of stdin from this device or file is being read, in case of stdout output gets written there. You can even connect stdout of one process to stdin of another process - this is called "pipeline" and looks like this:

Code:
command1 | command2

So far, so basic. Now, the garden hoses of UNIX processes are in fact Y-shaped: the normal flow is like explained above, but for diagnostic messages there is a small outlet in the middle (to stay in the hose picture) where errors, diagnostic messages, etc. are shown. This is good, because if the diagnostic output is separated from the normal output it is possible to process it via different processes.

So, this is what "stderr" is normally used for, but in fact it is just a so-called "I/O descriptor" - a possible outlet where a process can output something and in this respect not different from "stdin" or "stdout"*). You can use stderr in the same way you use stdout and if you write:

Code:
echo "hello world" >&2

you will see "hello world" appear on stderr. If this is inside a script and you redirect the stderr output of it to some file:

Code:
./script 2>/some/logfile

you will find "hello world" in this file.

Per default "stdout" is I/O-descriptor 1 and "stderr" is I/O-descriptor 2 (this is why it is "1>" and "2>" in redirections) but you can use other I/O-descriptors too - you just have to open and close them, just like i did for I/O-descriptor 3 in my sample above.

But the question "include variable in stderr" makes no sense: you can output everything to stderr the same way you output something to stdout or anywhere else - it is just a different part of the y-shaped hose you use.

I hope this helps.

bakunin
_________
*) in fact there are differences: i.e. stdout is buffered, stderr is not, but this is not relevant here

Last edited by bakunin; 09-18-2017 at 08:46 PM.. Reason: typos, thx to RudiC
This User Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

redirect stderr to dev/null in bash env.

Working in a bash environment, in the following example, how do I direct the error message that putting in an invalid flag (-j for example) would normally produce to dev/null? while getopts "abcd" opt do case "$opt" in i) a etc ;; r) b etc ;; f) c etc ;; v) d... (7 Replies)
Discussion started by: Sniper Pixie
7 Replies

2. Shell Programming and Scripting

can't redirect stderr in bash

Consider: #!/bin/sh #this is a shell script in sh (bourne) grep missingfile 2>errout.txt It works from the command line, but keeps producing errors from the script. So how do I redirect in a bash shell...or bourne? (3 Replies)
Discussion started by: lumix
3 Replies

3. Shell Programming and Scripting

Redirecting STDERR to a file from within a bash script

I am trying to redirect the output from stderr to a log file from within a bash script. the script is to long to add 2> $logfile to the end of each command. I have been trying to do it with the command exec 2> $logfile This mostly works. Unfortunately, when a read command requires that anything be... (5 Replies)
Discussion started by: vockleya
5 Replies

4. Shell Programming and Scripting

Can STDERR be saved to a variable

Guys i'm trying to save STDERR to a variable for a portion of my ksh script on solaris. I know i can create redirects to files as such: exec 4>/tmp/lava print "This will be saved to /tmp/lava and not screen"; >&4 print "This will be seen on screen" >&2 I want to save the STDOUT of a... (4 Replies)
Discussion started by: lavascript
4 Replies

5. Shell Programming and Scripting

Append stderr

Hi everybody. I was used to redirect stderr to a file in this way, calling a generic script:./myScript &> output.logBut now I need something more sophisticated...Inside a bash script I launch an executable in this way:${command} >> "${globalLogFile}"So I redirect the stdout into globalLogFile.... (14 Replies)
Discussion started by: canduc17
14 Replies

6. Shell Programming and Scripting

ssh, bash, and /dev/stderr: no such device

Hello, When I run the following program: ssh 192.168.1.4 bash -l <<EOF > echo foo >/dev/stderr > EOF I get the following confusing error. bash: line 1: /dev/stderr: No such device or address Does anyone know why and how to fix it? I'm capturing stdout in a variable, but I... (2 Replies)
Discussion started by: brsett
2 Replies

7. Shell Programming and Scripting

How to redirect the STDERR to a variable in perl?

in my perl script i tried the below statement $result = `cleartool rmstream -f $s1 1> /dev/null`; so as to redirect then error messages,when i print the $result ,it seems to be Null. (4 Replies)
Discussion started by: ram_unx
4 Replies

8. Shell Programming and Scripting

[bash] how is proper way to validate user input

hi all, i have a script that need user input provide all variables that needed to complete a job. this is my current script: echo "type file source and it full path :" read INPUTFILE if || ; then echo "ERROR: you didn't enter a file source or file source is not... (2 Replies)
Discussion started by: makan
2 Replies

9. Shell Programming and Scripting

Proper distribution of cards in terminal based crazy8's game in bash script

When I run the following script at the bottom it say cards remaining=44...It should be=35. Can anyone tell me what I'm doing wrong. I've spent hours trying to get this to work and I can't go any further until this part works. thank you in advance Cogiz #!/bin/bash # Date="November, 2016" #... (2 Replies)
Discussion started by: cogiz
2 Replies

10. Shell Programming and Scripting

Redirect string from bash stderr to user stdin

Hi there, I need to execute a command in the bash. The program prints some standard (output and) error and then wants the user to choose one of several options and type the according input. I am trying to solve this issue in a bash script but also running into some circular dependency. How can I... (7 Replies)
Discussion started by: fredestet
7 Replies
All times are GMT -4. The time now is 01:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy