Simulate Linux boot service to detect print effects

Source: Internet
Author: User

Linux Boot detection service status display Success or FailureThe state. There is no discovery, regardless of the name of the service, the last paragraph can be Snap To displayIt?

Can we write a script to simulate it? ^_^

 来我们首先来分析一下: 首先,确定一下当前终端的宽度 其次,确定一下服务状态显示所占的字符数 再次,确定一下服务名称所占的字符数 剩余的就是打印空格了

Now that's the idea, let's write a script, edit service_status.sh.

#!/bin/bash##获取当前终端的宽度COLUMNS=`resize -u | grep "COLUMNS=" | cut -d "=" -f2 | cut -d ";" -f1`#判断当前终端宽度是否为空,为空则设置默认值为80[ -z $COLUMNS ] && COLUMNS=‘80‘#当前终端宽度减去服务状态显示所在的字符数,设置服务状态显示字符数为10,[   OK    ][ FAILED  ]S_COLUMNS=$[$COLUMNS-10]#设置颜色变量RED=‘\033[31m‘GREEN=‘\033[32m‘NORMAL=‘\033[0m‘##设置成功服务的显示success(){        STRING=$1        #真实需要打印的空格数        R_COLUMNS=$[$S_COLUMNS-${#STRING}]        #显示服务名称,不换行        echo -n "$STRING"        #接着打印空格        for i in `seq 1 $R_COLUMNS`;do                echo -n " "        done        #最后打印服务成功字样        echo -e "[   ${GREEN}OK${NORMAL}   ]"}failed(){        STRING=$1        #真实需要打印的空格数        R_COLUMNS=$[$S_COLUMNS-${#STRING}]        #显示服务名称,不换行        echo -n "$STRING"        #接着打印空格        for i in `seq 1 $R_COLUMNS`;do                echo -n " "        done        #最后打印服务成功字样        echo -e "[ ${RED}FAILED${NORMAL} ]"}

Next, edit the other file test.sh, which calls the service_status.sh write function.

#!/bin/bash#. ./service_status.sh#测试启动成功服务的显示success "Service start success!"#测试启动失败服务的显示failed "Service start failed~"

Next, execute permissions for test.sh chmod u+x test.sh
Start execution#./test.sh

Simulate Linux boot service to detect print effects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.