New answers tagged

9

This is because the netcat command has not even started yet! The shell when trying to open the fifo for input will block. Try strace cat >fifo <fifo and you will see nothing. Instead use, for example, nc -k -l 4458 -v <>fifo >&0 which opens the fifo for read and write as stdin, and then dups it to stdout. Tracing the full bash ...


4

That's basically a duplicate of my answer on SO. However, since nobody mentioned the stdbuf command here, I felt like I should add that here as well. =============== Basically a process that reads from a pipe can consume the data byte by byte as soon as they are available in the pipe. However, as long as the programs are using std io functions of the libc, ...


5

The pipes connect the output or the left command to the input of the right command. This has nothing to do with the length of the stream. However, each command in the pipeline still has it's own buffering rules. If you don't trigger them in each command you won't see them on the final output.


3

You could use the -u option of sed to minimize buffering: cat | sed -u '' | sed ''


1

You may be after: \e[32m style escape sequences in your string such as echo -e "Doing ls\n\e[32m$(ls --color=never)\e[33m ^^^\n \e[36m ls \e[39m output" printf "\e[35mHello\e[41mWorld\e[0m!" There are a few articles & listings of colours including: http://www.bashguru.com/2010/01/shell-colors-colorizing-shell-scripts.html?m=1


3

This isn't possible with standard UNIX/POSIX facilities. Your imperfect options are: Just use a shell script, and have the "opening" application (or your shell) execute it instead of opening it for reading. Some apps may support doing this, otherwise you'll have to resort to a shell script wrapper. Use a fifo, and write a daemon that writes to it. This of ...


1

One example: $ echo jeff > jeff $ echo not > not $ echo 's/jeff/not/' | sed -i -f - * $ cat jeff not For more information on what standard shells should do with filename/pathname expansion (globbing), see: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_06 where it says: After field splitting, if set -f is not ...


4

Instead of >(cmd), assuming it's cmd's stdin you wish be a tty instead of a pipe, you could try and use: >(socat -u - exec:'cmd',pty) socat would use a pseudo-tty pair and have cmd's stdin connected to the slave part ([ -t 0 ] would then return true). The pseudo-terminal will be put in raw mode, so the line discipline should not interfere with the ...


2

The test command runs isatty(fd) to check whether a descriptor is related to a TTY. isatty(3) is a C library function that checks whether a file descriptor allows to send terminal ioctl(2) calls to the descriptor. You therefore cannot fake the result unless you use LD_PRELOAD= to overwrite the isatty() function in the shell.


0

As a pure awk solution, try: tail -f log.file | awk ' $0!=last{ print $1 " " $4 " " $9} {last=$0}' This one prints a new output line only if the input line is different from the previous input line. As a slight variation, this one prints a new output line only if this output line differs from the previous output line: tail -f log.file | awk '{$0=$1" ...


30

vipe is a program for editing pipelines: command1 | vipe | command2 You get an editor with the complete output of command1, and when you exit, the contents are passed on to command2 via the pipe. In this case, there's no command1. So, you could do: : | vipe | pandoc -o foo.pdf Or: vipe <&- | pandoc -o foo.pdf vipe picks up on the EDITOR and ...


18

You can do this from within Vim: :w !pandoc -o file.pdf Or even write the buffer into a complex pipeline: :w !grep pattern | somecommand > file.txt And then you can exit Vim without saving: :q! However, considering your specific use case, there is probably a better solution by using vi as your command line editor. Assuming you use bash: set ...


4

Make sure that vim is set as your default editor (e. g. export EDITOR=vim in your .bash_profile or .bashrc. Then, at any prompt, you can type Ctrl-X followed by Ctrl-E. This will open your current command line in your configured editor (e. g. vim). Make your edits, save and exit, and the command will be executed as though you had typed it on the command ...


10

Running in a pipeline Try: quickedit() ( trap 'rm ~/temp$$' exit; vim ~/temp$$ >/dev/tty; cat ~/temp$$ ) The key is that, to be able to use vim normally, vim needs stdout to be the terminal. We accomplish that here with the redirect >/dev/tty. For purposes of security, I put the temporary file in the user's home directory. For more on this, see ...


0

Here's another way with sed: myscript | sed '/PATTERN/w out.file' By default, sed prints every line so in this case stdout will be the same as stdin (i.e. you'll see the entire output of myscript on screen). In addition, all lines matching PATTERN will be written to out.file


5

Just use sed p. echo foobar | sed p You don't need cat, either: sed p input.txt # or sed p input.txt > output.txt Explanation p is the sed command for "print." Print is also sed's default action. So when you tell sed explicitly to print, the result is that it prints every line twice. Let's say you wanted to only print lines that include the ...


0

cat input.txt | awk '1;1' > output.txt edit: this is joepd's less verbose version, original below cat input.txt | awk '{print $0 "\n" $0}' > output.txt


0

Not sure what this is supposed to achieve but here is a solution that does exactly what you're doing without using an intermediate file: #!/bin/bash # prevent LF from being removed export IFS=' ' output=`process1` echo $output | head -n 3 echo $output | tail -n +4 | head -n -4 | process2 # producing output echo $output | tail -n 4


0

I think you can just pipe them : to process every line from process1 except the first and last, say, 3 lines: process1 | tail -n +4 | head -n -3 | process2


1

Finally I have figured out what the problem is! It is most likely because of an IP Conflict ._. Sorry to bother you guys. After I have switched from fix IP to DHCP, no packets loss in ping anymore. Thanks guys. Lesson learned.


0

Thanks to Jeff Schaller's answer, I ended up with something like this, which basically does what I need, let's call it reader.sh: #!/bin/sh INDEX=0 LOGNAME="$INDEX.log" switchlog() { local custom_name read -p "Add log name: " custom_name INDEX=$((INDEX+1)) LOGNAME="$(printf "%03d" $INDEX).$custom_name.log" echo now writing to $LOGNAME } trap ...


7

Building up on your SIGINT idea, here using SIGQUIT (Ctrl+\) to you can still use Ctrl+C to stop the whole thing: (trap '' QUIT; monitor_command) | ( trap : QUIT ulimit -c 0 # prevent core dump so SIGQUIT behaves like SIGINT # for cat n=0; while n=$((n+1)); file=output.$n.log; do printf 'Outputting to "%s"\n' "$file" cat ...


9

I'll suggest a named pipe. Create a pipe mkfifo p (call it whatever you want, if not 'p') Create a "reader" script that reads from the pipe and writes wherever you like Tell the monitoring program to write its logs to the named pipe Here's a sample reader script that reads from a named pipe 'p' and writes the data to an indexed 'mylog' file: #!/bin/sh ...


0

Without knowing more about your "request" its not really possible to answer. If it were based on file size or interval, then rotatelogs (which should come bundled with Apache httpd) would work.


1

You could probably use less and save from there by typing s then the file name you want to save to, then Enter. From How do I write all lines from less to a file?.


1

You can escape the double quotes printf '%b\n' "\"$(cat joyPhrase)\"" On my machine $ echo this is a file >> testfile $ printf '%b\n' "\"$(cat testfile)\"" "this is a file" Instead of using cat, you can use the redirect: $ printf '%b\n' "\"$(< testfile)\"" "this is a file"


2

espeak supports using --stdin to read from a pipe, so one option would be to change your function call to use that instead of parameters, and pipe the printf output into your function: speak(){ espeak -a 200 -s 130 -v la --stdout --stdin | aplay; } printf '%b\n' "$(cat joyPhrase)" | speak Or you can pass the output of your other command to speak's ...


0

#!/bin/bash set -x # prepare test data. mkdir -p ~/test_var_global cd ~/test_var_global echo "a"> core.1 echo "b"> core.2 echo "c"> core.3 var=0 coreFiles=$(find . -type f -name "core*") while read -r file; do # perform computations on $i ((var++)) done <<EOF $coreFiles EOF echo $var Result: ... + echo 3 3 it can work.


1

perl -00 -n -e 'print if (m/blue/i && m/green/i && m/yellow/i)' filename This uses perl's paragraph-reading mode (-00) to print only paragraphs containing all three words (with case-insensitive matches). a 'paragraph' is one or more lines of text, separated from other paragraphs by at least one blank line. e.g. I saved the text of your ...


5

Eric Blake answered on the bash-bugs mailing list: jobs is an interesting builtin - the set of jobs in a parent shell is DIFFERENT than the set of jobs in a subshell. Bash normally creates a subshell in order to do a pipeline, and since there are no jobs in that subshell, the hidden execution of jobs has nothing to report. Bash has code to ...


0

To remove all leading and trailing spaces from a given line thanks to a 'piped' tool, I can identify 3 different ways which are not completely equivalent. These differences concern the spaces between words of the input line. Dependanding on the excepted behaviour, you'll make your choice. Examples To explain the differences, let consider this dummy input ...



Top 50 recent answers are included