根据我之前的问题here https://stackoverflow.com/questions/76900817/check-whether-string-contains-emoji-in-posix-if-statement,请参阅下面我的问题的更完整形式。
代码用例
当前的代码是module https://github.com/polybar/polybar/wiki/User-contributed-modules用于polybar https://github.com/polybar/polybar,可定制的状态栏。根据第一个链接,使用配置文件和调用的每个条形图绘制多条形图(例如,通过运行polybar mybar
)读取任何给定模块的配置文件。可以看到一个完整的例子here https://wiki.archlinux.org/title/Polybar#Sample_configuration。通过调用相关脚本文件来加载自定义或用户定义的模块。例如:
[module/test]
type = custom/script
exec = some/script/file
tail = true
Polybar 至少接受 shell 脚本和 python 脚本(也许还有其他脚本,尽管我还没有对此进行测试)。出于这个问题的目的,我关心我的 shell 脚本的适当性和not它与 Polybar 的接口(目前按预期工作;尽管如@EdMortonexplains https://stackoverflow.com/questions/76900817/check-whether-string-contains-emoji-in-posix-if-statement/76900818?noredirect=1#comment135568149_76900818,这可能不是预期的功能,因此这个问题)。
当前代码
以下是我的代码的最小工作示例。
#!/bin/bash
smile=#d1dadf;
sad=#ff5833;
angry=#1a88a4;
laugh=#daf7a6;
while true; do
# get hour
time=$(date +%_H -u);
# set empty hour
timeShow=TEST;
# set empty emoji
emoji=;
if [ $time -ge 0 ] && [ $time -lt 6 ];
then
emoji=%{F$smile}????%{F-}
elif [ $time -ge 6 ] && [ $time -lt 12 ];
then
emoji=%{F$sad}????%{F-}
elif [ $time -ge 12 ] && [ $time -lt 18 ];
then
emoji=%{F$angry}????%{F-}
elif [ $time -ge 18 ] && [ $time -ne 0 ];
then
emoji=%{F$laugh}????%{F-}
fi
if [[ "$emoji" =~ [????] ]];
then
((timeShow = time - 1))
elif [[ "$emoji" =~ [????] ]];
then
((timeShow = time - 2))
elif [[ "$emoji" =~ [????] ]];
then
((timeShow = time - 3))
fi
echo "$timeShow:$(date '+%M' -u) $emoji"
sleep 1
done
输入(和输出)的解释
按原样运行脚本不需要输入(输出是从脚本本身内部计算的)。因此,运行脚本只需通过以下方式完成:
./script.sh
输出应该(并且确实)返回格式化的时间,以及用颜色格式标签包裹的表情符号,如上所述。
目前的程序采用当前 UTC 时间date
。该值(存储在变量time
) 然后被修改并存储在timeShow
。假设特定的时间范围(因此emoji
),然后脚本修改timeShow
通过减去 1、2 或 3。表情符号可在字符串中找到emoji
通过执行正则表达式操作来定位由颜色格式包装的字符。
给定硬编码输入的预期输出
更改以下内容:
while true; do... done
To this:
for time in 8 12 22 0; do... done
此外,应更改以下行以匹配以下内容:
# get hour
#time=$(date +%_H -u);
# set default hour
timeShow=0
假设脚本按小时运行,应给出以下预期输出。
循环 1(上午 8 点)(偏移量 -1):
7:00 %{F#ff5833}????%{F-}
循环 2(中午 12 点)(偏移量 -2):
10:00 %{F#1a88a4}????%{F-}
循环 3(晚上 10 点)(偏移量 -3):
19:00 %{F#daf7a6}????%{F-}
循环 4(上午 12 点)(偏移量为 0):
0:00 %{F#d1dadf}????%{F-}
所需输出
该脚本按预期工作。然而,如上所述,这可能并非偶然。此问题的目的是了解所使用的方法是否产生正确的输出,或者仅产生正确的输出happens这样做。正则表达式目前隔离了特点通过使用[????]
。对于任何可能包含 2 个或更多字符的字形,M-bM-^XM-^A$
例如,这样隔离表情符号,[????]
可能不准确,因为它会匹配第一个对应的字符,从而导致不良或意外的行为。提供了对该问题的进一步解释here https://stackoverflow.com/questions/76900817/check-whether-string-contains-emoji-in-posix-if-statement/76900818#comment135568149_76900818.