stdin

How to read mutliline input from stdin into variable and how to print one out in shell(sh,bash)?

末鹿安然 提交于 2020-01-20 12:52:54
问题 What I want to do is the following: read in multiple line input from stdin into variable A make various operations on A pipe A without losing delimiter symbols ( \n , \r , \t ,etc) to another command The current problem is that, I can't read it in with read command, because it stops reading at newline. I can read stdin with cat , like this: my_var=`cat /dev/stdin` , but then I don't know how to print it. So that the newline, tab, and other delimiters are still there. My sample script looks

How to pass the value of a variable to the stdin of a command?

喜欢而已 提交于 2020-01-19 04:42:26
问题 I'm writing a shell script that should be somewhat secure i.e. does not pass secure data through parameters of commands and preferably does not use temporary files. How can I pass a variable to the stdin of a command? Or, if it's not possible, how to correctly use temporary files for such task? 回答1: Something as simple as: echo "$blah" | my_cmd 回答2: Passing a value to stdin in bash is as simple as: your-command <<< "$your_variable" Always make sure you put quotes around variable expressions!

Splitting up lines into ints

空扰寡人 提交于 2020-01-17 07:38:10
问题 I have a file that I read from, it contains a bunch of lines each with a different number of integers, I'm having trouble splitting it up into a vector of a vector of ints. This is my current code. std::vector<int> read_line() { std::vector<int> ints; int extract_int; while((const char*)std::cin.peek() != "\n" && std::cin.peek() != -1) { std::cin >> extract_int; ints.push_back(extract_int); } return ints; } std::vector<std::vector<int> > read_lines() { freopen("D:\\test.txt", "r", stdin);

Boost asio - async reading established number of chars from stdin

吃可爱长大的小学妹 提交于 2020-01-16 07:15:28
问题 I wanna write boost::asio app which is reading from stdin with boost::asio::streambuf. Anyway the only function which works on streambuf made from STDIN_FILENO is boost::asio::async_read_until. The other ones throws errors. Is there any possibility to read 100 first character from stdin with boost asio function? 回答1: In principle this just works #include <boost/asio.hpp> #include <boost/asio/posix/stream_descriptor.hpp> using namespace boost::asio; using boost::system::error_code; #include

When calling Python as a subprocess, can I force it to run in interactive mode?

元气小坏坏 提交于 2020-01-16 01:16:21
问题 I'm on a mac using Scala, and I want to create a Python interpreter as a subprocess that my program interacts with. I've been using Process with a ProcessIO, but python insists on running in non-interactive mode. So it only does anything after I close down its input and kill the process. Is there a way to force it to run in interactive mode, so that I can keep the Python process alive and interact with it? This sample code (which I'm pasting into a Scala repl) shows the problem: import scala

Multi-line stdin - C++

十年热恋 提交于 2020-01-15 14:31:50
问题 So if i have a program that takes stdin such as 1 5 2 4 How exactly can i go through each line and say print that value, This is what im thinking: #include <iostream> using namespace std; int main() { while ( // input has ended// ) { cout << //current line// //increment to next line// } return 0; } Is there such a way or no? 回答1: The pattern that I like is: while (!cin.eof()) { string line; getline(cin, line); if (cin.fail()) { //error break; } cout << line << endl; } As in the other answers,

Multi-line stdin - C++

你。 提交于 2020-01-15 14:29:19
问题 So if i have a program that takes stdin such as 1 5 2 4 How exactly can i go through each line and say print that value, This is what im thinking: #include <iostream> using namespace std; int main() { while ( // input has ended// ) { cout << //current line// //increment to next line// } return 0; } Is there such a way or no? 回答1: The pattern that I like is: while (!cin.eof()) { string line; getline(cin, line); if (cin.fail()) { //error break; } cout << line << endl; } As in the other answers,

stdin allows to read with the EOF flag set

萝らか妹 提交于 2020-01-15 08:53:12
问题 On my platform, the following code allows me to successfully read from stdin even if its end-of-file flag is set, which also remains set after the read. To reproduce the behavior, first type the end-of-file shortcut ( Ctrl + D on Unix, Ctrl + Z on Windows) and then type a normal character. #include <stdio.h> // first type the shortcut for EOF, then a single character int main(void) { getchar(); printf("feof(stdin): %s\n", feof(stdin) ? "true" : "false"); int ch = getchar(); if (ch == EOF)

Accept user Input os.stdin to container using Golang docker SDK - Interactive Container

亡梦爱人 提交于 2020-01-15 07:27:09
问题 My last resort is asking here. I'm new to Golang and I've made simple programs. I'm trying to do the following: Using golang: 1 - run a container 2 - accept input stdin to the container The example I want to use is the hashicorp/terraform docker image, I want to do a simple terraform apply but I need to wait for user input below is the code I have working so far...anyone trying the exact code below needs to update the AWS environment variables or change the terraform test file to another

PHP giving a trailing “=” on each line after reading from stdin

风流意气都作罢 提交于 2020-01-15 03:45:31
问题 The contents of stdin is getting corrupted with word wrapping and trailing "=" throughout which obviously breaks the URL that I need to post. I need to extract a URL/link from an email then post the URL. So, I'm piping my email to a php script in cpanel using a standard code snip I've seen all over the internet: $fd = fopen("php://stdin", "r"); $email = ""; // This will be the variable holding the data. while (!feof($fd)) { $email .= trim(fread($fd, 1024)); } fclose($fd); Then dumping the