Read from file descriptor and write to stdout
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I want to prepend something to each line of output in a script, for every command.
I was thinking of doing something like this:
rm foo
mkfifo foo
exec 3<>foo
cat <&3 | while read line; do
if [[ -n "$line" ]]; then
echo " [prepend] $line";
fi
done &
echo "foo" >&3
echo "bar" >&3
echo "baz" >&3
basically for all commands I want to prepend something to each line of the output. My code above is fairly bogus, but I don't quite know how to do it, it's something like the above but not quite.
bash shell stdout file-descriptors exec
add a comment |Â
up vote
0
down vote
favorite
I want to prepend something to each line of output in a script, for every command.
I was thinking of doing something like this:
rm foo
mkfifo foo
exec 3<>foo
cat <&3 | while read line; do
if [[ -n "$line" ]]; then
echo " [prepend] $line";
fi
done &
echo "foo" >&3
echo "bar" >&3
echo "baz" >&3
basically for all commands I want to prepend something to each line of the output. My code above is fairly bogus, but I don't quite know how to do it, it's something like the above but not quite.
bash shell stdout file-descriptors exec
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to prepend something to each line of output in a script, for every command.
I was thinking of doing something like this:
rm foo
mkfifo foo
exec 3<>foo
cat <&3 | while read line; do
if [[ -n "$line" ]]; then
echo " [prepend] $line";
fi
done &
echo "foo" >&3
echo "bar" >&3
echo "baz" >&3
basically for all commands I want to prepend something to each line of the output. My code above is fairly bogus, but I don't quite know how to do it, it's something like the above but not quite.
bash shell stdout file-descriptors exec
I want to prepend something to each line of output in a script, for every command.
I was thinking of doing something like this:
rm foo
mkfifo foo
exec 3<>foo
cat <&3 | while read line; do
if [[ -n "$line" ]]; then
echo " [prepend] $line";
fi
done &
echo "foo" >&3
echo "bar" >&3
echo "baz" >&3
basically for all commands I want to prepend something to each line of the output. My code above is fairly bogus, but I don't quite know how to do it, it's something like the above but not quite.
bash shell stdout file-descriptors exec
edited May 8 at 4:17
asked May 8 at 4:01
Alexander Mills
1,885929
1,885929
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
Assuming script produces:
L1
L2
L4
L5
then the following command
script | sed 's/^(.+)/ [prepend] 1/'
prepends " [prepend] " to each non-empty line:
[prepend] L1
[prepend] L2
[prepend] L4
[prepend] L5
add a comment |Â
up vote
1
down vote
You may want to look at the DEBUG
trap in bash. From man builtins
:
If a sigspec is DEBUG, the command arg is executed before every simple command,
for command, case command, select command, every arithmetic for command, and
before the first command executes in a shell function (see SHELL GRAMMAR
above). Refer to the description of the extdebug option to the shopt builtin
for details of its effect on the DEBUG trap. If a sigspec is RETURN, the comâÂÂ
mand arg is executed each time a shell function or a script executed with the .
or source builtins finishes executing.
So, you could set up a debug function like this. Since it runs before commands you could use it to prepend to your output.
#!/bin/bash
debug()
: # echo or other commands here
trap debug DEBUG
# Commands here
add a comment |Â
up vote
0
down vote
This exact code seems to do what I want, but not sure how safe it is:
rm foo
mkfifo foo
exec 3<>foo
(
cat <&3 | while read line; do
if [[ -n "$line" ]]; then
echo " [prepend] $line";
fi
done &
)
echo "" >&3;
echo "" >&3;
echo "foo" >&3
echo "bar" >&3
echo "baz" >&3
pkill -P $$
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Assuming script produces:
L1
L2
L4
L5
then the following command
script | sed 's/^(.+)/ [prepend] 1/'
prepends " [prepend] " to each non-empty line:
[prepend] L1
[prepend] L2
[prepend] L4
[prepend] L5
add a comment |Â
up vote
1
down vote
Assuming script produces:
L1
L2
L4
L5
then the following command
script | sed 's/^(.+)/ [prepend] 1/'
prepends " [prepend] " to each non-empty line:
[prepend] L1
[prepend] L2
[prepend] L4
[prepend] L5
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Assuming script produces:
L1
L2
L4
L5
then the following command
script | sed 's/^(.+)/ [prepend] 1/'
prepends " [prepend] " to each non-empty line:
[prepend] L1
[prepend] L2
[prepend] L4
[prepend] L5
Assuming script produces:
L1
L2
L4
L5
then the following command
script | sed 's/^(.+)/ [prepend] 1/'
prepends " [prepend] " to each non-empty line:
[prepend] L1
[prepend] L2
[prepend] L4
[prepend] L5
answered May 8 at 22:11
John Doe
804
804
add a comment |Â
add a comment |Â
up vote
1
down vote
You may want to look at the DEBUG
trap in bash. From man builtins
:
If a sigspec is DEBUG, the command arg is executed before every simple command,
for command, case command, select command, every arithmetic for command, and
before the first command executes in a shell function (see SHELL GRAMMAR
above). Refer to the description of the extdebug option to the shopt builtin
for details of its effect on the DEBUG trap. If a sigspec is RETURN, the comâÂÂ
mand arg is executed each time a shell function or a script executed with the .
or source builtins finishes executing.
So, you could set up a debug function like this. Since it runs before commands you could use it to prepend to your output.
#!/bin/bash
debug()
: # echo or other commands here
trap debug DEBUG
# Commands here
add a comment |Â
up vote
1
down vote
You may want to look at the DEBUG
trap in bash. From man builtins
:
If a sigspec is DEBUG, the command arg is executed before every simple command,
for command, case command, select command, every arithmetic for command, and
before the first command executes in a shell function (see SHELL GRAMMAR
above). Refer to the description of the extdebug option to the shopt builtin
for details of its effect on the DEBUG trap. If a sigspec is RETURN, the comâÂÂ
mand arg is executed each time a shell function or a script executed with the .
or source builtins finishes executing.
So, you could set up a debug function like this. Since it runs before commands you could use it to prepend to your output.
#!/bin/bash
debug()
: # echo or other commands here
trap debug DEBUG
# Commands here
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You may want to look at the DEBUG
trap in bash. From man builtins
:
If a sigspec is DEBUG, the command arg is executed before every simple command,
for command, case command, select command, every arithmetic for command, and
before the first command executes in a shell function (see SHELL GRAMMAR
above). Refer to the description of the extdebug option to the shopt builtin
for details of its effect on the DEBUG trap. If a sigspec is RETURN, the comâÂÂ
mand arg is executed each time a shell function or a script executed with the .
or source builtins finishes executing.
So, you could set up a debug function like this. Since it runs before commands you could use it to prepend to your output.
#!/bin/bash
debug()
: # echo or other commands here
trap debug DEBUG
# Commands here
You may want to look at the DEBUG
trap in bash. From man builtins
:
If a sigspec is DEBUG, the command arg is executed before every simple command,
for command, case command, select command, every arithmetic for command, and
before the first command executes in a shell function (see SHELL GRAMMAR
above). Refer to the description of the extdebug option to the shopt builtin
for details of its effect on the DEBUG trap. If a sigspec is RETURN, the comâÂÂ
mand arg is executed each time a shell function or a script executed with the .
or source builtins finishes executing.
So, you could set up a debug function like this. Since it runs before commands you could use it to prepend to your output.
#!/bin/bash
debug()
: # echo or other commands here
trap debug DEBUG
# Commands here
answered May 8 at 23:20
m0dular
63115
63115
add a comment |Â
add a comment |Â
up vote
0
down vote
This exact code seems to do what I want, but not sure how safe it is:
rm foo
mkfifo foo
exec 3<>foo
(
cat <&3 | while read line; do
if [[ -n "$line" ]]; then
echo " [prepend] $line";
fi
done &
)
echo "" >&3;
echo "" >&3;
echo "foo" >&3
echo "bar" >&3
echo "baz" >&3
pkill -P $$
add a comment |Â
up vote
0
down vote
This exact code seems to do what I want, but not sure how safe it is:
rm foo
mkfifo foo
exec 3<>foo
(
cat <&3 | while read line; do
if [[ -n "$line" ]]; then
echo " [prepend] $line";
fi
done &
)
echo "" >&3;
echo "" >&3;
echo "foo" >&3
echo "bar" >&3
echo "baz" >&3
pkill -P $$
add a comment |Â
up vote
0
down vote
up vote
0
down vote
This exact code seems to do what I want, but not sure how safe it is:
rm foo
mkfifo foo
exec 3<>foo
(
cat <&3 | while read line; do
if [[ -n "$line" ]]; then
echo " [prepend] $line";
fi
done &
)
echo "" >&3;
echo "" >&3;
echo "foo" >&3
echo "bar" >&3
echo "baz" >&3
pkill -P $$
This exact code seems to do what I want, but not sure how safe it is:
rm foo
mkfifo foo
exec 3<>foo
(
cat <&3 | while read line; do
if [[ -n "$line" ]]; then
echo " [prepend] $line";
fi
done &
)
echo "" >&3;
echo "" >&3;
echo "foo" >&3
echo "bar" >&3
echo "baz" >&3
pkill -P $$
answered May 8 at 4:25
Alexander Mills
1,885929
1,885929
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442455%2fread-from-file-descriptor-and-write-to-stdout%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password