2011-03-31 18 views
8

Estoy buscando producir mensajes de éxito/error alineados a la derecha en bash. Un ejemplo sería lo apache2 produce en la ejecución de: sudo /etc/init.d/apache2 reload etc.bash: echo algo en el extremo derecho de la ventana (alineado a la derecha)

En ejemplo anterior, apache2 produce muy agradable y concisa [OK] o [fail] mensaje que se alineado a la derecha.

Además, me encantaría saber cómo obtener el texto en rojo, en el caso, vamos a producir un mensaje [fail].

+1

uso printf; vea http://stackoverflow.com/questions/2199843/bourne-shell-left-right- justify –

Respuesta

7
#!/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 
+2

@SiegeX: 'col = $ (tput cols)' es mejor que hardcoding y más consistente con los otros usos de 'tput' . Además, su método consume más espacio para los escapes de color, que en realidad no ocupan espacio. 'printf '% s% * s% s'" $ GREEN "$ col '[OK]'" $ NORMAL "' podría ser más limpio. – geekosaur

+0

@geek: Decidí no usar 'tput cols' y también' tput cup' porque el primero no le da la flexibilidad para especificar la columna (puede que no se vea mejor en el borde peludo) y el último requiere la fila #. – SiegeX

+1

@SiegeX: puede usar 'tput hpa' para este último. – geekosaur

2

Tener un vistazo a este hilo, podría ser interesante: how to write a bash script like the ones used in init.d?

en Linux CentOS 6.5, estoy usando el archivo /etc/init.d/functions:

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

action "Description of the action" command 

exit 0 

asumiendo command devuelve 0 en caso de éxito, valor positivo si ocurre un error. Para que el script sea fácil de leer, utilizo una llamada a función en lugar de todo el comando.

Aquí se muestra un ejemplo:

#!/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 

lo que resulta en: console output (nota: locale francés ;-))

esperanza que va a ayudar!

+1

tenga en cuenta que la importación de '/ etc/init.d/functions' modifica el' $ PATH'. – epsilonhalbe

0

Aquí hay algo en su mayoría sobre la base de la escritura de los centos 'funciones', pero más simplificada

#!/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 
    "[email protected]" && echo_success $"$STRING" || echo_failure $"$STRING" 
    rc=$? 
    echo 
    return $rc 
} 

action testing true 
action testing false 
Cuestiones relacionadas