- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在之前的question我问如何写入程序的标准输入。这个问题建立在那个问题之上。现在我问“我如何解析我调用的程序的输出”。
在这个例子中我正在调用一个我可以编辑的程序(Sum.sh)这一事实与问题无关。如果我不控制我正在调用的程序的实现,我希望这种行为。我想“替换”程序期望的物理人并通过脚本驱动它。
给定两个脚本
总和.sh:
#!/bin/bash
sum=0
inp=0
while true;
do
printf "Sum: %d\n" $sum
printf "Give me an integer: "
read inp
if [ "$inp" == 0 ]; then
exit
fi
sum=$((sum+inp))
done
和driver.sh:
#!/bin/bash
rm -f /tmp/sumpipe
mkfifo /tmp/sumpipe
./Sum.sh < /tmp/sumpipe &
echo 2 > /tmp/sumpipe
echo 5 > /tmp/sumpipe
echo 0 > /tmp/sumpipe
sleep 1 # this is required for some reason or the script hangs
然后我运行 driver.sh 我得到以下信息:
$ ./driver.sh
Sum: 0
Give me an integer: Sum: 2
Give me an integer: Sum: 7
Give me an integer: $
终端输出的格式对于这个问题并不重要。
我想做的是解析所述输出以便在 driver.sh 中做出决定。在以下 driver.sh 的伪代码中,我尝试展示我想要做的事情:
#!/bin/bash
rm -f /tmp/sumpipe
mkfifo /tmp/sumpipe
output=$(./Sum.sh < /tmp/sumpipe &)
while true; do
if [ "$output" != empty ]; then
(make some decision based on what output is and echo back to my process if necessary)
fi
done
不幸的是,$output
从来不包含任何东西,我试过很多版本的abusing read和 piping在这个应用程序中,他们都没有为我工作。如何重定向我的后台程序的标准输出以便我可以解析它?
我尝试了 OrangesV 提出的解决方案(并尊重 William Pursell 关于输出缓冲区太空的建议),方法如下:
#!/bin/bash
rm -f /tmp/sumpipe
mkfifo /tmp/sumpipe
while read output; do
echo "2347823" > /tmp/sumpipe
if [ -z $output ]; then
continue
else
echo "got it!"
echo $output
sleep 1
fi
done < <(./Sum.sh < /tmp/sumpipe &)
不幸的是,“明白了!”从来没有回应过,所以它似乎没有用。
如果相关,我的 bash 版本:
$ bash --version
GNU bash, version 4.4.0(7)-rc2 (x86_64-unknown-linux-gnu)
现在有一个部分解决方案。 Diego Torres Milano 的回答
#!/bin/bash
inpipe=/tmp/sumpipei
outpipe=/tmp/sumpipeo
rm -f $inpipe
rm -f $outpipe
mkfifo $inpipe
mkfifo $outpipe
./Sum.sh < $inpipe > $outpipe &
count=0
while true;
do
while read line
do
echo "line=$line"
done < $outpipe &
echo 2 > $inpipe
done
我尝试了各种 stdbuf
的调用(例如:stdbuf -o0 ./Sum.sh <$inpipe > $outpipe &
)。 stdbuf
在这种情况下显然无法强制关闭缓冲,因为根据 stdbuf
上的 GNU coreutils 手册对于 command
在stdbuf option… command
:
command must start with the name of a program that does ... not adjust the buffering of its standard streams (note the program tee is not in this category).
不幸的是,这正是我在这里所做的。
我将 Diego 的回答标记为正确,即使它在 stdio 缓冲区问题得到解决之前不起作用。我将提出下一个问题来解决 stdio 缓冲问题。我几乎可以在这里尝到奖品的滋味。
最佳答案
使用另一个管道输出
mkfifo /tmp/output
./Sum.sh < /tmp/sumpipe > /tmp/output &
你可以处理它
while read line
do
echo "line=$line"
done < /tmp/output &
关于linux - 在 bash 中如何为我正在后台处理的任意程序解析和/或重定向标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39757914/
我用 IntelliJ IDEA 2021.1 CE 在 流行!_OS 20.04 与 bash 5.0.17 . 问题造句:我将IntelliJ终端设置为/bin/bash通过 IntelliJ 设
给定如下命令: bash --shortcuts 我想显示一个快捷方式列表,就像在这个页面上一样: http://www.skorks.com/2009/09/bash-shortcuts-for-m
我有一个脚本可以操作数据、创建参数并将它们发送到第二个脚本。其中一个参数包含一个空格。 脚本1.sh: args=() args+=("A") args+=("1 2") args+=("B") .
我的脚本的“只运行一次”版本的一个非常简单的示例: ./myscript.sh var1 "var2 with spaces" var3 #!/bin/bash echo $1 #output: va
我想了解数字( double )在 bash 中是如何表示的,以及当我在 bash 中以十六进制格式打印数字时会发生什么。 根据 IEEE 754 标准,double 应由 64 位表示:52 位(1
我试图在 bash -c "..." 命令中获取 bash 脚本,但它不起作用。 如果我在 bash -c "..." 之外运行命令,它会起作用。 我需要使用 bash -c "..." 因为我想确保
如何检测我的 bash shell 中是否加载了 bash 补全包?从 bash-completion 的 2.1 版(包含在 Debian 8 中)开始,除了 BASH_COMPLETION_COM
我的 bash_profile 中有一个投影函数。现在我试图从 bash 脚本中调用这个函数,但是我得到了一个未找到的错误。如何使投影函数对 bash 脚本可见? 最佳答案 必须导出函数 export
我正在编写一个 bash 脚本,它接受许多命令行参数(可能包括空格)并通过登录 shell 将它们全部传递给程序 (/bin/some_program)。从 bash 脚本调用的登录 shell 将取
当我创建一个新的 bash 进程时,提示符默认为一个非常简单的提示符。我知道我可以编辑 .bashrc 等来更改它,但是有没有办法使用 bash 命令传递提示? 谢谢! 最佳答案 提示由 PS1、PS
好的,我希望这个问题有一定道理,但是 bash shell 和 bash 终端之间有什么区别?例子。当我第一次打开终端时,会提示我当前的目录和用户名。在终端窗口标题中显示 -bash- ,当我键入 e
我是 SBCL 的新手,我正在尝试从 bash 终端运行存储在文本文件中的 Lisp 脚本。 这是我在文件开头写的内容 http://www.sbcl.org/manual/#Running-from
我知道我们可以在 bash 中使用将十六进制转换为十进制 #!/bin/bash echo "Type a hex number" read hexNum echo $(( 16#$hexNum ))
我正在尝试在 bash 脚本中自动完成文件夹名称。如果我输入完整的文件夹名称,一切正常,但我不知道如何自动完成名称。有什么想法吗? repo() { cd ~/Desktop/_REPOS/$1 }
我想检查远程网站上的一些文件。 这里是bash命令生成计算文件md5的命令 [root]# head -n 3 zrcpathAll | awk '{print $3}' | xargs -I {}
是否有任何内置函数可以使用 bash shell 脚本从给定日期获取下周日(下周一、下周二等)?例如,2014 年 9 月 1 日之后的第一个星期日是什么时候?我预计 2014 年 9 月 7 日。
我一直在尝试根据表格重命名一些特定文件,但没有成功。它要么重命名所有文件,要么给出错误。 该目录包含数百个以长条形码命名的文件,我只想重命名包含模式 _1_ 的文件。 例子 barcode_1_bar
bash 中有没有办法用变量的内容替换文本文件中的占位符? 例如,我想发送一封电子邮件通知,如下所示: Dear Foo, Alert: blah blah blah blah blah blah
我有一个 bash 脚本,它在某些字符串上附加了一个重音字符,导致它失败,我找不到这些字符在哪里或如何进入那里。 这是一些示例输出: mv: cannot move â/tmp/myapp.zipâ
这个问题在这里已经有了答案: How do I place stdout on edit line? (1 个回答) Can a bash script prepopulate the prompt
我是一名优秀的程序员,十分优秀!