Padding characters in printf

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

    If you are ending the pad characters at some fixed column number, then you can overpad and cut to length:

    # Previously defined:
    # PROC_NAME
    # PROC_STATUS
    
    PAD="--------------------------------------------------"
    LINE=$(printf "%s %s" "$PROC_NAME" "$PAD" | cut -c 1-${#PAD})
    printf "%s %s\n" "$LINE" "$PROC_STATUS"
    

提交回复
热议问题