当我试图通过以下代码启动cmd.exe时:
use std::process::{Command, Stdio};
use std::io::{BufRead, Write, BufReader};
fn main() {
let mut child_cmd = Command::new("cmd.exe")
// seems it work well with `/bin/bash` in Linux
.stdin(Stdio::piped())
.stdout(Stdio::piped()) // Error
我读过,您可以将I/O重定向到Bash中的各种构造(例如"if“和"while")。当我尝试这一点时,我注意到在if结构的末尾使用一个“AC.26”可以阻止我在if构造中覆盖的任何变量生效;但是如果我使用">“代替,那么变量修改就生效了。
#!/bin/bash
VAR_0='Unmodified'
if true
then
VAR_0='Changed'
fi | cat
echo "VAR_0: $VAR_0"
###########
VAR_1='Unmodified'
我有一个类似于下面的bash脚本
func() {
echo "func was called"
var="right"
}
var="wrong"
func
echo $var
var="wrong"
func | tee file
echo $var
由于func()将var设置为"right",因此我希望echo打印该字符串两次。但是,我注意到,如果通过管道重定向函数的输出(而不是使用>将其重定向到文件),则不会设置环境变量。在这种情况下,输出为
right
func was called
我有一个python脚本test.py:
print "first"
import os
os.system("echo second")
在linux命令行上,我执行
python test.py
它返回:
first
second
然后我执行
python test.py > test.out; cat test.out
它会返回
second
first
如果重定向输出使os.system调用在打印语句之前打印,该怎么办?