Tag Info

Hot answers tagged

13

Yes! You can use a process pipe |. ./preprocess.sh | while IFS= read -r line do ./research.sh "$line" & done A process pipe passes the standard output (stdout) of one process to the standard input (stdin) of the next. You can optionally put a newline character following a | and extend the command to the next line. Note: a|b is ...


9

man bash says: ... redirection operators may precede or appear anywhere within a simple command or may follow a command. while is not a simple command.


7

Always try and break things down into there simplest steps, then try and put things together after. To start the first part, I'd construct myself a sample file. $ echo -e "line 1 ABA\nline 2 ABB\nline 3 CCC\n" > xxx $ cat xxx line 1 ABA line 2 ABB line 3 CCC So now we have file xxx. Now we need to use something that can act as program yyy. Unix is ...


6

You can use bash process substitution: while IFS= read -r line do ./research.sh "$line" & done < <(./preprocess.sh) Some advantages of process substitution: No need to save temporary files. Better performance. Reading from another process often faster than writing to disk, then read back in. Save time to computation since when it is ...


6

On an older RHEL system I've got, /bin/cat does not loop for cat x >> x. cat gives the error message "cat: x: input file is output file". I can fool /bin/cat by doing this: cat < x >> x. When I try your code above, I get the "looping" you describe. I also wrote a system call based "cat": #include <sys/types.h> #include ...


6

You were really close: tr "o" "a" < <(echo "Foo") The substitution <() makes a file descriptor and just pastes the path to the shell. For comprehension just execute: <(echo blubb) You will see the error: -bash: /dev/fd/63: Permission denied That's why it just pastes /dev/fd/63 into the shell and /dev/fd/63 is not excutable, because it's ...


5

You can in zsh, not in bash and choroba has already pointed you to the documentation, but if you want to have the redirection before, you can do things like: < file eval ' while IFS= read -r line; do ... done' Or (on systems with support for /dev/fd/n): < file 3<< 'EOF' . /dev/fd/3 while IFS= read -r line; do ... done EOF (not ...


4

Yes, this is job for tee: rpm -qa | tee file | wc -l In this construction a | b a's stdout goes to stdin of b. In case of a > file | b all output form a goes to file and nothing goes to b stdin. tee command make a copy of all it receives on stdin to both file and stdout.


3

You can use that substitution, if you want to precede the input: cat lines | while read line; do echo "line: $line"; done


3

Oh, you're a teacher...!!! mysql -uUSERNAME -p < sqlfile.sql sort < domain_list.txt > sorted_domain_list.txt Counting words of a text: root@debian:/home/mohsen# wc -c << EOF > This is a simple lookup program > for good (and bad) restaurants > in Cape Town. > EOF 90


3

Yes, this is a job for tee: rpm -qa | tee file | wc -l Shell redirection (>) is just that — redirection — and you can only point the output stream to one other place at a time. There's nothing left for the | to see at that point. tee is made for just this purpose, where you want to split the stream into two parts, one going into a file and one still on ...


3

Instead of using nohup, you could have your script ask these questions interactively and then background and disown the remainder of whatever else it has to do. Example $ more a.bash #!/bin/bash read a echo "1st arg: $a" read b echo "2nd arg: $b" ( echo "I'm starting" sleep 10 echo "I'm done" ) & disown Sample run: $ ./a.bash 10 1st arg: 10 20 ...


3

General, you can always do: <command> | ssh user@remote-server "cat > output.txt" It saves output of <command> to output.txt file in remote server. In your case, on Server-1: echo "qwerty" | ssh user@Server-2 "cat > output.txt" If two servers have no connectivity, but you can ssh to both servers, then from local machine, you can do: ...


2

I think sort uses temporary files automatically. Temproaries are mentioned in several places in the man and info pages for sort, including: --compress-program=PROG compress temporaries with PROG; decompress them with PROG -d -T, --temporary-directory=DIR use DIR for temporaries, not $TMPDIR or /tmp; multiple options ...


2

result=$(grub-md5-crypt | grep xy) echo $result If grub-md5-crypt prints to stderr use: result=$(grub-md5-crypt 2>&1 | grep xy) echo $result


2

There are multiple usecases, this is one of them. Applying a kernel patch: cd linux-3.16.2 && patch -p1 < patch-3.16.2-3


2

You can use exec to redirect the stdin. In a script: exec < <(cat lines) while read line ; do echo "line: $line"; done You can't use in a login shell though (it will dump the file on the stdout and exit). In that case you can open a different file descriptor: exec 3< <(cat lines) while read -u 3 line ; do echo "line: $line"; done For ...


1

Good ol' ed: $ cat file.txt foo bar baz $ cat edcommands.txt ,s/ba/ta/ w q $ ed file.txt < edcommands.txt 12 12 $ cat file.txt foo tar taz (Though of course for such a short command you would just do printf ",s/ba/ta/\nw\nq" | ed file.txt. ;) )


1

Standard Input (stdin) Redirection In Linux, cat command is used to print the content of a file. However, if cat command is executed without any input argument then by default it tries to read from the standard input (stdin) and because stdin is linked to the keyboard therefore it just waits for user to type something. # cat Hello World! This is ...


1

If it doesn't work in your shell script you might want to use bash. Just add: #!/bin/bash It must be in the first line of your file! This means your script will be using bash interpreter other than normal shell's one (/bin/sh). Completing noEntry's answer, you can also save output to a file. grub-md5-crypt | grep xy > output Or: ...


1

I tested the behavoir with a huge file (comma separated, csv, 2173762 lines, 186MB) awk piped to sort gives me, 49611 syscalls and: real 0m5.134s user 0m5.048s sys 0m0.080s awk to a temporary file and then sort the file in a 2nd step gives me 49719 syscalls and: real 0m6.006s user 0m5.836s sys 0m0.152s Even the other way, sort ...


1

part of fuser is sent to stdout (standard output), and part to standard error. how is output split ? piping mechanism only catch stdout. plain fuser mybox $ fuser / /: 350r 356r 357r 364r 10484rc 10485r now redirecting, see pid are in a, while type of file (c or r) is in stderr. mybox $ fuser / > a /: ...


1

I found the Bash One-Liners Explained series very useful in understanding more about all this stuff. Specifically the article linked above is all about input redirection. To solve the specific example above: > tr "o" "a" <<< $(echo "Foo") Faa


1

You're running into an output buffering problem. sed normally buffers its output when not writing to a terminal, so nothing gets written to the file until the buffer fills up (probably every 4K bytes). Use the -u option to sed to unbuffer output. clock -sf 'S%A, %B %d. %I:%M %P' | sed -u 's/\b0\+\([0-9]\+\)/\1/g' > testfile


1

I am so sorry, I just woke up. I was piping the output through grep and had my redirection on the wrong side of the pipe! I was redirecting the stderr of grep not yum! Mods, feel free to delete this, or leave it up if it has any value.


1

rpm -qa > file ; wc -l file should do what you want.


1

netcat springs to mind; it may be the more sensible choice (given the no-overhead, no compression approach to network communications) on your low-spec receiving machine. A nice usage example can be found here: http://stackoverflow.com/questions/4113986/example-of-using-named-pipes-in-linux-bash


1

If you know you're going to want to do this in advance ... Create a script session on your home computer (using -f to flush buffer): script -f output.txt rsync -vr /media/master /media/slave (Ctrl+D to finish the script session when get home) At work you can track output.txt: tail -f output.txt



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