Personally, I would use a different approach. Make an alias for your script. Add this line to your ~/.bashrc
(or equivalent in other shells)
alias backlight-adjust='sudo ~/bin/backlight-adjust'
That way, you don't need to worry about remembering to run it with sudo
and you don't need to add the sudo
to the script. It will be completely transparent to you and simply ask for your password when you try and run backlight-adjust
.
For a pipe, the end of file is seen by the consumer(s) once all the producers have closed their file descriptor to the pipe and the consumer has read all the data.
So, in:
{
echo foo
echo bar
} | cat
cat
will see end-of-file as soon as the second echo
terminates and cat
has read both foo\n
and bar\n
. There's nothing more for you to do.
Things to bear in mind though is that if some of the commands on the left side of the pipe starts some background process, that background process will inherit a fd to the pipe (its stdout), so cat
will not see eof until that process also dies or closes its stdout. As in:
{
echo foo
sleep 10 &
echo bar
} | cat
You see cat
not returning before 10 seconds have passed.
Here, you may want to redirect sleep
's stdout to something else like /dev/null
if you don't want its (non)output to be fed to cat
:
{
echo foo
sleep 10 > /dev/null &
echo bar
} | cat
If you want the writing end of the pipe to be closed before the last command in the subshell left of the |
is run, you can close stdout or redirecting to that subshell in the middle of the subshell with exec
, like:
{
echo foo
exec > /dev/null
sleep 10
} | (cat; echo "cat is now gone")
However note that most shells will still wait for that subshell in addition to the cat
command. So while you'll see cat is now gone
straight away (after foo
is read), you'll still have to wait 10 seconds for the whole pipeline to finish. Of course, in that example above, it would make more sense to write it:
echo foo | cat
sleep 10
<<ANYTHING...content...ANYTHING
is a here-document, it's to make the stdin of command a file that contains the content. It wouldn't be useful there. \4
is byte that when read from a terminal makes data held by a terminal device be flushed to the application reading from it (and when there's no data, read()
returns 0 which means end-of-file). Again, not of any use here.
Best Answer
You want the
-a
option totee
which appends rather than overwriting.