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:
In order to get the missing files logs i run:
But if I want to append a variable let's say the date, I was able to came with:
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
If you have single lines in a loop you do not need another loop within
It is more efficient to have two loops indeed, but one after the other
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.
Last edited by MadeInGermany; 09-17-2017 at 03:54 AM..
This User Gave Thanks to MadeInGermany For This Post:
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:
If you do this in ksh you have an even better and cleanlier way to do this:
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 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:
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:
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:
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)