命令替换内部的bash逻辑 [英] bash logic inside command substitution
本文介绍了命令替换内部的bash逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在登录时会显示一个小横幅
I have a little banner that I display at login
fignow() { figlet -w $(tput cols) $(date +"%b %d, Week %V"); figlet -w $(tput cols) -f univers $(date +"%H:%M"); }
如果为了获得 univers
而安装了其他字体,此方法就可以正常工作,但这不是默认安装的一部分,因此我可以执行以下操作:
This works fine if the additional fonts are installed to get univers
, but that's not part of the default installation, so I can do the following:
fignow() { figlet -w $(tput cols) $(date +"%b %d, Week %V"); if [ -f /usr/share/figlet/univers.flf ]; then figlet -w $(tput cols) -f univers $(date +"%H:%M"); else figlet -w $(tput cols) $(date +"%H:%M"); fi; }
这也可以正常工作,但是我很好奇是否可以删除 if/else/fi
,并且逻辑可以在命令本身内部完成,即:
This also works fine, but I was curious if the if/else/fi
could be removed and the logic could be done inside the command itself, i.e. something like:
fignow() { figlet -w $(tput cols) $(date +"%b %d, Week %V"); figlet -w $(tput cols) -f $(use-univers-or-failback-to-standard.flf-if-univers.flf-is-not-available) $(date +"%H:%M"); }
有可能吗?
推荐答案
我建议使用 figlist
避免对主题路径进行硬编码:
I suggest to use figlist
to avoid hardcoding the themes path:
figlet -w "$(tput cols)" -f "$((figlist | /bin/grep -hw universe) || echo "standard")" "foo"
这篇关于命令替换内部的bash逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文