1
我嘗試使用printf創建格式化輸出。但我無法按照我的意願對齊輸出。我的代碼是象下面這樣:使用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
正如你看到的,字段不正確對齊。那麼我應該在哪裏修改以更好地查找此表?
由於
謝謝,我錯過了。現在看起來更好。 – JavaRed