Bash - proper way to append variable to stderr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - proper way to append variable to stderr
# 1  
Old 09-16-2017
Bash - proper way to append variable to stderr

Hello,

Can you please if the bellow is the proper way of appending a variable to the stderr:

The easiest way to test this,I was able to imagine, was by touching 5 files and afterwards looping trough to the results:
Code:
-rw-r--r--  1 ab  owner  0 Sep 14 13:45 file1
-rw-r--r--  1 ab  owner  0 Sep 14 13:45 file3
-rw-r--r--  1 ab  owner  0 Sep 14 13:45 file5
-rw-r--r--  1 ab  owner  0 Sep 14 13:45 file6
-rw-r--r--  1 ab  owner  0 Sep 14 13:45 file8

In order to get the missing files logs i run:
Code:
for i in {1..8} ; do ls -al file$i 2>> error.log ; done

But if I want to append a variable let's say the date, I was able to came with:
Code:
for i in {1..8} ; do ls -al file$i 2> >( while read line; do echo "$(date): ${line}"; done >> error.log ) ; done

The question I have if this the best way to append a variable to stderr?

Last edited by MadeInGermany; 09-16-2017 at 04:16 PM.. Reason: Changed icode tags to code tags
# 2  
Old 09-17-2017
If you have single lines in a loop you do not need another loop within
Code:
for i in {1..8} ; do ls -al file$i 2> >( read line; echo "$(date): ${line}" >> error.log ) ; done

It is more efficient to have two loops indeed, but one after the other
Code:
for i in {1..8} ; do ls -al file$i; done 2> >( while read line; do echo "$(date): ${line}"; done >> error.log )

where error.log is written in one stream, so you have the option > error.log
And in the case of date, if its output does not change much, one can store it in a variable first, and several time refer to the variable.
Code:
date=$(date); for i in {1..8} ; do ls -al file$i; done 2> >( while read line; do echo "${date}: ${line}"; done >> error.log )

Code:
for i in {1..8} ; do ls -al file$i; done 2> >( date=$(date); while read line; do echo "${date}: ${line}"; done >> error.log )


Last edited by MadeInGermany; 09-17-2017 at 03:54 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 09-18-2017
Hello, Thank you very much for taking time to answer and for the useful examples.
# 4  
Old 09-18-2017
Quote:
Originally Posted by alex2005
In order to get the missing files logs i run:
Code:
for i in {1..8} ; do ls -al file$i 2>> error.log ; done

I am not sure what exactly you want to do, but in this case you can simply test for the existence of the file(s) and then construct your own error message:

Code:
for i in 1 2 3 4 5 6 7 8 ; do
     if [ ! -e "/path/to/file$i" ] ; then
          echo "File file$i is missing, date is: $(date)" >>/your/log/file
     fi
done

If you do this in ksh you have an even better and cleanlier way to do this:

Code:
#! /bin/ksh

exec 3>>/path/to/your/logfile        # open logfile for appending and use IOD 3 to address it.

print -u3 - "----- start of execution ( $(date) ) -----" # -u3 : send it to IOD 3
for i in 1 2 3 4 5 6 7 8 ; do
     if [ ! -e "/path/to/file$i" ] ; then
          print -u3 - "File file$i is missing, date is: $(date)"
     fi
done
print -u3 - "----- end of execution ( $(date) ) -----"

exec 3>&-     # close IO descriptor 3 again

This way you can open (and close) the file once and do not have to address it anew for every message you want to append. You also don't have to worry about one misplaced ">" instead of a ">>" wiping out the whole logfile. And you can use several such files at once by using IOD 4, 5, 6, ... too.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 09-18-2017
Hi, Thank you for your post.

I was trying to figure out what will be the best way to append a variable to the stderr.
# 6  
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:
# 7  
Old 09-18-2017
Thanks for the detailed explanations, for sure will help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question