Tag Info

Hot answers tagged

34

This is an unabashed yes. When one uses ssh to execute a command on a remote server it performs some kind of fancy internal input/output redirection. In fact, I find this to be one of the subtly nicer features of OpenSSH. Specifically, if you use ssh to execute an arbitrary command on a remote system, then ssh will map STDIN and sTDOUT to that of the command ...


18

I don't think you're going to get any more elegant than the tail -f /dev/null that you already suggested (assuming this uses inotify internally, there should be no polling or wakeups, so other than being odd looking, it should be sufficient). You need a utility that will run indefinitely, will keep its stdout open, but won't actually write anything to ...


15

Standard input and standard output are not commands. Imagine commands as machines in a factory with an assembly line. Most machines are designed to have one conveyor belt to feed data in and one conveyor belt to feed data out; they are the standard input and the standard output respectively. The standard error is an opening on the side of the machine where ...


14

The whole point of stdin is that it can be anything, it can for instance be a pipe, a network socket a regular file, a device, it can be half way through a regular file when your script is started... If you can't process the data in one pass, then you limit yourself to seekable files, that is regular files and a few device files, or will have to store the ...


13

A convenient way of piping data between hosts when you don't need to worry about security over the wire is using netcat on both ends on the connection. This also lets you set them up asynchronously: On the "receiver" (really, you'll have two-way communication, but it's easier to think of it like this), run: nc -l -p 5000 > /path/to/backupfile.tar And ...


11

A very powerful tool for creating uni- and bidirectional connections is socat. For a short look at the possibilities, look at the examples in its manpage. It replaces netcat and similar tools completely and has support for ssl encrypted connections. For beginners, it might be not simple enough, but it is at least good to know that it exists.


10

Like this: ( echo a echo b echo c ) | real_program That runs the three echo commands in a subshell and pipes the subshell's output to the script. Subshells are syntactical sugar for bash -c 'echo a; echo b; echo c' | real_program.


8

sleep 2147483647 | program > output & Yes, 2^31-1 is a finite number, and it won't run forever, but I'll give you $1000 when the sleep finally times out. (Hint: one of us will be dead by then.) no temporary files; check. no busy-waiting or periodic wakeups; check no exotic utilities; check. as short as possible. Okay, it could be shorter.


8

The cleanest way to do this is probably to look for something like bash's --rcfile option. Put your custom commands in your custom file and pass it to the interactive shell to run on start-up. If no such option exists you can also try the following: cat custom_commands_file - | ./shell_executable cat will interpret - as stdin.


7

Use empty: With the password safely stored (it's a way of saying...) $ echo password > pwd-file Start process with empty. (You would omit -L log in the real case.) $ empty -f -i fifo1 -o fifo2 -L log curl -u user http://example.com Send the contents of pwd-file to empty's input pipe, which the process sees as both its stdin and /dev/tty. $ empty ...


7

You may be looking for a named pipe. mkfifo f { echo 'V cebqhpr bhgchg.' sleep 2 echo 'Urer vf zber bhgchg.' } >f rot13 < f Writing to the pipe doesn't start the listening program. If you want to process input in a loop, you need to keep a listening program running. while true; do rot13 <f >decoded-output-$(date +%s.%N); done Note ...


7

You will have to store the file contents somehow. You can use a variable. content=`cat` x=`echo "$content" | grep -o '>' | wc -l` y=`echo "$content" | grep -o '<' | wc -l` if [ "$x" -ne "$y" ]; then echo "Mismatch!" fi echo $x echo $y Or a temporary file (necessary if example.html contains null bytes). tmp=`mktemp` trap "rm $tmp" EXIT x=`grep -o ...


7

Found this clever answer in a similar question at stackoverflow (echo -e "cmd 1\ncmd 2" && cat) | ./shell_executable This does the trick. cat will pump in the output of echo into input stream of shell_executable and wait for more inputs until EOF.


6

I observe this behavior under OpenSSL 1.0.0e on Ubuntu 11.10, whereas OpenSSL 0.9.8k and 0.9.8t output just the hash. OpenSSL's command line is not designed to be flexible, it's more of a quick-and-dirty way to perform cryptographic calculations from the command line. If you want to use OpenSSL, filter the output: echo -n "foo" | openssl dgst -sha1 | sed ...


6

I don't think OpenOffice can be convinced to read from its standard input. But that doesn't matter. Just write the data to a temporary file. You don't want the passwords to be written to disk. That's fine. Write them to a file that isn't stored on disk. Many systems use an in-memory filesystem (tmpfs) for /tmp. Solaris has been doing that for ages; Linux ...


5

Check if a filename is given as an argument, or else read from sys.stdin. Something like this: if sys.argv[1]: f = open(sys.argv[1]) else: f = sys.stdin It's similar to Mikel's answer except it uses the sys module. I figure if they have it in there it must be for a reason...


5

I assume you want to edit the mail before it is sent? In that case piping is not going to work because mutt receives an EOF when the pipe closes. Either use an actual file or use process substitution, a ksh93 feature also available in bash and zsh, e.g.: mutt -i<(git request-pull HEAD https://...) -s SUBJECT invalid@example.org


5

Simplified, stdbuf is a wrapper around stdio functionality. Line buffering of input streams is undefined in stdio; I can find no standards document that says what it means, so it is literally meaningless as far as the standards go. Assuming behavior analogous to stdout line buffering, the line buffering of stdin would require calling read() once for each ...


5

Yes, as required by POSIX, commands started in background with & have their standard input redirected from /dev/null. And indeed { cmd <&3 3<&- & } 3<&0 is the most obvious way to work around it. It's not clear why you'd want to run part of pipeline in background though.


5

You need to Run python interactively even though its stdin is not a terminal: use python -i keep the writing end of the pipe open, otherwise python will detect EOF and exit. So: python -i < p1 And elsewhere: exec 3> p1 echo '1j*1j' >&3 ... # and when done: exec 3>&1


5

I'd make heavier use of I/O redirection: #!/bin/bash [[ $1 ]] && [[ ! -f $1 ]] && echo "file $1 dne" && exit 1 [[ $1 ]] && exec 3<$1 || exec 3<&0 [[ $2 ]] && exec 4>$2 || exec 4>&1 fgrep -v "stuff" <&3 >&4 Explanation [[ $1 ]] && [[ ! -f $1 ]] && echo "file $1 dne" ...


4

A terminal doubles as two things: an input device (such as a keyboard) and a display device (such as a monitor). When you read from the terminal, you get what comes from the input device. When you write to the terminal, the data goes onto the display device. There is no general way of forcing input into a terminal. There is rarely any need to do so. If you ...


4

standard input is a command that allows user to write to a file Not a command, but a stream. Standard in and out are like mail boxes. When a program starts, it's given a box to recieve and a box to send mail. Usually, input comes from the keyboard and and is put in the in-box, mail put in the out-box ends up on your terminal screen. standard output is a ...


4

When a writer writes to a pipe and the pipe is full (its size is limited to a few kilobytes), its process blocks until one of the readers frees some space. Similarly, when a reader reads from a pipe, its process blocks until there is something there. There are also asynchronous writes and reads that a programmer can use to queue up these reads and writes. ...


4

For your command to detect eof, it has to read from stdin. So presumably it is expecting some input. So it sounds like what you need is not an empty input (/dev/null is exactly meant for that), but input that never comes. It can be simulated with a pipe where nobody is ever going to write on the other end like: sleep 999999999 | the-command Or to avoid ...


4

Your second line is incorrect, and overly complex anyway. The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively, so to read from stdin you'd want to have while read CMD <&0; do However, since stdin is the default input for read, while read CMD; do is really the simplest way to go. This way, you can manually enter the ...


4

Try something like this: $ ssh -t yourserver "$(<your_script)" The -t forces a tty allocation, $(<some_file) reads the whole file and in this cases passes the content as one argument to ssh, which will be executed by the remote user's shell. Works for me, not sure if it's universal though.


4

exec 3<&0 while read line do mplayer "$line" <&3 done < <(find-me-random-mp3s) This copies terminal input to file descriptor 3. In the while loop, stdin is everywhere read from your find-me-random-mp3s program except for the mplayer line which gets its stdin from file descriptor 3 which is your terminal. Hence, you can still ...


4

I like @Stephane's answer for the general case, but here's something more appropriate for your specific example. Start up MySQL interactive mode and use the source command to run SQL scripts. $ mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 947 Server version: 5.1.73-1 (Debian) Type ...


4

I'm not familiar with AutoHotKey, so if you can't find any solution there, read on. POSIX specifies the read command, which allows taking a line of input while stifling the terminal echo (that's what you see when you type) with -s. This is also a bash built-in, but you might check your system to see if it is present as a standalone. Otherwise, looking ...



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