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

后端 未结 3 664
日久生厌
日久生厌 2020-12-28 22:37

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 relo

相关标签:
3条回答
  • 2020-12-28 23:00

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

    #!/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=$1
      printf "$STRING "
      shift
      "$@" && echo_success $"$STRING" || echo_failure $"$STRING"
      rc=$?
      echo
      return $rc
    }
    
    action testing true
    action testing false
    
    0 讨论(0)
  • 2020-12-28 23:05
    #!/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
    
    0 讨论(0)
  • 2020-12-28 23:05

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

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

    #!/bin/bash
    . /etc/init.d/functions # include the said file
    
    action "Description of the action" command
    
    exit 0
    

    assuming command returns 0 on 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.

    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 !

    0 讨论(0)
提交回复
热议问题