Padding characters in printf

前端 未结 13 814
半阙折子戏
半阙折子戏 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:32

    Pure Bash. Use the length of the value of 'PROC_NAME' as offset for the fixed string 'line':

    line='----------------------------------------'
    PROC_NAME='abc'
    printf "%s %s [UP]\n" $PROC_NAME "${line:${#PROC_NAME}}"
    PROC_NAME='abcdef'
    printf "%s %s [UP]\n" $PROC_NAME "${line:${#PROC_NAME}}"
    

    This gives

    abc ------------------------------------- [UP]
    abcdef ---------------------------------- [UP]
    

提交回复
热议问题