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

后端 未结 3 686
日久生厌
日久生厌 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: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 !

提交回复
热议问题