All Questions
Tagged with file-descriptors pipe
42
questions
19
votes
1
answer
7k
views
How to terminate a TCP connection established by bash itself?
I have used exec 3<>/dev/tcp/192.168.0.101/6435 to establish a TCP connection with 192.168.0.101:6435. And I have received as well as sent a few messages with the pipe command.
Now, I want to ...
17
votes
3
answers
5k
views
SSH: Provide additional "pipe" fds in addition to stdin, stdout, stderr
When connecting to a host with SSH, usually three "pipes" are provided between host and guest, for stdin, stdout, and stderr.
Is there a command-line option to create forwards for additional file ...
11
votes
2
answers
5k
views
How to read user input when using script in pipe
General problem
I want to write a script that interacts with the user even though it is in the middle of a chain of pipes.
Concrete example
Concretely, it takes a file or stdin, displays lines (...
8
votes
4
answers
8k
views
Why is there a "/dev/fd/63" in the output of "echo 123 >(cat)"?
$ echo 123 | cat
123
is doing what I expected, both commands run inside the same shell.
But when I connect them with the >( ... ) expression, which connects the output of one command in the ...
6
votes
2
answers
1k
views
"command | less" vs "less <(command)"
A common "idiom" for viewing large amounts of command output is to pipe it to less, via command | less. However, it's also possible (perhaps only in bash, haven't tested in other shells) to use the ...
6
votes
2
answers
1k
views
dash: Pipe STDIN to multiple commands and their output to STDOUT in defined order
At first I thought this answer was the solution, but now I think I need a temporary file as buffer.
This works unreliably:
#!/bin/sh
echo 'OK' |
{
{
tee /dev/fd/3 | head --bytes=1 >&...
5
votes
2
answers
4k
views
Does `echo -n | ...` send an EOF to the pipe?
Does echo -n | ... send an EOF to the pipe? I.e.,
echo -n | sth
Will sth recieve an EOF on its stdin?
5
votes
1
answer
3k
views
Opening named pipe blocks forever, if pipe is deleted without being connected
Try the following shell commands:
mkfifo /tmp/test.pipe
ls -1 /tmp > /tmp/test.pipe &
rm /tmp/test.pipe
mkfifo /tmp/test.pipe
cat /tmp/test.pipe &
jobs
The ls command is just an ...
4
votes
1
answer
4k
views
How to read a webcam that is already used by a background capture?
A ffmpeg webcam capture is running in the background.
ffmpeg -f video4linux2 -s vga -i /dev/video0 capture.mp4 (1)
I am therefore unable to read it with ffplay since the device /dev/video0 is ...
4
votes
1
answer
5k
views
A command wants file paths. How can I give it stdin for the "infile" and stdout for the "outfile"?
Consider a command like foo -in /path/to/infile -out /path/to/outfile, that you'd like to pass strings to rather than using temp files. It may be called a lot causing much disk access, or is related ...
4
votes
2
answers
4k
views
What does it mean when a file descriptor is a link to a pipe?
When I look at ls -la /proc/<pid>/fd, I see that stdout is a link to a pipe:
lr-x------ 1 admin root 64 Jul 9 21:22 1 -> pipe:[1155]
What does it mean? Who is listening to ...
3
votes
2
answers
2k
views
Flush the pipe/printf buffers externally for already running process with known PID
I am writing a data logging app, all programs are started like:
./program > out.bin
The data collector periodically pools the stdout output files and reads the data.
The issue is that the IO ...
3
votes
1
answer
829
views
Redirection to stderr works in bash, not in zsh
In bash,
❯ echo "hello" 1>&2 | echo "world"
hello
world
In zsh,
❯ echo "hello" 1>&2 | echo "world"
world
More than a way around this, I am ...
3
votes
3
answers
882
views
Print a message from the left-hand side of a pipe
I want to use a variable from the main shell in a sub shell. I did:
export mysql_root_password="test"
(
echo $mysql_root_password
) | dialog --gauge "Working hard..." 6 50
There is no output. Of ...
3
votes
2
answers
261
views
Mark descriptor as terminal for the -t test
One can test if a descriptor is connected to terminal with -t test.
if [ ! -t 0 ]; then
echo "Stdin is not from terminal"
fi
Having a descriptor X, pointing to a file or to a process (with >(cmd)...
3
votes
3
answers
2k
views
Please help explain this bash output redirection
This script was posted as answer to a Question.
And I'm trying to work out what's going on.
result=$(
{
{
ssh host app-status >&3 3>&-; echo "$?"
} | {
until read -...
3
votes
2
answers
682
views
Struggling with understanding redirection in pipes and subshells: Code explanation would be highly appreciated
Please consider the following log from a terminal session (Debian Buster, Bash 5.0):
root@cerberus ~/scripts # rm -f result
root@cerberus ~/scripts # { { echo test; } | cat > result; }
root@...
2
votes
2
answers
677
views
stop pipe() opening stdin
I've currently got code that forks two processes. The first reads a http streaming radio and pushes the data down a pipe (opened with pipe() ) for the second process to read, decode and output to the ...
2
votes
1
answer
3k
views
Effect on the shell pipe operator on stdin, stdout and stderr
When I create a pipe using the shell, for example:
ls | cat
What I know for sure is that the stdin for cat will be the stdout for ls (i.e. everything ls writes to its stdout, cat will read it ...
2
votes
1
answer
2k
views
How to redirect stderr to stdout then pipe (apt-cache)
I'm trying to redirect stderr to stdout and then pipe it but I think I'm missing something basic here.
The command and output to be piped:
$ apt-cache show contractor
N: Can't select versions from ...
2
votes
1
answer
1k
views
Understanding pipes and redirections in dash
Someone asked how to pass the output of two commands as files to another command and they got the answer below.
( cmd1 | ( cmd2 | ( main_command /dev/fd/3 /dev/fd/4 ) 4<&0 ) 3<&0 )
I ...
2
votes
1
answer
1k
views
zsh: understanding sequence of redirections and pipe with pointers
In zsh,
echo "hello" 1>&1 1>&1 1>&1 | cat
prints hello 8 times, while
echo "hello" 1>&1 1>&1 1>&1 1>&1 | cat
prints hello 16 ...
2
votes
1
answer
1k
views
Understanding anonymous pipe ID
I'm currently debugging a system which has a resource leak. It seems that we have too many pipes open. When I list my /proc/PROC_ID/fd I see the following list of pipes:
l-wx------ 1 root root 64 Jun ...
2
votes
0
answers
321
views
bash: writing to the file descriptor #3 of an unnamed (anonymous) pipe
I need to call a PHP interpreter, which is in a docker container, from my Linux host system. For some reasons I don't want to install PHP on the host system. Now I'm trying to call PHP in this way:
...
2
votes
1
answer
695
views
Is there a system call to bind a file descriptor directly into another?
A read() & write() loop would probably be as good as what I'm looking for, but nevertheless is anything like that around or is it impossible because of an obstacle I didn't envisage ? I'm curious
1
vote
4
answers
3k
views
How to pipe fd 3 to consumer instead of fd1 / fd2
I was hoping to do something like this:
echo 'foo' >&3 3| cat
Basically, I want to write 'foo' to 3, and then only pipe the data in 3 to cat. But the above doesn't work, I get:
bash: 3: ...
1
vote
1
answer
499
views
Byte Offsets for pipe/FIFO
Why are byte offsets for a pipe/FIFO maintained in the inode rather than the file table, like for regular files?
I read this line at page 113 of The Design of The Unix Operating System (1986) by ...
1
vote
2
answers
417
views
Is there a way to force the write command to block until all bytes have been written?
Per the man pages, the write command "writes up to count bytes" and then returns the actual number of bytes written. Thus if I wanted to ensure all bytes were written to the file descriptor, ...
1
vote
1
answer
433
views
File descriptor 10 when running a script (Bash/dash, etc.)
When the script runs, a file descriptor 10 appears with the contents of the executable script.
For example, the script:
#!/bin/sh
sleep 600
When viewing the fd of a running script through processes, ...
1
vote
1
answer
886
views
Closing stdout fd gives error, while closing stderr fd does not
The following test code gives error "bad file descriptor". Why is that so? This is just a test code to understand file descriptors and their interaction with pipes.
Example 1)
❯ echo "...
1
vote
1
answer
571
views
Is 2 pipes more expensive than 1 socketpair?
Suppose I want to launch a co-process and attach its standard input and output to the main process, what we have here are 2 options:
call pipe(2) and create 2 pipes, and attach them separately to the ...
1
vote
0
answers
954
views
Read from an arbitrary fd of a running process
I have two processes A and B, communicating over a pipe. I would like to be able to read from that pipe, using lsof I can easily find the right fd, unfortunately tail -f /proc/$pid/fd/$fd doesn't seem ...
1
vote
0
answers
911
views
How to fork a ffmpeg stream in order to read it with ffplay?
Say I create an encrypted webcam capture
ffmpeg -f alsa -ac 2 -i plughw:0,0 \
-f video4linux2 -s vga -i /dev/video0 \
-f ogg >(openssl enc -k $PASSWORD -aes-256-cbc -out webcam.aes-256-cbc
How ...
0
votes
1
answer
411
views
execlp "sort" with input from pipe stucks, why?
sort is waiting, but what? I tried execlp("head", "head", "-n", "3", NULL); instead sort and it works fine.
#include <stdlib.h>
#include <sys/wait.h&...
0
votes
1
answer
410
views
Multiple fifos over SSH
Shell script allow multiple file descriptors (up to 9 for POSIX compliance, but even more for bash):
$ install -m 755 /dev/stdin /tmp/test <<\EOF
#!/bin/sh
echo hi1 >&1
echo hi2 >&...
0
votes
2
answers
615
views
STDOUT + STDERR output ... is there any difference between considering the output to be an empty string vs NULL
I'm writing some application code that is used to execute Linux shell commands, and it then logs the command details into an SQL database. This includes the output of STDOUT + STDERR (separately).
...
0
votes
1
answer
186
views
When simply `| cat -`, I get file descriptor errors
Here's the working diff command:
$ diff -u <(echo 'foo:bar:baz' | tr : "\n") <(echo 'foo:baz' | tr : "\n")
--- /dev/fd/11 2023-08-30 13:11:50
+++ /dev/fd/13 2023-08-30 13:...
0
votes
0
answers
320
views
Write data into read-end of a pipe or vice versa
From pipe manual page
A pipe has a read end and a write end. Data written to the write end of a pipe can be read from the read end of the pipe.
My understanding is that pipe is a buffer (in memory) ...
0
votes
1
answer
2k
views
Writing in the file descriptor of the child process (fd/0)
I'm playing with file descriptors in my CentOS 7 sandbox. In doing so, I noticed an interesting situation. Suppose we've a simple PHP script:
$step = 4 * 1024;
echo "php started\n";
while (!...
0
votes
0
answers
197
views
Is it possible to name an anonymous file socket?
Suppose I perform the following command:
python3 foo.py
Usage: foo ...
Next, I use redirection:
python3 <(foo.py)
Usage: 63 ...
(because the file descriptor assigned is /dev/fd/63)
My arg ...
0
votes
0
answers
110
views
Read user input from piped function
I want to use a function's piped content as message to an interactive prompt, still from inside the function. Here is what I tried;
myfunc ()
{
local input output
output=$(cat /dev/stdin)
...
0
votes
0
answers
76
views
Separate stdout of an application
I have an application that takes a few command line parameters, then prints some text to the terminal, then starts writing data to a file. It has a parameter I can use to define the file name to write ...