stdin

How to pipe input to a Bash while loop and preserve variables after loop ends

南楼画角 提交于 2019-11-26 04:39:33
问题 Bash allows to use: cat <(echo \"$FILECONTENT\") Bash also allow to use: while read i; do echo $i; done </etc/passwd to combine previous two this can be used: echo $FILECONTENT | while read i; do echo $i; done The problem with last one is that it creates sub-shell and after the while loop ends variable i cannot be accessed any more. My question is: How to achieve something like this: while read i; do echo $i; done <(echo \"$FILECONTENT\") or in other words: How can I be sure that i survives

Continuously read from STDOUT of external process in Ruby

混江龙づ霸主 提交于 2019-11-26 04:31:17
问题 I want to run blender from the command line through a ruby script, which will then process the output given by blender line by line to update a progress bar in a GUI. It\'s not really important that blender is the external process whose stdout I need to read. I can\'t seem to be able to catch the progress messages blender normally prints to the shell when the blender process is still running, and I\'ve tried a few ways. I always seem to access the stdout of blender after blender has quit, not

Read binary data from std::cin

筅森魡賤 提交于 2019-11-26 04:28:06
问题 What is the easiest way to read binary (non-formated) data from std::cin into either a string or a stringstream ? 回答1: std::cin is not opened with ios_binary. If you must use cin, then you need to reopen it, which isn't part of the standard. Some ideas here: http://compgroups.net/comp.unix.programmer/How-can-I-reopen-std-cin-and-std-cout-in-binary-mode. Once it's binary, you can use cin.read() to read bytes. If you know that in your system, there is no difference between text and binary (and

How to make ssh receive the password from stdin

送分小仙女□ 提交于 2019-11-26 03:58:57
问题 How can you make SSH read the password from stdin, which it doesn\'t do by default? 回答1: You can't with most SSH clients. You can work around it with by using SSH API's, like Paramiko for Python. Be careful not to overrule all security policies. 回答2: based on this post you can do: Create a command which open a ssh session using SSH_ASKPASS (seek SSH_ASKPASS on man ssh) $ cat > ssh_session <<EOF export SSH_ASKPASS="/path/to/script_returning_pass" setsid ssh "your_user"@"your_host" EOF NOTE: To

How can I “intercept” Ctrl+C in a CLI application?

安稳与你 提交于 2019-11-26 03:57:14
How can I intercept Ctrl + C (which normally would kill the process) in a CLI (command line interface) Java application? Does a multi-platform solution exist (Linux, Solaris, Windows)? I'm using Console 's readLine() , but if necessary, I could use some other method to read characters from standard input. VonC Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { /* my shutdown code here */ } }); This should be able to intercept the signal, but only as an intermediate step before the JVM completely shutdowns itself, so it may not be what you are looking after. You need to use

How do I write to a Python subprocess&#39; stdin?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 03:22:28
问题 I\'m trying to write a Python script that starts a subprocess, and writes to the subprocess stdin. I\'d also like to be able to determine an action to be taken if the subprocess crashes. The process I\'m trying to start is a program called nuke which has its own built-in version of Python which I\'d like to be able to submit commands to, and then tell it to quit after the commands execute. So far I\'ve worked out that if I start Python on the command prompt like and then start nuke as a

How to open every file in a folder?

我与影子孤独终老i 提交于 2019-11-26 03:02:52
问题 I have a python script parse.py, which in the script open a file, say file1, and then do something maybe print out the total number of characters. filename = \'file1\' f = open(filename, \'r\') content = f.read() print filename, len(content) Right now, I am using stdout to direct the result to my output file - output python parse.py >> output However, I don\'t want to do this file by file manually, is there a way to take care of every single file automatically? Like ls | awk \'{print}\' |

How to detect if Console.In (stdin) has been redirected?

与世无争的帅哥 提交于 2019-11-26 01:58:19
I want to write a console application that have a different behavior depending if the input is coming from keyboard or from, say, a file. Is it possible? What's the most elegant way to do it in C#? You can find out by p/invoking the Windows FileType() API function. Here's a helper class: using System; using System.Runtime.InteropServices; public static class ConsoleEx { public static bool IsOutputRedirected { get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stdout)); } } public static bool IsInputRedirected { get { return FileType.Char != GetFileType(GetStdHandle(StdHandle

Why do I get the “Unhandled exception type IOException”?

末鹿安然 提交于 2019-11-26 01:36:43
问题 I have the following simple code: import java.io.*; class IO { public static void main(String[] args) { BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { System.out.println(userInput); } } } And I get the following error message: ---------- 1. ERROR in io.java (at line 10) while ((userInput = stdIn.readLine()) != null) { ^^^^^^^^^^^^^^^^ Unhandled exception type IOException ---------- 1 problem (1

Setting smaller buffer size for sys.stdin?

时光怂恿深爱的人放手 提交于 2019-11-26 01:36:20
问题 I\'m running memcached with the following bash command pattern: memcached -vv 2>&1 | tee memkeywatch2010098.log 2>&1 | ~/bin/memtracer.py | tee memkeywatchCounts20100908.log to try and track down unmatched gets to sets for keys platform wide. The memtracer script is below and works as desired, with one minor issue. Watching the intermediate log file size, memtracer.py doesn\'t start getting input until memkeywatchYMD.log is about 15-18K in size. Is there a better way to read in stdin or