stdout

Python, logging print statements while having them print to stdout

一世执手 提交于 2019-11-30 12:17:38
问题 I have a long python script that uses print statements often, I was wondering if it was possible to add some code that would log all of the print statements into a text file or something like that. I still want all of the print statements to go to the command line as the user gets prompted throughout the program. If possible logging the user's input would be beneficial as well. I found this which somewhat answers my question but makes it where the "print" statement no longer prints to the

Test stdout and stderr redirection in bash script

半城伤御伤魂 提交于 2019-11-30 11:17:29
I would like to test in my bash script where stdout and stderr are directed, or more precisely, if they have been redirected. Have you an idea ? The $* bash variable don't give me this info. Mat You should be able to use the -t test switch to tell if the output streams are tty s or not: if [ -t 1 ] ; then echo stdout is a terminal else echo stdout is not a terminal fi Use -t 0 for stdin . Use -t 2 for stderr . Technically there is no way of telling whether stdin/stdout/stderr are "redirected" because you don't know what's invoking your script. If it's not being invoked from another shell,

Call an exe from Java with passing parameters with writing to stdout and reading from stdin

旧时模样 提交于 2019-11-30 10:00:11
问题 I have read this question: Java Programming: call an exe from Java and passing parameters And this answer is good enough https://stackoverflow.com/a/5604756/2674303 But I additionally want to pass parameters to stdin of external process and read from stdout of this process. How can I do this? My efforts : main method: public class ProcessBuilderTest { public static void main(String[] args) throws IOException, InterruptedException { ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\

How to execute a command and get return code stdout and stderr of command in C++

怎甘沉沦 提交于 2019-11-30 09:58:30
Given the following answer (first c++11 answer): How to execute a command and get output of command within C++ using POSIX? Here is the implementation for your convinience: #include <cstdio> #include <iostream> #include <memory> #include <stdexcept> #include <string> #include <array> std::string exec(const char* cmd) { std::array<char, 128> buffer; std::string result; std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); if (!pipe) throw std::runtime_error("popen() failed!"); while (!feof(pipe.get())) { if (fgets(buffer.data(), 128, pipe.get()) != nullptr) result += buffer.data(); } return

Why is sys.getdefaultencoding() different from sys.stdout.encoding and how does this break Unicode strings?

一曲冷凌霜 提交于 2019-11-30 09:44:57
I spent a few angry hours looking for the problem with Unicode strings that was broken down to something that Python (2.7) hides from me and I still don't understand. First, I tried to use u".." strings consistently in my code, but that resulted in the infamous UnicodeEncodeError . I tried using .encode('utf8') , but that didn't help either. Finally, it turned out I shouldn't use either and it all works out automagically. However, I (here I need to give credit to a friend who helped me) did notice something weird while banging my head against the wall. sys.getdefaultencoding() returns ascii ,

Shell script call from Android.mk, standard output and missing separator error

僤鯓⒐⒋嵵緔 提交于 2019-11-30 09:13:43
I have a simple Android.mk file: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) $(shell ($(LOCAL_PATH)/echo_test.sh)) LOCAL_MODULE := libecho_test LOCAL_MODULE_TAGS := optional include $(BUILD_SHARED_LIBRARY) The interesting thing that it does is to call the 'echo_test.sh' bash script. In the case when the contents of the script are #!/bin/bash echo 'echo is working' >&2 or #!/bin/bash echo 'echo is working' >/dev/null everything is OK. Things go wrong when the bash script is #!/bin/bash echo 'echo is working' or #!/bin/bash echo 'echo is working' >&1 Then the returned error is Android.mk

How can I display the output of a Opscode Chef bash command in my console?

送分小仙女□ 提交于 2019-11-30 08:38:09
I use Vagrant to spawn a standard "precise32" box and provision it with Chef so I can test my Node.js code on Linux when I work on a Windows machine. This works fine. I also have this bash command so it auto installs my npm modules: bash "install npm modules" do code <<-EOH su -l vagrant -c "cd /vagrant && npm install" EOH end This also works fine except that I never see the console output if it completes successfully. But I'd like to see it so we can visually monitor what is going on. This is not specific to npm. I see this similar question with no concrete answers: Vagrant - how to print

using bash: write bit representation of integer to file

无人久伴 提交于 2019-11-30 08:34:46
I have a file with binary data and I need to replace a few bytes in a certain position. I've come up with the following to direct bash to the offset and show me that it found the place I want: dd bs=1 if=file iseek=24 conv=block cbs=2 | hexdump Now, to use "file" as the output: echo anInteger | dd bs=1 of=hextest.txt oseek=24 conv=block cbs=2 This seems to work just fine, I can review the changes made in a hex editor. Problem is, "anInteger" will be written as the ASCII representation of that integer (which makes sense) but I need to write the binary representation. I want to use bash for this

Write to terminal after redirecting stdout to a file without using stderr?

大兔子大兔子 提交于 2019-11-30 08:31:42
问题 I have two shell scripts, one that serves as the main "program" and another that serves as a "library." In several places in the "program," I'll do something like: log "$thing" >> "$logfile" , where log is a function defined in the "library." # program.sh logfile="log.txt" stuff="hahah heheh hoho" . library.sh for thing in $stuff; do log "$thing" >> "$logfile" done My question: Is there a way to redirect some of the output from the function back to the terminal without using stderr ? #

What do double parentheses mean in a function call? e.g. func(stuff)(stuff)?

此生再无相见时 提交于 2019-11-30 08:25:38
Original title: " Help me understand this weird Python idiom? sys.stdout = codecs.getwriter('utf-8')(sys.stdout) " I use this idiom all the time to print a bunch of content to standard out in utf-8 in Python 2.*: sys.stdout = codecs.getwriter('utf-8')(sys.stdout) But to be honest, I have no idea what the (sys.stdout) is doing. It sort of reminds me of a Javascript closure or something. But I don't know how to look up this idiom in the Python docs. Can any of you fine folks explain what's going on here? Thanks! .getwriter returns a function callable object; you are merely calling it in the same