0

I want my motd to make a nice overview of my system status. ATM I'm trying to check if a deamon is running or not and color it accordingly to it's status.

So normally you would enter deamon_name status and it outputs something like Deamon_name running / not running I got it to the point where I check if the Not is contained or not. That worked.

but then I noticed, that when I actually login and trigger the MOTD, I get some wrong information, I then noticed that I need to use dash and not bash or shell. And now my compare funtion does'nt work anymore.

if [[ $Server_name =~ .*Not.* ]]
    then 
        printf "NOT RUNNING";
    else 
        printf "RUNNING";
fi

This is my compare function and the check (later I want to add colors red/green)

$Server_name Not running. or running

4

2 回答 2

3

dash您可以使用 case 语句进行模式匹配,这也适用于bash

case $Server_name in 
  (*Not*) printf "NOT RUNNING" ;;
  (*)     printf "RUNNING"
esac

或者

case $Server_name in 
  (*Not*) printf "NOT "
esac
printf "RUNNING"
于 2014-12-20T12:35:43.687 回答
0

我通过比较初始化脚本中的整个Server RunningNot Running字符串来解决它,因为我知道这些消息和破折号不支持扩展功能,这似乎很合适。

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"
于 2014-12-12T10:18:20.697 回答