stdin

Send string to stdin

ε祈祈猫儿з 提交于 2019-11-26 08:42:59
问题 Is there a way to effectively do this in bash: /my/bash/script < echo \'This string will be sent to stdin.\' I\'m aware that I could pipe the output from the echo such as this: echo \'This string will be piped to stdin.\' | /my/bash/script 回答1: You can use one-line heredoc cat <<< "This is coming from the stdin" the above is the same as cat <<EOF This is coming from the stdin EOF or you can redirect output from a command, like diff <(ls /bin) <(ls /usr/bin) or you can read as while read line

fflush(stdin) function does not work

旧巷老猫 提交于 2019-11-26 08:37:51
问题 I can\'t seem to figure out what\'s wrong with this code: #include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #define MAX 100 #define TRUE 1 #define FALSE 0 char sect_cat; char customer_name[MAX]; char customer_number[MAX]; /* error handling is easier */ int prev_unit = 0; int current_unit = 0; int consumed = 0; int set = FALSE; float init_bill; float tax; float total_bill; void get_userinfo() { printf(\"Enter sector category: \"); scanf(\"%c\", &sect_cat); printf(\

Writing to stdin and reading from stdout (UNIX/LINUX/C Programming)

╄→尐↘猪︶ㄣ 提交于 2019-11-26 08:27:53
问题 I was working on an assignment where a program took a file descriptor as an argument (generally from the parent in an exec call) and read from a file and wrote to a file descriptor, and in my testing, I realized that the program would work from the command-line and not give an error if I used 0, 1 or 2 as the file descriptor. That made sense to me except that I could write to stdin and have it show on the screen. Is there an explanation for this? I always thought there was some protection on

Python 3: How to specify stdin encoding [duplicate]

给你一囗甜甜゛ 提交于 2019-11-26 08:22:47
问题 This question already has answers here : How to change the stdin and stdout encoding on Python 2 (5 answers) Closed 9 months ago . While porting code from Python 2 to Python 3, I run into this problem when reading UTF-8 text from standard input. In Python 2, this works fine: for line in sys.stdin: ... But Python 3 expects ASCII from sys.stdin , and if there are non-ASCII characters in the input, I get the error: UnicodeDecodeError: \'ascii\' codec can\'t decode byte .. in position ..: ordinal

how to redirect STDOUT to a file in PHP?

妖精的绣舞 提交于 2019-11-26 07:18:54
问题 The code below almost works, but it\'s not what I really meant: ob_start(); echo \'xxx\'; $contents = ob_get_contents(); ob_end_clean(); file_put_contents($file,$contents); Is there a more natural way? 回答1: It is possible to write STDOUT directly to a file in PHP, which is much easier and more straightforward than using output bufferering. Do this in the very beginning of your script: fclose(STDIN); fclose(STDOUT); fclose(STDERR); $STDIN = fopen('/dev/null', 'r'); $STDOUT = fopen('application

setvbuf not able to make stdin unbuffered

寵の児 提交于 2019-11-26 06:40:20
问题 My main intention was to make getchar return as soon as it gets a character instead of waiting for the ENTER key. I tried this int main() { setvbuf(stdin,NULL,_IONBF,0); getchar(); return 0; } Comparing this with the prototype of setvbuf setvbuf ( FILE * stream, char * buffer, int mode, size_t size ); it should set stdin to unbuffered mode. But still getchar() keeps waiting for ENTER I\'ve seen related posts like this Printing while reading characters in C which are suggesting alternate

How do I check if stdin has some data?

孤街浪徒 提交于 2019-11-26 06:35:30
问题 In Python, how do you check if sys.stdin has data or not? I found that os.isatty(0) can not only check if stdin is connected to a TTY device, but also if there is data available. But if someone uses code such as sys.stdin = cStringIO.StringIO(\"ddd\") and after that uses os.isatty(0) , it still returns True. What do I need to do to check if stdin has data? 回答1: On Unix systems you can do the following: import sys import select if select.select([sys.stdin,],[],[],0.0)[0]: print "Have data!"

How to read from a file or STDIN in Bash?

送分小仙女□ 提交于 2019-11-26 06:10:19
问题 The following Perl script ( my.pl ) can read from either the file on the command line args or from STDIN: while (<>) { print($_); } perl my.pl will read from STDIN, while perl my.pl a.txt will read from a.txt . This is very convenient. Wondering is there an equivalent in Bash? 回答1: The following solution reads from a file if the script is called with a file name as the first parameter $1 otherwise from standard input. while read line do echo "$line" done < "${1:-/dev/stdin}" The substitution

Running an interactive command from within python

大兔子大兔子 提交于 2019-11-26 05:34:43
问题 I have a script that I want to run from within python (2.6.5) that follows the logic below: Prompt user for password. Looks like (\"Enter password: \") (*Note: Input does not echo to screen) Output irrelevant information Prompt user for response (\"Blah Blah filename.txt blah blah (Y/N)?: \") The last prompt line contains text which I need to parse (filename.txt). The response provided doesn\'t matter (The program could actually exit here without providing one, as long as I can parse the line

Multiple inputs and outputs in python subprocess communicate

橙三吉。 提交于 2019-11-26 04:55:02
问题 I need to do something like this post, but I need to create a subprocess that can be given input and give output many times. The accepted answer of that post has good code... from subprocess import Popen, PIPE, STDOUT p = Popen([\'grep\', \'f\'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) grep_stdout = p.communicate(input=b\'one\\ntwo\\nthree\\nfour\\nfive\\nsix\\n\')[0] print(grep_stdout.decode()) # four # five ...that I would like to continue like this: grep_stdout2 = p.communicate(input=b\