stdin

How can I tell if STDIN is connected to a terminal in Perl?

大憨熊 提交于 2019-12-04 03:35:18
How can I tell if STDIN is connected to a terminal in Perl? if (-t STDIN) { # stdin is connected } else { # stdin is not connected } I usually use this in conjunction with -t STDOUT, to find out if I'm running from an interactive shell, or from cron, to enable more output. You might also be interested in IO::Interactive to figure out if Perl thinks it is interacting with a user. Simply being connected to a tty doesn't mean the user is going to see what you do. One solution would be to use tty: [root@server] ~> tty /dev/pts/0 [root@server] ~> echo y | tty not a tty But not very pretty... 来源:

How to asynchronously read stdin?

跟風遠走 提交于 2019-12-04 01:46:49
问题 Is there an elegant way to fire an event when characters are available from System.in ? I'd like to avoid polling InputStream.available() . 回答1: You would have to create a separate thread that blocks in read until something is available. If you don't want to actually eat up the input, you would have to wrap it with an internal buffer, read into the buffer, shout, and when asked for the input, give back data from the buffer. You could solve it like this: InputStream stdin = System.in; //

How Postgresql COPY TO STDIN With CSV do on conflic do update?

天涯浪子 提交于 2019-12-04 01:39:10
问题 I want to do " on conflict (time) do update set name , description " but I have no idea when I use stdin with csv , I don't know what name equal what? and description equal what... table_a: xxx.csv: with open('xxx/xxx.csv', 'r', encoding='utf8') as f: sql = """ COPY table_a FROM STDIN With CSV on conflict (time) do update set name=??, description=??; """ cur.copy_expert(sql, f) conn.commit() 回答1: Thanks for every master's solution. this is my solution. sql = """ CREATE TABLE temp_h ( time ,

How can I test STDIN without blocking in Perl?

感情迁移 提交于 2019-12-04 00:59:22
I'm writing my first Perl app -- an AOL Instant Messenger bot that talks to an Arduino microcontroller, which in turn controls a servo that will push the power button on our sysadmin's server, which freezes randomly every 28 hours or so. I've gotten all the hard stuff done, I'm just trying to add one last bit of code to break the main loop and log out of AIM when the user types 'quit'. The problem is, if I try to read from STDIN in the main program loop, it blocks the process until input is entered, essentially rendering the bot inactive. I've tried testing for EOF before reading, but no dice.

How to pass Rscript -e a multiline string?

假如想象 提交于 2019-12-03 22:56:39
Is there a way to provide the code to Rscript -e in multiple lines? This is possible in vanilla R R --vanilla <<code a <- "hello\n" cat(a) code But using Rscript I get two different things depending on the R version. # R 3.0.2 gives two ignores Rscript -e ' quote> a <- 3+3 quote> cat(a, "\n") quote> ' # ARGUMENT 'cat(a,~+~"' __ignored__ # ARGUMENT '")' __ignored__ Rscript -e 'a <- 3+3;cat(a, "\n")' # ARGUMENT '")' __ignored__ # R 2.15.3 gives an ignore for the multiline, but it works with semicolons Rscript -e ' quote> a <- 3+3 quote> cat(a, "\n") quote> ' # ARGUMENT 'cat(a,~+~"\n")' __ignored

C| how to check if my input buffer(stdin) is empty?

梦想与她 提交于 2019-12-03 22:45:19
I want to know how to check if my input buffer(perhaps its called stdin) is empty or not. i dont want the program to stop if the buffer is empty, and i dont want the input to necessarily end with '\n', therefore just using scanf is not enough. i tried searching on google and on this website but no answer was enough. i tried using feof(stdin) like this: int main() { char c,x; int num; scanf("%c",&c); scanf("%c",&x); num=feof(stdin); printf("%d",num); } but all it did was printing 0 no matter the input. adding fflush(stdin) after the second scanf gave the same result. other answers suggested

Script to run against stdin if no arg; otherwise input file =ARGV[0]

天大地大妈咪最大 提交于 2019-12-03 19:34:31
问题 This works quite nicely - just wondered if there are any improvements to shorten it ? if (ARGV[0].nil?) then input=$< else input=File.new(ARGV[0],"r"); end ... # Do something with the input here, for example: input.each_line do |line| puts line end 回答1: You can eliminate the first five lines entirely. From Pickaxe $<: An object that provides access to the concatenation of the contents of all the files given as command-line arguments or $stdin (in the case where there are no arguments). $<

Why does Python read from the current directory when printing a traceback?

给你一囗甜甜゛ 提交于 2019-12-03 19:23:01
问题 $ echo "Your code is bad and you should feel bad" > "<stdin>" $ python Python 3.6.0 (default, Dec 28 2016, 19:53:26) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 2 + '2' Traceback (most recent call last): File "<stdin>", line 1, in <module> Your code is bad and you should feel bad TypeError: unsupported operand type(s) for +: 'int' and 'str' Why does Python confuse the string "<stdin>" with a file matching that

linux - write commands from one terminal to another

拥有回忆 提交于 2019-12-03 19:13:17
问题 I need to write commands from one terminal to another terminal. I tried these: echo -e "ls\n" > /proc/pid/fd/0 echo -e "ls\n" > /dev/pts/4 Which just prints the ls as output and doesn't execute. I tried these: chmod 777 /dev/tty4 ;echo "ls" > /dev/tty4 chmod 777 /dev/tty40 ;echo "ls" > /dev/tty40 Which don't seem to do anything Any ideas? [note that I don't want to touch the second terminal to accomplish this. only the first one] 回答1: Python code: #!/usr/bin/python import sys,os,fcntl,termios

ksh: how to probe stdin?

左心房为你撑大大i 提交于 2019-12-03 16:57:28
I want my ksh script to have different behaviors depending on whether there is something incoming through stdin or not: (1) cat file.txt | ./script.ksh (then do "cat <&0 >./tmp.dat" and process tmp.dat) vs. (2) ./script.ksh (then process $1 which must be a readable regular file) Checking for stdin to see if it is a terminal[ -t 0 ] is not helpful, because my script is called from an other script. Doing "cat <&0 >./tmp.dat" to check tmp.dat's size hangs up waiting for an EOF from stdin if stdin is "empty" (2nd case). How to just check if stdin is "empty" or not?! vladr EDIT: You are running on