我想在可以打印的变量中获取命令的原始彩色输出。
我有以下脚本:
#Colors
RED='\033[0;31m'
NC='\033[0m' # No Color
# Runing test cases
res=$(script -q /dev/null mocha $(find "tests/non-ui" -name "*.js")) #Command that runs test cases
RESULT=$?
[ $RESULT -ne 0 ] && echo -e "$res ${RED}Some TestCaes did not pass. Please check your code.${NC}" && exit 1
echo "All test cases passed.\n"
exit 0
它给出了以下输出:
Some TestCaes did not pass. Please check your code.:13:16)
其中:13:16)
part 是该命令输出的最后一些字符res=$(script -q /dev/null mocha $(find "tests/non-ui" -name "*.js"))
。
单独运行mocha $(find "tests/non-ui" -name "*.js")
此命令将产生以下输出:
All element creation test:
1) Label
0 passing (16ms)
1 failing
1) All element creation test: Label:
AssertionError: false == true
+ expected - actual
-false
+true
at Context.<anonymous> (tests/non-ui/sample-test.js:13:16)
如何在变量中获取整个输出?
答案1
在命令周围添加双引号:
res="$(script -q /dev/null mocha $(find "tests/non-ui" -name "*.js"))"
echo "$res"
这将保留 ANSI 颜色。