如何在zsh-theme中运行Shell命令?

时间:2020-10-30 01:19:06

标签: zsh oh-my-zsh

因此,我已经启动并运行了Oh My Zsh,并且正在创建自己的新zsh主题。在其中,我希望从https://api.myip.com获取外部IP地址-我正在使用curl&grep来获取它。当我在命令提示符下输入它时,它工作正常,但是当嵌入到我的zsh-theme文件中时,它给我一个错误:

zsh: no matches found: ((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5]).){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])
(23) Failed writing body

Jacobs-MacBook-Pro-2.local jacobjackson ttys002 0 [                               ] 10/29/20 18:32:46 PM

这是我的zsh主题:

PROMPT='%F{white}%M %n %y %j $(curl -s https://api.myip.com | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])') %F{green}%2c%F{blue} [%f '
RPROMPT='$(git_prompt_info) %F{blue}] %F{green}%W %* %F{yellow}%D{%p}%f'

ZSH_THEME_GIT_PROMPT_PREFIX="%F{yellow}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%f"
ZSH_THEME_GIT_PROMPT_DIRTY=" %F{red}*%f"
ZSH_THEME_GIT_PROMPT_CLEAN=""

这是获取IP地址的命令序列:

curl -s https://api.myip.com | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])'

1 个答案:

答案 0 :(得分:0)

尝试一下:

# Function name that's compatible with
# http://zsh.sourceforge.net/Doc/Release/User-Contributions.html#Prompt-Themes
# in case you ever want to build a full prompt theme.
# `-s` to prevent `curl` from outputting a progress bar.
# Use a service that simply outputs our IP, so we don't have to parse anything.
prompt_jacobjackson_precmd() { 
  psvar[1]=$(curl -s ifconfig.co) 
}

# `precmd` hooks get executed just before each new prompt.
autoload -Uz add-zsh-hook 
add-zsh-hook precmd prompt_jacobjackson_precmd

# `%1v` inserts the 1st element of the `psvar` array. See
# http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Conditional-Substrings-in-Prompts
PS1='%1v > '