Tag Info

Hot answers tagged

36

Both of those examples are useless uses of cat. Both are equivalent to wc < file1 > file2. There is no reason to use cat in this example, unless you are using cat file as a temporary stand-in for something that dynamically generates output.


22

cat file | wc | cat > file2 would usually be two useless uses of cat as that's functionally equivalent to: < file wc > file2 However, there may be a case for: cat file | wc -c over < file wc -c That is to disable the optimisation that many wc implementations do for regular files. For regular files, the number of bytes in the file ...


14

While I don't disagree with the argument for saying it is a 'useless use of cat', there can be reasons for it: In many languages (including English) words and sentences are read from left to right, so showing the flow of data in the same way can appear more natural to the reader. A reason for the second cat could be to mask the return code. Such as: $ wc ...


13

wc will tell you what file it's working on if it's able. With the first one with the pipe it's reading from stdin, not a file, so does not report a filename. The second one, however, you're using process substitution which presents the output of the command as a file, which wc reports. It reports on the file descriptor it was given from which to read.


12

The | will take the output of the command on the left and give it to the input of the command on the right. The > operator will take the output of the command and put it into a file. That means, in your example, by the time it gets to the | there is no output left; it's all gone into a.txt. So the sort on the right operates on an empty string and saves ...


7

Let's suppose prog forks a new subprocess and exits, and the new subprocess writes something to its standard output and then exits. Then the command prog won't wait for the subprocess to exit, and it will display the shell prompt early. But the command prog | cat will wait for an EOF on the stdin of cat, which effectively waits for the subprocess to ...


7

bsdtar (based on libarchive) can filter tar (and some other archives) from stdin to stdout. It can for example pass through only filenames matching a pattern, and can do s/old/new/ renaming. It's already packaged for most distros, for example as bsdtar in Ubuntu. sudo apt-get install bsdtar # or aptitude, if you have it. # example from the man page: ...


5

You cannot refer variable updates made in the child process (pipe connected while block). Instead, feed data using input redirection like this: #!/bin/bash data_file="$1" down=() counter=0 while read line; do isEven=$(( $counter % 2 )) if [ $isEven -eq 0 ]; then down+=("$line") fi (( counter ++ )) done < $data_file echo ...


4

It's the shell; as you will see via the ps command, in that case the PPID of less will be the PID of the shell. The processes ls and less haven't much in common; it's just that the stdout of the former is piped in the stdin of the latter.


3

The command you're trying is trying to treat cat as a file, shoving it's contents into mysql's STDIN. Instead, you need the results of the cat command shoved into STDIN, which can be done a couple different ways. The way I would do it would be to pipe the output of cat into mysql: $ cat schema/db1/*.sql | mysql -u root Equally validly, you can use: $ ...


3

Here's a summary of some of the drawbacks of: cat $file | cmd over < $file cmd First, a note: there are (intentionally for the purpose of the discussion) missing double quotes around $file above. In the case of cat, that's always a problem except for zsh; in the case of the redirection, that's only a problem for bash or for some other shells only ...


3

ddrescue is a tool that will try to help you get data from a disk that is dying, where it may be necessary to reread the same parts of a file several times until no error occurs anymore. To be able to do that, it needs to seek the input file, which is impossible on a pipe. It's also pretty pointless to try to use ddrescue unless your disk is, in fact, ...


3

The easiest way would be to copy the whole archive; I presume you don't want to do that because it's too large. The usual command line tools (tar, pax) don't support copying members of an archive to another archive. If you didn't need to preserve ownership, I'd suggest using FUSE filesystems. You can use archivemount to mount an archive as a filesystem; do ...


3

You can do this using socket forwarding, which is available since openssh-6.7. This is some kind of pipe. This technique is described for example here: http://www.25thandclement.com/~william/projects/streamlocal.html You will gain two-direction route for your data. There is example with mysql: Proxy MySQL client connections on a remote server to your ...


2

If prog2 follows a common convention you could use - as the "file" to tell it to read from stdin and then the pipeline would be prog1 <inputfile> - min max | prog2 - <outputfile> min max which would tell prog1 to write to stdout as its output file, and prog2 would use stdin as its input file with the pipe connecting those two. This would not ...


2

SSH keys First of all, the best solution for you would be to create new ssh keypair and use this key to login to all your servers (or some, based on preferences). If you don't know how, you can find it many times here on stackexchange, but shortcut: ssh-keygen; ssh-copy-id your-host Basically you should set passphrase for your key, so you will log in ...


2

is easy and there are many ways to do, for example v=$(echo $x1+$x2 | bc) v=`echo $x1+$x2 | bc` Note that bc is integer arithmetics only and that you need bc -l for a proper math library. Note that you can skip the echoing with the 'here' redirection <<< for strings: v=$( bc <<< $x1+$x2 )


2

ls > a.txt | sort > b.txt You are executing ls. Then you are redirecting only the STDOUT of the ls command into a.txt. Then you are trying to also PIPE STDOUT to the STDIN of the sort command. Because STDOUT is being redirected into the file a.txt, there is nothing in the STDIN of the sort command to be sorted into b.txt, which is why the file is ...


2

Some experiments: $ yes | sleep 10m & [1] 32395 32396 $ pstree -pa $(ps -o ppid= -p $(pgrep yes)) zsh,29630 ├─pstree,32402 -pa 29630 ├─sleep,32396 10m └─yes,32395 As can be seen, the parent of both processes is the shell. With a longer pipeline: $ sleep 10m | sleep 10m | sleep 10m | sleep 10m & [1] 32320 32321 32322 32323 $ pstree -pa $(ps ...



Only top voted, non community-wiki answers of a minimum length are eligible