我尝试使用printf创建格式化输出。但我无法按照自己的意愿调整输出。我的代码如下:
#!/bin/bash
header="%-10s %8s %13s %15s %3s\n"
format="%-10s %8s %13s %15s %3s\n"
lvalue1="10000000000"
rvalue1="10000000000"
lvalue2="20000000000"
rvalue2="20000000000"
lvalue3="10000"
rvalue3="10000"
lvalue4="300000000"
rvlaue4="300000000"
lvalue5="2000"
rvalue5="2000"
value6="1500"
value7="2200"
lvalue8="2300"
rvalue8="2300"
formula1="formula1"
formula2="formula2"
formula3="formula3"
formula4="formula4"
formula5="formula5"
formula6="formula6"
formula7="formula7"
formula8="formula8"
check1="Total Received Packets"
check2="Total Transmitted Packets"
check3="Packets Lost"
check4="Media Access Control Address"
check5="IPv4 Default Gateway"
check6="Input Rate (bits/sec)"
check7="Output Rate (bits/sec)"
check8="Ignored Packets"
printf "$header" "CHECK" "FORMULA" "LVALUE" "RVALUE" "PASSED"
printf "$format" \
"$check1" "$formula1" "$lvalue1" "$rvalue1" "y/n" \
"$check2" "$formula2" "$lvalue2" "$rvalue2" "y/n" \
"$check3" "$formula3" "$lvalue3" "$rvalue3" "y/n" \
"$check4" "$formula4" "$lvalue4" "$rvalue4" "y/n" \
"$check5" "$formula5" "$lvalue5" "$rvalue5" "y/n" \
"$check6" "$formula6" "$value6" " " "y/n" \
"$check7" "$formula7" "$value7" " " "y/n" \
"$check8" "$formula8" "$lvalue8" "$rvalue8" "y/n"
输出如下:
CHECK FORMULA LVALUE RVALUE PASSED
Total Received Packets formula1 10000000000 10000000000 y/n
Total Transmitted Packets formula2 20000000000 20000000000 y/n
Packets Lost formula3 10000 10000 y/n
Media Access Control Address formula4 300000000 y/n
IPv4 Default Gateway formula5 2000 2000 y/n
Input Rate (bits/sec) formula6 1500 y/n
Output Rate (bits/sec) formula7 2200 y/n
Ignored Packets formula8 2300 2300 y/n
如您所见,字段未正确对齐。那么我应该在哪里修改以更好地查看此表?
由于
答案 0 :(得分:0)
printf
字段长度为最小长度。你的第一个字段太短,所以,例如:
format="%-30s %8s %13s %15s %3s\n"