Tag Info

Hot answers tagged

15

As far as I know, cmd1 | tee verylong.txt | cmd2 will not hold the whole file in memory. In fact, if cmd2 was to wait too long before consuming its input, cmd1 might block on a write call and unblock only when cmd2 starts reading again. The reason for that is that there is a buffer for the pipe, and that buffer, by default, is limited to a certain ...


8

Redirect all the output as a block. ( yum -y update service restart nginx yum install atop cat /var/log/somelog.log > /home/cron/php/script ) > /dev/null 2>&1


7

You can save the file descriptors of stdout and stderr, overwrite them, and restore them after the programs have been run: exec 3>&1 exec 4>&2 exec 1>/dev/null exec 2>/dev/null ./script-1 ./script-2 ... ./script-n exec 1>&3 exec 2>&4 # delete the copies exec 3>&- exec 4>&-


6

The already given answer is correct. But if your goal is to selectively read your verylongfile.txt w/ cmd2, sed might be another option. cmd1 | sed -e 'w verylongfile.txt' -e '/notinteresting/d' | cmd2 sed will write all of its input to the outfile, but only the bits that do not match the /notinteresting/ address to the pipe. Or you might negate the ...


4

Digging around here, I understood from http://unix.stackexchange.com/a/56877/54067 (where both the question and answer are worded differently and the problem is not related to interactive input) that the reason for the problem is that the cp -i expects the user to give the interactive input confirmation via stdin but in the cat | while read loop stdin is ...


4

I you mean that someone may open 2 TCP connections to your machine, one to port 50505 and another to port 60606, send data on the first one intended to be fed to P and expect to read the output of P from the second TCP connection, then that would be: < /dev/null nc -q -1 -l 50505 | P | nc -l 60606 > /dev/null Or with socat: socat -u ...


3

You are piping the output from echo "some info" > final.csv into cat temp.csv >> final.csv, so these two commands (echo and cat) run in parallel. Because of that, what ends up in the final.csv depends on what program gets scheduled. What you want to do is replace that | with a ;. And then the echo command will run until finished, and only then cat ...


3

Edit in place, keeping only lines that match the pattern: sed -n -r -i "/(pattern1|pattern2|pattern3|pattern4|pattern5)/p" ~/mpp/*.xml On BSD sed (Mac OSX), try: sed -n -E -i '' "/(pattern1|pattern2|pattern3|pattern4|pattern5)/p" ~/mpp/*.xml Basing the file name off of an already existing variable for fname in ~/mpp/*.xml do egrep ...


3

On second thought, perhaps a more simple approach than my original answer (which I have reproduced below since that is what was originally accepted) is to change the file descriptor from 200 to something that is <9. For example, 5. So, change all three instances of 200 in your script to 5: #!/bin/sh case "$IFACE" in lo) # The loopback ...


2

The problem is that the output redirection kills the file before less is even started: open("file", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3 dup2(3, 1) = 1 close(3) = 0 execve("/usr/bin/less", ["less", "-WNS", "file"], [/* 102 vars */]) = 0 The normal ourput redirection overwrites the file. The O_TRUNC (see man 2 ...


2

The only file descriptor numbers that you can portably use in a shell script are 0 through 9 (i.e. a single digit). Quoting POSIX: Open files are represented by decimal numbers starting with zero. The largest possible value is implementation-defined; however, all implementations shall support at least 0 to 9, inclusive, for use by the application. These ...


1

Store the output of smartctl into a variable and use it as many times as you like. smartctl_output=$(smartctl -a /dev/sda) serial_number=$(printf %s "$smartctl_output" | awk 'sub(/^Serial Number: */,"")') printf %s "$smartctl_output" >"$serial_number.txt"


1

You could use another command (namely lsblk) to get only the serial number of that particular device (so no need to parse smartctl output): smartctl -a /dev/sdb > $(lsblk --nodeps -no serial /dev/sdb)


1

This allows you to embed the text that will be fed to a command within a script in the script itself instead of an external file. So, instead of redirecting from another file, you could have the input content in a single file containing both the script and the input data. This is specially useful when mixing shell scripts and awk, perl or other scripting ...


1

If you want to hide all output from the commands you're running (both output and errors), but still be able to print messages yourself, then Hauke Laging's approach is absolutely the right way to do this. It lets you keep a reference to stdout (aka file descriptor 1) and stderr (aka file descriptor 2), redirect them to /dev/null, but still use them if you ...


1

| mean pipe == output of the previous command piped to next command input. If you use > final.csv that redirect output of echo into file and remains nothing to pipe. So to correct you free use 2 command separatelly as you have offered above or use pipe if your want echo "some info" | cat - temp.csv > final.csv Other do the same cat - temp.csv ...



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