stdin

What's the point of ARGV in Ruby?

≡放荡痞女 提交于 2019-11-26 23:11:43
问题 What's the point of ARGV in Ruby? first, second, third = ARGV puts "The script is called: #{$0}" puts "Your first variable is: #{first}" puts "Your second variable is: #{second}" puts "Your third variable is: #{third}" What's the point of this when to run the file I need to do: ruby ex1.rb and to put in the first, second and third variables I need to type in ruby ex1.rb blah blah blah How does this benefit at all the person running the program? They can't do it anyway since I'd assume it be

How to feed mysql queries from bash

谁说我不能喝 提交于 2019-11-26 23:01:49
问题 I'm trying to make a bash script that creates a mysql user and database but I can't find a way to feed the sql into mysql, I'm trying with this format: mysql < echo "query" But that is not working, see the example below: mysql --host=localhost --user=user --password=password < echo "CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'jakdJxct8W'; CREATE DATABASE IF NOT EXISTS 'testuser_dev' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin; GRANT ALL PRIVILEGES ON 'testuser_dev' . * TO 'testuser'@

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

纵饮孤独 提交于 2019-11-26 22:52:58
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 stdin/stdout and you certainly can't fprintf to stdin or fgets from stdout. #include <stdlib.h>

Running external program with redirected stdin and stdout from Java

你离开我真会死。 提交于 2019-11-26 22:36:24
I'm trying to running an external program from a Java program and I'm having trouble. Basically what I'd like to do would be this: Runtime.getRuntime().exec("./extprogram <fileIn >fileOut"); However I've found that that doesn't work - Java apparentls needs to use a Process with input and output streams and other things which I'm not experienced with. I've looked at a number of examples across the internet (many of which are from SO), and there doesn't seem to be a simple standard way of doing this, which for someone who doesn't fully understand what's going on, can be quite frustrating. I'm

Python 3: How to specify stdin encoding [duplicate]

三世轮回 提交于 2019-11-26 22:32:50
This question already has an answer here: How to change the stdin and stdout encoding on Python 2 4 answers 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 not in range(128) For a regular file, I would specify the encoding when opening the file: with open('filename', 'r', encoding=

Sending to the stdin of a program in python3

北慕城南 提交于 2019-11-26 22:25:53
问题 I have to files, main.py and child.py . I am trying to send a string to the stdin of main.py . This is my incomplete code: main.py from subprocess import * import time def main(): program = Popen(['python.exe'. 'child.py', 'start']) while True: #waiting for'1' to be sent to the stdin if sys.stdin == '1': print('text) if __name__ == '__main__': main() child.py import sys if sys.argv[1] == 'start': inp = input('Do you want to send the argument?\n').lower() if inp == 'no': sys.exit() elif inp ==

Interactive input/output using python

吃可爱长大的小学妹 提交于 2019-11-26 22:15:27
I have a program that interacts with the user (acts like a shell), and I want to run it using python subprocess module interactively. That means, I want the possibility to write to stdin and immediately get the output from stdout. I tried many solutions offered here, but none of them seems to work for my needs. The code I've written based on Running an interactive command from within python import Queue import threading import subprocess def enqueue_output(out, queue): for line in iter(out.readline, b''): queue.put(line) out.close() def getOutput(outQueue): outStr = '' try: while True: #Adds

How to change the stdin and stdout encoding on Python 2

Deadly 提交于 2019-11-26 21:16:11
问题 I'm using Windows and Linux machines for the same project. The default encoding for stdin on Windows is cp1252, and on Linux it is utf-8. I would like to change everything to utf-8. Is it possible? How can I do it? This question is about Python 2; for Python 3, see Python 3: How to specify stdin encoding 回答1: You can do this by not relying on the implicit encoding when printing things. Not relying on that is a good idea in any case -- the implicit encoding is only used when printing to stdout

Does reading from stdin flush stdout?

旧街凉风 提交于 2019-11-26 21:01:22
stdout is line-buffered when connected to a terminal, but I remember reading somewhere that reading (at least from stdin) will automatically flush stdout. All C implementations that I have used have done this, but I can't find it in the standard now. It does make sense that it works that way, otherwise code like this: printf("Type some input: "); fgets(line, sizeof line, stdin); would need an extra fflush(stdout); So is stdout guaranteed to be flushed here? EDIT: As several replies have said, there seems to be no guarantee in the standard that the output to stdout in my example will appear

Understanding stdin stdout stderr [duplicate]

陌路散爱 提交于 2019-11-26 20:24:42
问题 This question already has an answer here: Confused about stdin, stdout and stderr? 10 answers I'm trying to understand stdin stdout and stderr . I see them used in people's code all the time and I can't understand exactly what they are. I am assuming that they have something to do with input/output but have been searching for an explanation online and can't find one. Does anybody know of a good link with an explanation or if it is simple enough to explain it would be a great help to me. Since