Welcome to the Linux Foundation Forum!
Writing to pipe files command
Vani
Posts: 2
HI,
I am a newbie to Linux and am trying to create a pipe file with the below command:
mkfifo pone
Now I am trying to write data to the file with the following command on the command prompt:
echo asdadsasdasdasdasdasd >./pone
But after i enter this command- the screen hangs up and doesnt proceed ahead.
Same thing happened when I tried writing using:
gedit pone
Can anyone help me resolve this issue?
0
Comments
-
Hi there!
Good news is you've got the syntax correct!echo blahblahblah > named_pipe
Is taking the output from the echo command and putting it in the pipe.
Here's the thing: The pipe is what is known as "Blocked" - there is no output at the other end, so your original command is still waiting. (you can cancel it by pressing CTRL+C)
If you open another virtual terminal (or tab in your emulator) you can now read from the named pipe:less < named_pipe
(press 'q' to quit less)
and you will see the output of echo read by less in the second virtual terminal, whilst in the first virtual terminal, the command completes and you are returned to a prompt.
Are you sure you need a named pipe? remember that a named pipe is exactly what it says on the tin - a pipe with a name assigned by you instead of the system, it behaves in exactly the same way. For example, if we repeat the steps above, and create a new named pipe "test" in our home directory:mkfifo ~/test
Then we echo 'mic check check check' down the pipe:echo mic check check check > ~/test
we notice that our command is suspended by the kernal, so we open a new virtual terminal and pick up the pipe:less < ~/test
(notice that we use < not > as we are reading as input not writing as output)
we see the output in the other virtual terminal and the command completes in the first, as expected. Now, let's try and repeat the last step:less < ~/test
Uh - oh! the command hung, the pipe is again 'blocked' - we have no input. Even though we used a file as a pipe, the data is not stored. Don't quit the command yet, let's got to the first virtual terminal again, and type:echo oops! > ~/test
See how now the pipe is 'unblocked' it completes in both virtual terminals? there is no persistence with a named pipe, it just allows you to redirect a pipe across unrelated virtual terminals and applications.
In your example a normal file would work better.echo put this in a file > ~/new_file less ~/new_file
Here the '>' writes the output OVER the file, if no file is found, it creates it. You could use '>>' if you want to APPEND to the end of the file and not erase it. Generally it is risky to '>' to a new file as you may not have checked if the file already exists and accidentally loose data. You can create a blank file to use with:touch new_file
Generally though, the alternative for users would be the 'tee' command. Similar to its' name, the 'tee' command can be thought of as a 'T' junction: it passes the input straight to the output (down the road) whilst also redirecting a copy to a file (turning off at the intersection). To use your example:touch pone echo asdadsasdasdasdasdasd | tee pone
echo completes, and if we check:less pone
Our data has persisted.
If you wanted to use tee between two commands:touch tee_file echo would you like a cuppa? | tee tee_file | cat
And yes, you can use named pipes in place of the unnamed pipes here too to send this accross termials!
Terminal 1:touch ~/cup mkfifo ~/coffee echo Thanks a latte! > ~/coffee
Terminal 2:tee ~/cup <~/coffee | cat
Remember, our cup file has 'Thanks a latte!' in it because it is persistent, but out coffee named pipe has retained no data. We asked echo to print to the screen, which it did, our shell then redirected that output down a pipe called 'coffee' to the tee command, which took it as input, saved it to a file called 'cup' then passed it back to the shell which sent it down a pipe with no name (well a kernel assigned name)to the cat command which printed it back to screen.
Hope it helps!0
Categories
- All Categories
- 175 LFX Mentorship
- 175 LFX Mentorship: Linux Kernel
- 745 Linux Foundation IT Professional Programs
- 372 Cloud Engineer IT Professional Program
- 168 Advanced Cloud Engineer IT Professional Program
- 73 DevOps IT Professional Program - Discontinued
- 3 DevOps & GitOps IT Professional Program
- 98 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- AI & ML Training
- Blockchain & Decentralized Identity Training
- 1 Cloud & Containers Training
- Cybersecurity Training
- DevOps & Site-Reliability Training
- Linux Kernel Development Training
- Networking Training
- Open Source Best Practice Training
- System Administration Training
- System Engineering Training
- Web & Application Development Training
- 2 LFD103-JP クラス フォーラム
- 4 LFD210-CN Class Forum
- 764 LFD259 Class Forum
- 681 LFS101 Class Forum
- 2 LFS158-JP クラス フォーラム
- 162 LFS207 Class Forum
- 3 LFS207-DE-Klassenforum
- 4 LFS207-JP クラス フォーラム
- 61 LFS241 Class Forum
- 52 LFS242 Class Forum
- 42 LFS243 Class Forum
- 19 LFS244 Class Forum
- 4 LFS250-JP クラス フォーラム
- 166 LFS253 Class Forum
- 1.4K LFS258 Class Forum
- 792 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 95 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 87 Storage
- 768 Linux Distributions
- 81 Debian
- 67 Fedora
- 22 Linux Mint
- 13 Mageia
- 24 openSUSE
- 150 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 465 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 98 Linux Security
- 78 Network Management
- 101 System Management
- 46 Web Management
- 106 Mobile Computing
- 18 Android
- 73 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 392 Off Topic
- 121 Introductions
- 181 Small Talk
- 29 Study Material
- 945 Programming and Development
- 310 Kernel Development
- 617 Software Development
- 978 Software
- 370 Applications
- 182 Command Line
- 5 Compiling/Installing
- 68 Games
- 317 Installation
- Archived
- 2 LFD140 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)