我有这样一个剧本:
#!/bin/bash
temp=`inxi -xxx -w`
regex="(Conditions:(.+))(Wind:(.+))Humidity"
[[ $temp =~ $regex ]]
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[3]}
它很酷,除了这样的细节 - 字体颜色已经改变,现在整个文字变成了蓝色。我该怎样预防呢?
答案 0 :(得分:2)
避免蓝色输出:
#!/bin/bash
temp=$(inxi -xxx -w | sed -r 's/\x1B\[[0-9;]*[JKmsu]//g')
regex="(Conditions:(.+))(Wind:(.+))Humidity"
[[ $temp =~ $regex ]]
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[3]}
我所做的改变:
`
)已弃用。使用$(command)
代替`command`
。inxi
正则表达式将sed
的输出中的颜色代码移除到I管道上。正确使用蓝色输出:
#!/bin/bash
temp=$(inxi -xxx -w)
regex="(Conditions:(.+))(Wind:(.+))Humidity"
[[ $temp =~ $regex ]]
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[3]}
printf "\e[0m" # Reset the color.
\e
或\033
代替\x1b
。一切都是一样的。[0m
。[39;49;00m
printf
代替echo -e
,但我不建议使用。{/ li>