Padding characters in printf

前端 未结 13 836
半阙折子戏
半阙折子戏 2020-11-30 16:58

I am writing a bash shell script to display if a process is running or not.

So far, I got this:

printf \"%-50s %s\\n\" $PROC_NAME [UP]
13条回答
  •  心在旅途
    2020-11-30 17:56

    Simple but it does work:

    printf "%-50s%s\n" "$PROC_NAME~" "~[$STATUS]" | tr ' ~' '- '
    

    Example of usage:

    while read PROC_NAME STATUS; do  
        printf "%-50s%s\n" "$PROC_NAME~" "~[$STATUS]" | tr ' ~' '- '
    done << EOT 
    JBoss DOWN
    GlassFish UP
    VeryLongProcessName UP
    EOT
    

    Output to stdout:

    JBoss -------------------------------------------- [DOWN]
    GlassFish ---------------------------------------- [UP]
    VeryLongProcessName ------------------------------ [UP]
    

提交回复
热议问题