我正在努力为我要做的事情找到一个好的解决方案。
因此,我有一个通过yarn run start:e2e
启动的CreateReactApp实例。一旦来自该命令的输出具有"0";编译成功";,我希望能够在bash脚本中运行下一个命令。
我尝试过不同的东西:
if yarn run start:e2e | grep "Compiled successfully"; then
exit 0
fi
echo "THIS NEEDS TO RUN"
这看起来确实会停止日志,但不会运行下一个命令。
yarn run start:e2e | while read -r line;
do
echo "$line"
if [[ "$line" == *"Compiled successfully!"* ]]; then
exit 0
fi
done
echo "THIS NEEDS TO RUN"
yarn run start:e2e | grep -q "Compiled successfully";
echo $?
echo "THIS NEEDS TO RUN"
我已经阅读了管道/过程替换之间的差异,但没有看到关于我的用例的实际实现。。
有人能告诉我我做错了什么吗?
提前感谢!
EDIT:因为我提出了多个解决方案,但都不起作用,所以我可能会重新定义我的主要问题。
所以
yarn run start:e2e
启动了一个react应用程序,它有一种";手表;模式因此,它在";编译成功"部分,当源代码、类型检查等发生更改时
React部分启动后(因此,如果成功输出了Compiled日志(,日志不再重要,但localhost:3000(纱线编译到的(必须保持活动状态。然后,我在yarn运行后运行其他命令,在localhost:3000上进行一些测试
所以基本上我想在pseudo中实现什么(命令A中的管道内容非常抽象,甚至可能看起来不像正确的解决方案,但试图彻底解释(:
# command A
yarn run dev | cmd_to_watch_the_output "Compiled succesfully" | exit 0 -> localhost:3000 active but the shell is back in 'this' window
-> keep watching the output until Compiled succesfully occurs
-> If it occurs, then the logs does not matter anymore and I want to run command B
# command B
echo "I WANT TO SEE THIS LOG"
... do other stuff ...
I hope this clears it up a bit more :D
Thanks already for the propositions!
如果您希望yarn run
即使在Compiled successfully
之后也能继续运行,则不能将其stdout直接管道传输到该行之后退出的另一个程序:该stdout需要有位置,这样yarn
将来写入日志的尝试就不会失败或阻塞。
#!/usr/bin/env bash
case $BASH_VERSION in
''|[0-3].*|4.[012].*) echo "Error: bash 4.3+ required" >&2; exit 1;;
esac
exec {yarn_fd}< <(yarn run); yarn_pid=$!
while IFS= read -r line <&$yarn_fd; do
printf '%sn' "$line"
if [[ $line = *"Compiled successfully!"* ]]; then
break
fi
done
# start a background process that reads future stdout from `yarn run`
cat <&$yarn_fd >/dev/null & cat_pid=$!
# close the FD from that background process so `cat` has the only copy
exec {yarn_fd}<&-
echo "Doing other things here!"
echo "When ready to shut down yarn, kill $yarn_pid and $cat_pid"