bash:将某些内容回显到窗口的右端(右对齐)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5506176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:43:05  来源:igfitidea点击:

bash: echo something to right end of window (right aligned)

bash

提问by Stoic

I am looking for producing success/fail messages which are right aligned in bash. An example would be what apache2 produces on executing: sudo /etc/init.d/apache2 reloadetc.

我正在寻找在 bash 中正确对齐的成功/失败消息。一个例子是 apache2 在执行时产生的内容:sudo /etc/init.d/apache2 reload等。

In above example, apache2 produces very nice and concise [OK]or [fail]message which are right aligned.

在上面的例子中,apache2 产生非常漂亮和简洁的[OK][fail]右对齐的消息。

Also, would love to know how to get the text red, in case, we are to produce a [fail]message.

另外,很想知道如何使文本变红,以防万一,我们要生成一条[fail]消息。

采纳答案by SiegeX

#!/bin/bash

RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)

col=80 # change this to whatever column you want the output to start at

if <some condition here>; then
  printf '%s%*s%s' "$GREEN" $col "[OK]" "$NORMAL"
else
  printf '%s%*s%s' "$RED" $col "[FAIL]" "$NORMAL"
fi

回答by Alo?ké Go

Have a look at this thread, might be interesting : how to write a bash script like the ones used in init.d?

看看这个线程,可能会很有趣:如何编写像 init.d 中使用的那样的 bash 脚本?

On Linux CentOS 6.5, i'm using the /etc/init.d/functions file:

在 Linux CentOS 6.5 上,我使用 /etc/init.d/functions 文件:

#!/bin/bash
. /etc/init.d/functions # include the said file

action "Description of the action" command

exit 0

assuming commandreturns 0on success, positive value if an error occurs. To let the script be easy to read, i use a function call instead of the whole command.

假设成功command返回0,如果发生错误,则为正值。为了让脚本易于阅读,我使用函数调用而不是整个命令。

Here is an example:

下面是一个例子:

#!/bin/bash

. /etc/init.d/functions

this_will_fail()
{
    # Do some operations...
    return 1
}

this_will_succeed()
{
    # Do other operations...
    return 0
}


action "This will fail"     this_will_fail
action "This will succeed"  this_will_succeed

exit 0

resulting in: console output(note : french locale ;-) )

结果:( 控制台输出注意:法语语言环境;-))

Hope it'll help !

希望它会有所帮助!

回答by Neil McGill

Here is something mostly based on the centos 'functions' script, but more stripped down

这里有一些主要基于 centos 'functions' 脚本的东西,但更多的是精简

#!/bin/bash

RES_COL=60
MOVE_TO_COL="printf \033[${RES_COL}G"

DULL=0
BRIGHT=1

FG_BLACK=30
FG_RED=31
FG_GREEN=32
FG_YELLOW=33
FG_BLUE=34
FG_MAGENTA=35
FG_CYAN=36
FG_WHITE=37

ESC="^[["
NORMAL="${ESC}m"
RESET="${ESC}${DULL};${FG_WHITE};${BG_NULL}m"

BLACK="${ESC}${DULL};${FG_BLACK}m"
RED="${ESC}${DULL};${FG_RED}m"
GREEN="${ESC}${DULL};${FG_GREEN}m"
YELLOW="${ESC}${DULL};${FG_YELLOW}m"
BLUE="${ESC}${DULL};${FG_BLUE}m"
MAGENTA="${ESC}${DULL};${FG_MAGENTA}m"
CYAN="${ESC}${DULL};${FG_CYAN}m"
WHITE="${ESC}${DULL};${FG_WHITE}m"

SETCOLOR_SUCCESS=$GREEN
SETCOLOR_FAILURE=$RED
SETCOLOR_NORMAL=$RESET

echo_success() {
  $MOVE_TO_COL
  printf "["
  printf $SETCOLOR_SUCCESS
  printf $"  OK  "
  printf $SETCOLOR_NORMAL
  printf "]"
  printf "\r"
  return 0
}

echo_failure() {
  $MOVE_TO_COL
  printf "["
  printf $SETCOLOR_FAILURE
  printf $"FAILED"
  printf $SETCOLOR_NORMAL
  printf "]"
  printf "\r"
  return 1
}

action() {
  local STRING rc

  STRING=
  printf "$STRING "
  shift
  "$@" && echo_success $"$STRING" || echo_failure $"$STRING"
  rc=$?
  echo
  return $rc
}

action testing true
action testing false