bash字符串中心化处理

CENTRAL STRING


function central_line() {

    #输入字符串长度 
    str_length=${#1}
    
    #echo ${str_length}
    
    #中心线长度
    line_length=$(( (120-${str_length}) / 2 ))
    
    #echo ${line_length}
    
    line=`eval printf "=%0.s" {1..${line_length}}`
    
    #echo ${line}
    
    if ((str_length % 2)) ; then
    
        printf "${line} $1  ${line}\n" 
    
    else
    
        printf "${line} $1 ${line}\n"
    
    fi
    
}

central_line "hello world"
central_line "hello worldu"
central_line "lina how are you now"

posted @ 2021-04-17 00:14  MOVIT  阅读(36)  评论(0编辑  收藏  举报