Bash 用破折号检查子字符串的字符串
我希望我的motd对我的系统状态有一个很好的概述。 ATM我试图检查执事是否正在运行,并根据其状态对其进行相应的着色 因此,通常您会输入Bash 用破折号检查子字符串的字符串,bash,shell,ubuntu,dash-shell,Bash,Shell,Ubuntu,Dash Shell,我希望我的motd对我的系统状态有一个很好的概述。 ATM我试图检查执事是否正在运行,并根据其状态对其进行相应的着色 因此,通常您会输入deamon\u name status,它会输出类似deamon\u name running/not running的内容,我会检查not是否包含。成功了 但后来我注意到,当我实际登录并触发MOTD时,我得到了一些错误信息,然后我注意到我需要使用dash,而不是bash或shell。 现在我的比较功能不起作用了 if [[ $Server_name =~ .
deamon\u name status
,它会输出类似deamon\u name running/not running
的内容,我会检查not
是否包含。成功了
但后来我注意到,当我实际登录并触发MOTD时,我得到了一些错误信息,然后我注意到我需要使用dash
,而不是bash或shell。
现在我的比较功能不起作用了
if [[ $Server_name =~ .*Not.* ]]
then
printf "NOT RUNNING";
else
printf "RUNNING";
fi
这是我的比较函数和检查(稍后我想添加红色/绿色)
$Server\u name
未运行。
或运行
我通过比较init脚本中的整个服务器运行
或未运行
字符串解决了这个问题,因为我知道这些消息,并且dash不支持扩展功能,这似乎是合适的
if [ "$ServerName" = "Not running." ] # I know those messages
then
printf '%b' "\033[31;1mNOT RUNNING\033[0m" # print NOT RUNNING in red
else
printf '%b' "\033[32;1mRUNNING\033[0m" # print RUNNING in greend
fi
printf "\n"
我通过比较init脚本中运行的整个
服务器
或未运行的字符串解决了这个问题,因为我知道这些消息,并且dash不支持扩展功能,所以这似乎是合适的
if [ "$ServerName" = "Not running." ] # I know those messages
then
printf '%b' "\033[31;1mNOT RUNNING\033[0m" # print NOT RUNNING in red
else
printf '%b' "\033[32;1mRUNNING\033[0m" # print RUNNING in greend
fi
printf "\n"
我通过比较init脚本中运行的整个服务器
或未运行的字符串解决了这个问题,因为我知道这些消息,并且dash不支持扩展功能,所以这似乎是合适的
if [ "$ServerName" = "Not running." ] # I know those messages
then
printf '%b' "\033[31;1mNOT RUNNING\033[0m" # print NOT RUNNING in red
else
printf '%b' "\033[32;1mRUNNING\033[0m" # print RUNNING in greend
fi
printf "\n"
我通过比较init脚本中运行的整个服务器
或未运行的字符串解决了这个问题,因为我知道这些消息,并且dash不支持扩展功能,所以这似乎是合适的
if [ "$ServerName" = "Not running." ] # I know those messages
then
printf '%b' "\033[31;1mNOT RUNNING\033[0m" # print NOT RUNNING in red
else
printf '%b' "\033[32;1mRUNNING\033[0m" # print RUNNING in greend
fi
printf "\n"
在dash
中,可以使用case语句进行模式匹配,这也可以在bash
中使用:
case $Server_name in
(*Not*) printf "NOT RUNNING" ;;
(*) printf "RUNNING"
esac
或
在dash
中,可以使用case语句进行模式匹配,这也可以在bash
中使用:
case $Server_name in
(*Not*) printf "NOT RUNNING" ;;
(*) printf "RUNNING"
esac
或
在dash
中,可以使用case语句进行模式匹配,这也可以在bash
中使用:
case $Server_name in
(*Not*) printf "NOT RUNNING" ;;
(*) printf "RUNNING"
esac
或
在dash
中,可以使用case语句进行模式匹配,这也可以在bash
中使用:
case $Server_name in
(*Not*) printf "NOT RUNNING" ;;
(*) printf "RUNNING"
esac
或