1. 其实shell下也可以将输出的数据以表格的格式输出,看起来更加直观些。数据存放的aa文件里面,以空格/tab/回车分割,数据依次是姓名,年龄,性别,籍贯,序列
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#!/bin/bash
# It
is
a scripts of make tab
#
"aa"
is
filename of store about info,
DI=(`cat aa`)
DATA_NUM=${#DI[*]}
tab_top()
{
echo -n -e
"+------------------------------------------------------------------------------+\n"
printf
"%-1s %-16s %-1s %-8s %-1s %-8s %-1s %-16s %-1s %-16s %-1s\n"
\| name \| age \| sex \|
native
\| education \|
}
tab_mod()
{
echo -n -e
"+------------------+----------+----------+------------------+------------------+\n"
printf
"%-1s %-16s %-1s %-8s %-1s %-8s %-1s %-16s %-1s %-16s %-1s\n"
\| ${DI[i]} \| ${DI[i+
1
]} \| ${DI[i+
2
]} \| ${DI[i+
3
]} \| ${DI[i+
4
]} \|
}
tab_end()
{
echo -n -e
"+------------------------------------------------------------------------------+\n"
}
clear
tab_top
for
(( i=
0
;i<
"$DATA_NUM"
;i=i+
5
))
do
tab_mod
done
tab_end
exit
0
|
2. shell 进度条控制
关键命令:tput
说 明:这个进度条不是很理想,理想中的应该百分比在进度条后,而且位置固定才对,只是printf语句不好控制,所以改成了百分比随进度条移动。另外shell不支持浮点运算,用bc -l算出来的数据取整数在$RATE_MAX不能整除$BAR_NUM是,会出现进度条到99%结束了,所以为了规避这个错误,在后面有添加了
tput rc
tput ed
printf
"%-0s %-15s"
"$BAR_PRO"
" 100% completed."
三句,勉强达到效果。
实际应用,可以下面代码以模块的格式拷贝到执行的脚本中,修改$TIME_CONTROL为你脚本运行的时间,然后模块以后台模式运行,记得最后结束语句前加wait语句。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#!/bin/bash
# It
is
a scripts of process bar.
# Variable
"BAR"
is
use
of control process bar color.
# Variable
"TIME_CONTROL"
is
use
of control the scripts run time.
# Variable
"BAR_NUM"
is
use
of control process bar length.
process_bar()
{
BAR=`echo -e
"\033[32;42m \033[0m"
`
TOP_NOTE=
"Starting get system info:"
END_NOTE=
'done.'
TIME_CONTROL=
20
BAR_NUM=
25
RATE_MAX=
100
PER_TIME=`echo $TIME_CONTROL/$BAR_NUM | bc -l`
PER_RATE=`echo $RATE_MAX/$BAR_NUM | bc -l`
echo -n
"$TOP_NOTE "
tput sc
for
(( i=
1
;i<=$BAR_NUM;i++ ))
do
tput rc
tput ed
BAR_PRO=$BAR_PRO$BAR
I=`echo $PER_RATE*$i | bc -l | awk -F .
'{print $1}'
`
printf
"%-0s %-15s"
"$BAR_PRO"
"$I% completed."
sleep $PER_TIME
done
tput rc
tput ed
printf
"%-0s %-15s"
"$BAR_PRO"
" 100% completed."
}
process_bar &
sleep
20
wait
echo
echo
"done."
exit
0
|
本文转自 chengchow 51CTO博客,原文链接:http://blog.51cto.com/chengchow/1323601,如需转载请自行联系原作者