一、脚本排错参数-x
[root@localhost jiaofan]# cat cbe.sh
#!/bin/bash
#测试continue、break、exit
for i in {1..5}
do
for ((j=0;j<5;j++))
do
if [[ $j -eq 3 ]] ;then
exit
fi
echo -n "$i$j "
done
echo
done
[root@localhost jiaofan]# sh -x cbe.sh #<==会展示每一步的步骤
+ for i in '{1..5}'
+ (( j=0 ))
+ (( j<5 ))
+ [[ 0 -eq 3 ]]
+ echo -n '10 '
10 + (( j++ ))
+ (( j<5 ))
+ [[ 1 -eq 3 ]]
+ echo -n '11 '
11 + (( j++ ))
+ (( j<5 ))
+ [[ 2 -eq 3 ]]
+ echo -n '12 '
12 + (( j++ ))
+ (( j<5 ))
+ [[ 3 -eq 3 ]]
+ exit
二、终端属性设置
注:shopt 和 set 也可以改变终端属性,这两个我感觉意义不大,也不常用,就没有做笔记。
这里介绍一个可以改变显示位置的设置(tput)
1)通过cols 可以显示终端列数,就是一行可以显示多少个字符
[root@localhost jiaofan]# tput cols
171
2)通过 lines 可以显示当前终端的行数
[root@localhost jiaofan]# tput lines
33
3)通过clear可以清屏
[root@localhost jiaofan]# tput clear
4)通过cup 可以移动光标到特定位置的行与列
[root@localhost jiaofan]# tput cup 10 20 #<==移动光标至 10 行 20 列
[root@localhost jiaofan]# tput cup 20 10 #<==移动光标至 20 行 10 列
5)通过 sc 可以将当前光标的位置保存,rc 可以将光标还原到之后一次 sc 保存的位置。
[root@localhost jiaofan]# tput sc #<==保存当前光标位置
[root@localhost jiaofan]# ls #<==执行命令
[root@localhost jiaofan]# tput rc #<==恢复光标位置
[root@localhost jiaofan]# tput sc ; tput cup 10 10 ; echo "welcome" ; tput rc
[root@localhost jiaofan]#
6)通过civis 可以设置不显示光标,通过cvvis 或者 cnorm 可以设置显示光标
[root@localhost jiaofan]# tput civis
[root@localhost jiaofan]# tput cnorm
[root@localhost jiaofan]# tput civis
[root@localhost jiaofan]# tput cvvis
7)通过blink 可以想终端设置为闪烁模式,bold 可以将终端设置为加粗模式,rev 可以将当前终端的字体色和背景互换。
[root@localhost jiaofan]# tput blink
[root@localhost jiaofan]# tput bold
[root@localhost jiaofan]# tput rev
8)通过 smcup 可保存当前屏幕,rmcup 可以还原最近保存的屏幕状态。
[root@localhost jiaofan]# echo "hello"
hello
[root@localhost jiaofan]# echo "save screen"
save screen
[root@localhost jiaofan]# tput smcup #<==保存屏幕状态
[root@localhost jiaofan]# tput rmcup #<==恢复屏幕状态
9)通过sgr0 可以取消所有终端属性,将终端还原为正常状态
[root@localhost jiaofan]# tput sgr0
10)使用 reset 命令也可以将我们的当前终端重置为初始状态。
[root@localhost jiaofan]# reset
三、电子时钟案例
[root@localhost jiaofan]# cat clock.sh
#!/bin/bash
trap 'tput cnorm;exit' INT
number=(
' 0000000000 111 2222222222 3333333333 44 44 5555555555 6666666666'
' 00 00 11111 22 33 44 44 55 66 '
' 00 00 111111 22 33 44 44 55 66 '
' 00 00 11 22 33 44 44 55 66 '
' 00 00 11 2222222222 3333333333 4444444444 5555555555 6666666666'
' 00 00 11 22 33 44 55 66 66'
' 00 00 11 22 33 44 55 66 66'
' 00 00 11 22 33 44 55 66 66'
' 0000000000 111111111 2222222222 3333333333 44 5555555555 6666666666'
)
clear
draw_map(){
tput cup 1 35
for x in `seq 90`
do
printf "\e[46m#\e[0m"
done
for y in {0..16}
do
tput cup $[$y+2] 35
printf "\e[46m##\e[0m"
tput cup $[$y+2] 123
printf "\e[46m##\e[0m"
done
tput cup 19 35
for x in `seq 90`
do
printf "\e[46m#\e[0m"
done
}
now_time(){
hour=$(date +%H)
min=$(date +%M)
sec=$(date +%S)
hour_left=`echo $hour/10 | bc`
hour_right=`echo $hour%10 | bc`
min_left=`echo $min/10 | bc`
min_right=`echo $min%10 | bc`
sec_left=`echo $sec/10 | bc `
sec_right=`echo $sec%10 | bc `
}
print_time(){
begin=$[$1*12]
for i in `seq 0 ${#number[@]}`
do
tput cup $[i+5] $2
echo -en "\e[32m${number[i]:$begin:12}\e[0m"
done
}
print_punct(){
tput cup $1 $2
echo -en "\e[32m\u2588\e[0m"
}
draw_map
while :
do
tput civis
now_time
tput cup 2 75
echo -en "\e[5m${hour_left}${hour_right}:${min_left}${min_right}:${sec_left}${sec_right}\e[0m"
print_time ${hour_left} $[2+35]
print_time ${hour_right} $[14+35]
print_punct 8 $[28+35]
print_punct 10 $[28+35]
print_time ${min_left} $[30+35]
print_time ${min_right} $[42+35]
print_punct 8 $[56+35]
print_punct 10 $[56+35]
print_time ${sec_left} $[58+35]
print_time ${sec_right} $[70+35]
tput cup 19 1
echo
sleep 1
done
注:number() 里面有个数据,每个数据都是用单引号引用的。
效果展示: