when writing a script, when redirececting a message to stderror, how do i do it?
i can have
echo "message" 2> error.log
is it neccessary to do it while creating a file, if not how do i send it to stderror and have it displayed on the screen at same time, and not have to create a file.
I'm going to read between the lines and assume that you want to output to stderr within a script so that if you run the script with stderr redirected to a file, that output goes to the file.
The syntax is
echo "message" 1>&2
Generally
x>&y
where "x" and "y" are integers means to redirect file-descriptor "x" to the same place "y" is going. FD 1 is stdout (which "echo" writes to) and FD 2 is stderr, so
1>&2
(or just
>&2
which implies redirecting stdout) means redirect stdout to whatever stderr is connected to.
It redirects standard output(FD 1, the default output to redirect) to standard error(FD 2).
That's not the standard way to do it by the way, as some systems don't even have a "/dev/stderr" thing (which is usually just a symbolic link anyway). Best way to do it is echo message >&2 This takes the default output FD, 1, and redirects it into standard error, i.e. FD 2.